Click here to Skip to main content
15,884,298 members
Articles / Web Development / ASP.NET

Designing and implementing a versatile data access tier for an ASP.NET application

Rate me:
Please Sign up or sign in to vote.
4.63/5 (45 votes)
3 Feb 200328 min read 384.5K   3.8K   242  
In this article, we will drill down deeper in to the design of a n-tier architecture and our focus will be on the data access tier (DAT)
//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.0.3705.209
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

namespace TDS {
    using System;
    using System.Data;
    using System.Xml;
    using System.Runtime.Serialization;
    
    
    [Serializable()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Diagnostics.DebuggerStepThrough()]
    [System.ComponentModel.ToolboxItem(true)]
    public class DSCustomer : DataSet {
        
        private CustomersDataTable tableCustomers;
        
        private OrdersDataTable tableOrders;
        
        private DataRelation relationRelCustomersOrders;
        
        public DSCustomer() {
            this.InitClass();
            System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
            this.Tables.CollectionChanged += schemaChangedHandler;
            this.Relations.CollectionChanged += schemaChangedHandler;
        }
        
        protected DSCustomer(SerializationInfo info, StreamingContext context) {
            string strSchema = ((string)(info.GetValue("XmlSchema", typeof(string))));
            if ((strSchema != null)) {
                DataSet ds = new DataSet();
                ds.ReadXmlSchema(new XmlTextReader(new System.IO.StringReader(strSchema)));
                if ((ds.Tables["SPSelCustomers"] != null)) {
                    this.Tables.Add(new CustomersDataTable(ds.Tables["SPSelCustomers"]));
                }
                if ((ds.Tables["SPSelOrders"] != null)) {
                    this.Tables.Add(new OrdersDataTable(ds.Tables["SPSelOrders"]));
                }
                this.DataSetName = ds.DataSetName;
                this.Prefix = ds.Prefix;
                this.Namespace = ds.Namespace;
                this.Locale = ds.Locale;
                this.CaseSensitive = ds.CaseSensitive;
                this.EnforceConstraints = ds.EnforceConstraints;
                this.Merge(ds, false, System.Data.MissingSchemaAction.Add);
                this.InitVars();
            }
            else {
                this.InitClass();
            }
            this.GetSerializationData(info, context);
            System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
            this.Tables.CollectionChanged += schemaChangedHandler;
            this.Relations.CollectionChanged += schemaChangedHandler;
        }
        
        [System.ComponentModel.Browsable(false)]
        [System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)]
        public CustomersDataTable Customers {
            get {
                return this.tableCustomers;
            }
        }
        
