Download presentation
Presentation is loading. Please wait.
Published byDorthy Preston Modified over 9 years ago
1
June 10, 2006Applied Information Sciences, Inc Leveraging.NET Attributes to build a simple Relational Object Mapping Framework Jason Fabritz Applied Information Sciences, Inc. http://www.appliedis.com
2
June 10, 2006Applied Information Sciences, Inc Attributes Describing how to Serialize Data [Serializable] [XmlElement] Describing how to Serialize Data [Serializable] [XmlElement] Security Characteristics [CodeAccessSecurity] Security Characteristics [CodeAccessSecurity] Affect the Just-in-Time (JIT) Compiler [StructLayout] [MarshalAs] Affect the Just-in-Time (JIT) Compiler [StructLayout] [MarshalAs] Code Interoperability [ComVisible] Code Interoperability [ComVisible]
3
June 10, 2006Applied Information Sciences, Inc Usage Example using System; using System.Xml.Serialization; [Serializable] [XmlRoot( Namespace = "http://www.appliedis.com/address", IsNullable = false, ElementName = "Address" )] public class Address { [XmlAttribute( "id" )] public int Id { get; set; } [XmlElement( "Street1" )] public String Street { get; set; } [XmlElement( "City" )] public String City { get; set; } …
4
June 10, 2006Applied Information Sciences, Inc Rolling Your Own [AttributeUsage( AttributeTargets.Property, AllowMultiple=true )] public class SqlParameterAttribute : Attribute { public SqlParameterAttribute( String name, params String[] mode ) { //… } public bool ReturnsData { get { //… } set { //… } }
5
June 10, 2006Applied Information Sciences, Inc Calling a Stored Procedure public int addAddress( int personId, int type, String street, String city, String state, String zip ) { using( SqlConnection connection = new SqlConnection( connectionString ) ) { connection.Open( ); using( SqlCommand command = connection.CreateCommand( ) ) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "sp_add_address"; command.Parameters.Add( "@person_id", SqlDbType.Int ).Value = personId; command.Parameters.Add( "@type_id", SqlDbType.Int ).Value = type; command.Parameters.Add( "@street", SqlDbType.NVarChar, 255 ).Value = street; command.Parameters.Add( "@city", SqlDbType.NVarChar, 32 ).Value = city; command.Parameters.Add( "@state", SqlDbType.NVarChar, 120 ).Value = state; command.Parameters.Add( "@zip", SqlDbType.NVarChar, 12 ).Value = zip; return command.ExecuteNonQuery( ); }
6
June 10, 2006Applied Information Sciences, Inc Calling a Stored Procedure public int addAddress( Address address ) { using( SqlConnection connection = new SqlConnection( connectionString ) ) { connection.Open( ); using( SqlCommand command = connection.CreateCommand( ) ) { command.CommandType = CommandType.StoredProcedure; command.CommandText = "sp_add_address"; command.Parameters.Add( "@person_id", SqlDbType.Int ).Value = address.PersonId; command.Parameters.Add( "@type_id", SqlDbType.Int ).Value = address.AddressType; command.Parameters.Add( "@street", SqlDbType.NVarChar, 255 ).Value = address.Street; command.Parameters.Add( "@city", SqlDbType.NVarChar, 32 ).Value = address.City; command.Parameters.Add( "@state", SqlDbType.NVarChar, 120 ).Value = address.State; command.Parameters.Add( "@zip", SqlDbType.NVarChar, 12 ).Value = address.ZipCode; return command.ExecuteNonQuery( ); }
7
June 10, 2006Applied Information Sciences, Inc Calling a Stored Procedure public int addAddress( Address address ) { using( SqlEngine engine = factory.CreateSqlEngine( ) ) { return engine.Execute( address, "add" ); }
8
June 10, 2006Applied Information Sciences, Inc Calling a Stored Procedure using( SqlEngine engine = factory.CreateSqlEngine( ) ) { engine.Execute( address, "add" ); }
9
June 10, 2006Applied Information Sciences, Inc The Big Idea public class Address { public int PersonId {...}; public int AddressType {...}; public String Street {...}; public String City {...}; public String State {...}; public String ZipCode {...}; } CREATE PROCEDURE dbo.sp_add_address ( @person_id INT, @address_type_id INT, @street NVARCHAR(255), @city NVARCHAR(32), @state NVARCHAR(120), @zip NVARCHAR(12) ) AS BEGIN …
10
June 10, 2006Applied Information Sciences, Inc The Big Idea [SqlProcedure("sp_add_address“)] public class Address { [SqlParam("@person_id")] public int PersonId {...}; [SqlParam("@address_type_id")] public int AddressType {...}; [SqlParam("@street")] public String Street {...}; [SqlParam("@city")] public String City {...}; [SqlParam("@state")] public String State {...}; [SqlParam("@zip")] public String ZipCode {...}; } CREATE PROCEDURE dbo.sp_add_address ( @person_id INT, @address_type_id INT, @street NVARCHAR(255), @city NVARCHAR(32), @state NVARCHAR(120), @zip NVARCHAR(12) ) AS BEGIN …
11
June 10, 2006Applied Information Sciences, Inc The Big Idea public class Address { [SqlResult("person_id")] public int PersonId {...}; [SqlResult("address_type_id")] public int AddressType {...}; [SqlResult("street")] public String Street {...}; [SqlResult("city")] public String City {...}; [SqlResult("state")] public String State {...}; [SqlResult("zip")] public String ZipCode {...}; } CREATE PROCEDURE dbo.sp_get_address( @id int ) AS BEGIN SELECT person_id, address_type_id, street, city, state, zip FROM address WHERE id = @id END
12
June 10, 2006Applied Information Sciences, Inc Mapping Objects [SqlProcedure( "sp_add_address", "add" )] [SqlProcedure( "sp_update_address", "update" )] [SqlProcedure( "sp_delete_address", "delete" )] public class Address { [SqlParameter("@id","update","delete")] [SqlResult( "id", "add", "get" )] public int Id { get; set } [SqlParameter("@person_id","add","update")] [SqlResult("person_id","get")] public int PersonId { get; set; } [SqlParameter("@street","add","update")] [SqlResult("street","get")] public String Street { get; set; }...
13
June 10, 2006Applied Information Sciences, Inc Using Mapped Objects Address address; FindAddress finder = new FindAddress( ) ; finder.AddressId = 100 ; using( SqlEngine engine = factory.CreateSqlEngine( ) ) { address = engine.Execute ( finder, "get" ) ; } … // modify address using( SqlEngine engine = factory.CreateSqlEngine( ) ) { engine.Execute( address, "update" ) ; }
14
June 10, 2006Applied Information Sciences, Inc CODE
15
June 10, 2006Applied Information Sciences, Inc Resources MSDN http://msdn2.microsoft.com/en-us/library/z0w1kczw(VS.80).aspx MSDN http://msdn2.microsoft.com/en-us/library/z0w1kczw(VS.80).aspx NUnit http://www.nunit.org/ NUnit http://www.nunit.org/ Applied Information Sciences, Inc. http://www.appliedis.com Applied Information Sciences, Inc. http://www.appliedis.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.