        [System.ComponentModel.Browsable(false)]
        [System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)]
        public OrdersDataTable Orders {
            get {
                return this.tableOrders;
            }
        }
        
        public override DataSet Clone() {
            DSCustomer cln = ((DSCustomer)(base.Clone()));
            cln.InitVars();
            return cln;
        }
        
        protected override bool ShouldSerializeTables() {
            return false;
        }
        
        protected override bool ShouldSerializeRelations() {
            return false;
        }
        
        protected override void ReadXmlSerializable(XmlReader reader) {
            this.Reset();
            DataSet ds = new DataSet();
            ds.ReadXml(reader);
            if ((ds.Tables["SPSelCustomers"] != null)) {
                this.Tables.Add(new CustomersDataTable(ds.Tables["SPSelCustomers"]));
            }
            if ((ds.Tables["SPSelOrders"] != null)) {
                this.Tables.Add(new OrdersDataTable(ds.Tables["SPSelOrders"]));
            }
            this.DataSetName = ds.DataSetName;
            this.Prefix = ds.Prefix;
            this.Namespace = ds.Namespace;
            this.Locale = ds.Locale;
            this.CaseSensitive = ds.CaseSensitive;
            this.EnforceConstraints = ds.EnforceConstraints;
            this.Merge(ds, false, System.Data.MissingSchemaAction.Add);
            this.InitVars();
        }
        
        protected override System.Xml.Schema.XmlSchema GetSchemaSerializable() {
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            this.WriteXmlSchema(new XmlTextWriter(stream, null));
            stream.Position = 0;
            return System.Xml.Schema.XmlSchema.Read(new XmlTextReader(stream), null);
        }
        
        internal void InitVars() {
            this.tableCustomers = ((CustomersDataTable)(this.Tables["SPSelCustomers"]));
            if ((this.tableCustomers != null)) {
                this.tableCustomers.InitVars();
            }
            this.tableOrders = ((OrdersDataTable)(this.Tables["SPSelOrders"]));
            if ((this.tableOrders != null)) {
                this.tableOrders.InitVars();
            }
            this.relationRelCustomersOrders = this.Relations["RelCustomersOrders"];
        }
        
        private void InitClass() {
            this.DataSetName = "DSCustomer";
            this.Prefix = "";
            this.Namespace = "http://tempuri.org/DSCustomer.xsd";
            this.Locale = new System.Globalization.CultureInfo("en-US");
            this.CaseSensitive = false;
            this.EnforceConstraints = true;
            this.tableCustomers = new CustomersDataTable();
            this.Tables.Add(this.tableCustomers);
            this.tableOrders = new OrdersDataTable();
            this.Tables.Add(this.tableOrders);
            ForeignKeyConstraint fkc;
            fkc = new ForeignKeyConstraint("RelCustomersOrders", new DataColumn[] {
                        this.tableCustomers.CustomerIDColumn}, new DataColumn[] {
                        this.tableOrders.CustomerIDColumn});
            this.tableOrders.Constraints.Add(fkc);
            fkc.AcceptRejectRule = AcceptRejectRule.Cascade;
            fkc.DeleteRule = Rule.Cascade;
            fkc.UpdateRule = Rule.Cascade;
            this.relationRelCustomersOrders = new DataRelation("RelCustomersOrders", new DataColumn[] {
                        this.tableCustomers.CustomerIDColumn}, new DataColumn[] {
                        this.tableOrders.CustomerIDColumn}, false);
            this.Relations.Add(this.relationRelCustomersOrders);
        }
        
        private bool ShouldSerializeCustomers() {
            return false;
        }
        
        private bool ShouldSerializeOrders() {
            return false;
        }
        
        private void SchemaChanged(object sender, System.ComponentModel.CollectionChangeEventArgs e) {
            if ((e.Action == System.ComponentModel.CollectionChangeAction.Remove)) {
                this.InitVars();
            }
        }
        
        public delegate void CustomerChangeEventHandler(object sender, CustomerChangeEvent e);
        
        public delegate void OrderChangeEventHandler(object sender, OrderChangeEvent e);
        
        [System.Diagnostics.DebuggerStepThrough()]
        public class CustomersDataTable : DataTable, System.Collections.IEnumerable {
            
            private DataColumn columnCustomerID;
            
            private DataColumn columnCompanyName;
            
            private DataColumn columnAddress;
            
            private DataColumn columnCity;
            
            internal CustomersDataTable() : 
                    base("SPSelCustomers") {
                this.InitClass();
            }
            
            internal CustomersDataTable(DataTable table) : 
                    base(table.TableName) {
                if ((table.CaseSensitive != table.DataSet.CaseSensitive)) {
                    this.CaseSensitive = table.CaseSensitive;
                }
                if ((table.Locale.ToString() != table.DataSet.Locale.ToString())) {
                    this.Locale = table.Locale;
                }
                if ((table.Namespace != table.DataSet.Namespace)) {
                    this.Namespace = table.Namespace;
                }
                this.Prefix = table.Prefix;
                this.MinimumCapacity = table.MinimumCapacity;
                this.DisplayExpression = table.DisplayExpression;
            }
            
            [System.ComponentModel.Browsable(false)]
            public int Count {
                get {
                    return this.Rows.Count;
                }
            }
            
            internal DataColumn CustomerIDColumn {
                get {
                    return this.columnCustomerID;
                }
            }
            
            internal DataColumn CompanyNameColumn {
                get {
                    return this.columnCompanyName;
                }
            }
            
            internal DataColumn AddressColumn {
                get {
                    return this.columnAddress;
                }
            }
            
            internal DataColumn CityColumn {
                get {
                    return this.columnCity;
                }
            }
            
            public Customer this[int index] {
                get {
                    return ((Customer)(this.Rows[index]));
                }
            }
            
            public event CustomerChangeEventHandler CustomerChanged;
            
            public event CustomerChangeEventHandler CustomerChanging;
            
            public event CustomerChangeEventHandler CustomerDeleted;
            
            public event CustomerChangeEventHandler CustomerDeleting;
            
            public void AddCustomer(Customer row) {
                this.Rows.Add(row);
            }
            
            public Customer AddCustomer(string CustomerID, string CompanyName, string Address, string City) {
                Customer rowCustomer = ((Customer)(this.NewRow()));
                rowCustomer.ItemArray = new object[] {
                        CustomerID,
                        CompanyName,
                        Address,
                        City};
                this.Rows.Add(rowCustomer);
                return rowCustomer;
            }
            
            public System.Collections.IEnumerator GetEnumerator() {
                return this.Rows.GetEnumerator();
            }
            
            public override DataTable Clone() {
                CustomersDataTable cln = ((CustomersDataTable)(base.Clone()));
                cln.InitVars();
                return cln;
            }
            
            protected override DataTable CreateInstance() {
                return new CustomersDataTable();
            }
            
            internal void InitVars() {
                this.columnCustomerID = this.Columns["CustomerID"];
                this.columnCompanyName = this.Columns["CompanyName"];
                this.columnAddress = this.Columns["Address"];
                this.columnCity = this.Columns["City"];
            }
            
            private void InitClass() {
                this.columnCustomerID = new DataColumn("CustomerID", typeof(string), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnCustomerID);
                this.columnCompanyName = new DataColumn("CompanyName", typeof(string), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnCompanyName);
                this.columnAddress = new DataColumn("Address", typeof(string), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnAddress);
                this.columnCity = new DataColumn("City", typeof(string), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnCity);
                this.Constraints.Add(new UniqueConstraint("keyCustomerOrders", new DataColumn[] {
                                this.columnCustomerID}, false));
                this.columnCustomerID.AllowDBNull = false;
                this.columnCustomerID.Unique = true;
                this.columnCompanyName.AllowDBNull = false;
            }
            
            public Customer NewCustomer() {
                return ((Customer)(this.NewRow()));
            }
            
            protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {
                return new Customer(builder);
            }
            
            protected override System.Type GetRowType() {
                return typeof(Customer);
            }
            
            protected override void OnRowChanged(DataRowChangeEventArgs e) {
                base.OnRowChanged(e);
                if ((this.CustomerChanged != null)) {
                    this.CustomerChanged(this, new CustomerChangeEvent(((Customer)(e.Row)), e.Action));
                }
            }
            
            protected override void OnRowChanging(DataRowChangeEventArgs e) {
                base.OnRowChanging(e);
                if ((this.CustomerChanging != null)) {
                    this.CustomerChanging(this, new CustomerChangeEvent(((Customer)(e.Row)), e.Action));
                }
            }
            
            protected override void OnRowDeleted(DataRowChangeEventArgs e) {
                base.OnRowDeleted(e);
                if ((this.CustomerDeleted != null)) {
                    this.CustomerDeleted(this, new CustomerChangeEvent(((Customer)(e.Row)), e.Action));
                }
            }
            
            protected override void OnRowDeleting(DataRowChangeEventArgs e) {
                base.OnRowDeleting(e);
                if ((this.CustomerDeleting != null)) {
                    this.CustomerDeleting(this, new CustomerChangeEvent(((Customer)(e.Row)), e.Action));
                }
            }
            
            public void RemoveCustomer(Customer row) {
                this.Rows.Remove(row);
            }
        }
        
        [System.Diagnostics.DebuggerStepThrough()]
        public class Customer : DataRow {
            
            private CustomersDataTable tableCustomers;
            
            internal Customer(DataRowBuilder rb) : 
                    base(rb) {
                this.tableCustomers = ((CustomersDataTable)(this.Table));
            }
            
            public string CustomerID {
                get {
                    return ((string)(this[this.tableCustomers.CustomerIDColumn]));
                }
                set {
                    this[this.tableCustomers.CustomerIDColumn] = value;
                }
            }
            
            public string CompanyName {
                get {
                    return ((string)(this[this.tableCustomers.CompanyNameColumn]));
                }
                set {
                    this[this.tableCustomers.CompanyNameColumn] = value;
                }
            }
            
            public string Address {
                get {
                    try {
                        return ((string)(this[this.tableCustomers.AddressColumn]));
                    }
                    catch (InvalidCastException e) {
                        throw new StrongTypingException("Cannot get value because it is DBNull.", e);
                    }
                }
                set {
                    this[this.tableCustomers.AddressColumn] = value;
                }
            }
            
            public string City {
                get {
                    try {
                        return ((string)(this[this.tableCustomers.CityColumn]));
                    }
                    catch (InvalidCastException e) {
                        throw new StrongTypingException("Cannot get value because it is DBNull.", e);
                    }
                }
                set {
                    this[this.tableCustomers.CityColumn] = value;
                }
            }
            
            public bool IsAddressNull() {
                return this.IsNull(this.tableCustomers.AddressColumn);
            }
            
            public void SetAddressNull() {
                this[this.tableCustomers.AddressColumn] = System.Convert.DBNull;
            }
            
            public bool IsCityNull() {
                return this.IsNull(this.tableCustomers.CityColumn);
            }
            
            public void SetCityNull() {
                this[this.tableCustomers.CityColumn] = System.Convert.DBNull;
            }
            
            public Order[] GetOrders() {
                return ((Order[])(this.GetChildRows(this.Table.ChildRelations["RelCustomersOrders"])));
            }
        }
        
        [System.Diagnostics.DebuggerStepThrough()]
        public class CustomerChangeEvent : EventArgs {
            
            private Customer eventRow;
            
            private DataRowAction eventAction;
            
            public CustomerChangeEvent(Customer row, DataRowAction action) {
                this.eventRow = row;
                this.eventAction = action;
            }
            
            public Customer Row {
                get {
                    return this.eventRow;
                }
            }
            
            public DataRowAction Action {
                get {
                    return this.eventAction;
                }
            }
        }
        
        [System.Diagnostics.DebuggerStepThrough()]
        public class OrdersDataTable : DataTable, System.Collections.IEnumerable {
            
            private DataColumn columnOrderID;
            
            private DataColumn columnCustomerID;
            
            private DataColumn columnOrderDate;
            
            internal OrdersDataTable() : 
                    base("SPSelOrders") {
                this.InitClass();
            }
            
            internal OrdersDataTable(DataTable table) : 
                    base(table.TableName) {
                if ((table.CaseSensitive != table.DataSet.CaseSensitive)) {
                    this.CaseSensitive = table.CaseSensitive;
                }
                if ((table.Locale.ToString() != table.DataSet.Locale.ToString())) {
                    this.Locale = table.Locale;
                }
                if ((table.Namespace != table.DataSet.Namespace)) {
                    this.Namespace = table.Namespace;
                }
                this.Prefix = table.Prefix;
                this.MinimumCapacity = table.MinimumCapacity;
                this.DisplayExpression = table.DisplayExpression;
            }
            
            [System.ComponentModel.Browsable(false)]
            public int Count {
                get {
                    return this.Rows.Count;
                }
            }
            
            internal DataColumn OrderIDColumn {
                get {
                    return this.columnOrderID;
                }
            }
            
            internal DataColumn CustomerIDColumn {
                get {
                    return this.columnCustomerID;
                }
            }
            
            internal DataColumn OrderDateColumn {
                get {
                    return this.columnOrderDate;
                }
            }
            
            public Order this[int index] {
                get {
                    return ((Order)(this.Rows[index]));
                }
            }
            
            public event OrderChangeEventHandler OrderChanged;
            
            public event OrderChangeEventHandler OrderChanging;
            
            public event OrderChangeEventHandler OrderDeleted;
            
            public event OrderChangeEventHandler OrderDeleting;
            
            public void AddOrder(Order row) {
                this.Rows.Add(row);
            }
            
            public Order AddOrder(Customer parentCustomerByRelCustomersOrders, System.DateTime OrderDate) {
                Order rowOrder = ((Order)(this.NewRow()));
                rowOrder.ItemArray = new object[] {
                        null,
                        parentCustomerByRelCustomersOrders[0],
                        OrderDate};
                this.Rows.Add(rowOrder);
                return rowOrder;
            }
            
            public Order FindByOrderID(int OrderID) {
                return ((Order)(this.Rows.Find(new object[] {
                            OrderID})));
            }
            
            public System.Collections.IEnumerator GetEnumerator() {
                return this.Rows.GetEnumerator();
            }
            
            public override DataTable Clone() {
                OrdersDataTable cln = ((OrdersDataTable)(base.Clone()));
                cln.InitVars();
                return cln;
            }
            
            protected override DataTable CreateInstance() {
                return new OrdersDataTable();
            }
            
            internal void InitVars() {
                this.columnOrderID = this.Columns["OrderID"];
                this.columnCustomerID = this.Columns["CustomerID"];
                this.columnOrderDate = this.Columns["OrderDate"];
            }
            
            private void InitClass() {
                this.columnOrderID = new DataColumn("OrderID", typeof(int), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnOrderID);
                this.columnCustomerID = new DataColumn("CustomerID", typeof(string), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnCustomerID);
                this.columnOrderDate = new DataColumn("OrderDate", typeof(System.DateTime), null, System.Data.MappingType.Element);
                this.Columns.Add(this.columnOrderDate);
                this.Constraints.Add(new UniqueConstraint("DSCustomerKey1", new DataColumn[] {
                                this.columnOrderID}, true));
                this.columnOrderID.AutoIncrement = true;
                this.columnOrderID.AllowDBNull = false;
                this.columnOrderID.ReadOnly = true;
                this.columnOrderID.Unique = true;
            }
            
            public Order NewOrder() {
                return ((Order)(this.NewRow()));
            }
            
            protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {
                return new Order(builder);
            }
            
            protected override System.Type GetRowType() {
                return typeof(Order);
            }
            
            protected override void OnRowChanged(DataRowChangeEventArgs e) {
                base.OnRowChanged(e);
                if ((this.OrderChanged != null)) {
                    this.OrderChanged(this, new OrderChangeEvent(((Order)(e.Row)), e.Action));
                }
            }
            
            protected override void OnRowChanging(DataRowChangeEventArgs e) {
                base.OnRowChanging(e);
                if ((this.OrderChanging != null)) {
                    this.OrderChanging(this, new OrderChangeEvent(((Order)(e.Row)), e.Action));
                }
            }
            
            protected override void OnRowDeleted(DataRowChangeEventArgs e) {
                base.OnRowDeleted(e);
                if ((this.OrderDeleted != null)) {
                    this.OrderDeleted(this, new OrderChangeEvent(((Order)(e.Row)), e.Action));
                }
            }
            
            protected override void OnRowDeleting(DataRowChangeEventArgs e) {
                base.OnRowDeleting(e);
                if ((this.OrderDeleting != null)) {
                    this.OrderDeleting(this, new OrderChangeEvent(((Order)(e.Row)), e.Action));
                }
            }
            
            public void RemoveOrder(Order row) {
                this.Rows.Remove(row);
            }
        }
        
        [System.Diagnostics.DebuggerStepThrough()]
        public class Order : DataRow {
            
            private OrdersDataTable tableOrders;
            
            internal Order(DataRowBuilder rb) : 
                    base(rb) {
                this.tableOrders = ((OrdersDataTable)(this.Table));
            }
            
            public int OrderID {
                get {
                    return ((int)(this[this.tableOrders.OrderIDColumn]));
                }
                set {
                    this[this.tableOrders.OrderIDColumn] = value;
                }
            }
            
            public string CustomerID {
                get {
                    try {
                        return ((string)(this[this.tableOrders.CustomerIDColumn]));
                    }
                    catch (InvalidCastException e) {
                        throw new StrongTypingException("Cannot get value because it is DBNull.", e);
                    }
                }
                set {
                    this[this.tableOrders.CustomerIDColumn] = value;
                }
            }
            
            public System.DateTime OrderDate {
                get {
                    try {
                        return ((System.DateTime)(this[this.tableOrders.OrderDateColumn]));
                    }
                    catch (InvalidCastException e) {
                        throw new StrongTypingException("Cannot get value because it is DBNull.", e);
                    }
                }
                set {
                    this[this.tableOrders.OrderDateColumn] = value;
                }
            }
            
            public Customer Customer {
                get {
                    return ((Customer)(this.GetParentRow(this.Table.ParentRelations["RelCustomersOrders"])));
                }
                set {
                    this.SetParentRow(value, this.Table.ParentRelations["RelCustomersOrders"]);
                }
            }
            
            public bool IsCustomerIDNull() {
                return this.IsNull(this.tableOrders.CustomerIDColumn);
            }
            
            public void SetCustomerIDNull() {
                this[this.tableOrders.CustomerIDColumn] = System.Convert.DBNull;
            }
            
            public bool IsOrderDateNull() {
                return this.IsNull(this.tableOrders.OrderDateColumn);
            }
            
            public void SetOrderDateNull() {
                this[this.tableOrders.OrderDateColumn] = System.Convert.DBNull;
            }
        }
        
        [System.Diagnostics.DebuggerStepThrough()]
        public class OrderChangeEvent : EventArgs {
            
            private Order eventRow;
            
            private DataRowAction eventAction;
            
            public OrderChangeEvent(Order row, DataRowAction action) {
                this.eventRow = row;
                this.eventAction = action;
            }
            
            public Order Row {
                get {
                    return this.eventRow;
                }
            }
            
            public DataRowAction Action {
                get {
                    return this.eventAction;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Switzerland Switzerland
Paul Abraham is a software developer who designs and develops multi-shop systems.

He has received his M.Sc in Mathematics and Computer Science from the FernUniversität Hagen(http://www.fernuni-hagen.de Germany) and his main interests are neural networks and bayesian statistics He lives in Rosenheim (South Germany http://www.rosenheim.de). You can reach him at admin@paul-abraham.com.

Comments and Discussions