Need opinion / advice on an attempt at strong typing DAL using struct

Last post 02-07-2007 1:41 PM by cmcbride. 3 replies.

Sort Posts:

  • Need opinion / advice on an attempt at strong typing DAL using struct

    01-01-2007, 7:36 PM
    • Loading...
    • ngenius
    • Joined on 12-19-2006, 2:33 AM
    • Posts 8

    I have been tooling around with the idea of using a struct to declare properties of an object to use strong typing.

    Based on that I want to be able to pass values into those parameters in a code behind file by instantiating a new instance of the struct (that part I have) then I want to be able to pass the OBJECT into a DAL class which pulls the values from the object argument and use them to populate my database.

    My code/psuedo code is below.  I have tested the code and it works but my concers run deeper.

    This is my first use in .NET passing an struct object via a form-like post scenario. This will be used in the codebehind of an e-commerce shopping cart and I want to make sure that if two customers entering information press submit at the same time that their information will not get mixed up, etc.

     s this a functial way to do this?

    Has anyone else done this before and will this work successfully in an e-commerce application without having concurrency performance and/or security issues?
     

    I want to advance with the technology and use the tools the way the are meant to be used. Any help would be appreciated. 

     Here is some quick code:

     

    //In my DAL
    public struct testDetails
    {
    public string testName;
    public string testPhone;
    }


    //In my code behind .aspx.c private void vCallDBInsertMethod()
    {
    testDetails tDetails = new testDetails
    tDetails.testName = "testvalue";
    tDetails.testPhone = tbxPhone.text;

    DAL_dbMethods.InsertDetails(tDetails)
    }



    //In my DAL_dbMethods Class public static bool InsertDetails(testDetails argTestDetails)
    {
    ...db command...

    //psuedo code here ....Add.Paramaters("@Name").value = argTestDetails.testName;

    }

     

     Thanks in advance,


     

     

  • Re: Need opinion / advice on an attempt at strong typing DAL using struct

    01-03-2007, 3:46 PM
    • Loading...
    • datakix
    • Joined on 01-03-2007, 8:06 PM
    • Posts 1

    I wouldn't worry about 2 users concurrently submitting the form at the same time and getting mixed up - the .NET Framework along w/the ASP.NET SessionID will handle that for you. However, you may want to avoid using a 'struct' datatype in that fashion as it handcuffs your extensibility.

    See: http://www.csharp-station.com/Tutorials/Lesson12.aspx

    In general, when you collect user data you don't want to mix the presentation layer code and the business logic code. Moreover, you don't want to complicate the presentation layer with complex data types as it does add some overhead. Unless there is a real business need, I'd recommend keeping it simple and just populating a class you carry around in session after the user posts their data. Below is an example Customer class:

    public class Customer
    {
        private string _VisualID;
        private string _FirstName;
        private string _MiddleName;
        private string _LastName;
        private string _Street1;
        private string _Street2;
        private string _City;
        private string _State;
        private string _Zip;
        private string _CountryCode;
        private string _Phone;
        private string _Email;
        private string _DOB;

        private string _IsPrimary;

        /// //////////////////////////////////////////////////////////////
        /// <summary>
        /// Constructor
        /// </summary>
        /// //////////////////////////////////////////////////////////////
        public Customer()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /*
        * //////////////////////////////////////////////////////////
        * Accessor / Mutator methods
        * //////////////////////////////////////////////////////////
        */

        public string VisualID
        {
            get
            {
                return _VisualID;
            }

            set
            {
                _VisualID = value;
            }
        }

        public string FirstName
        {
            get
            {
                return _FirstName;
            }

            set
            {
                _FirstName = value;
            }
        }

        public string MiddleName
        {
            get
            {
                return _MiddleName;
            }

            set
            {
                _MiddleName = value;
            }
        }

        public string LastName
        {
            get
            {
                return _LastName;
            }

            set
            {
                _LastName = value;
            }
        }

        public string Street1
        {
            get
            {
                return _Street1;
            }

            set
            {
                _Street1 = value;
            }
        }

        public string Street2
        {
            get
            {
                return _Street2;
            }

            set
            {
                _Street2 = value;
            }
        }

        public string City
        {
            get
            {
                return _City;
            }

            set
            {
                _City = value;
            }
        }

        public string State
        {
            get
            {
                return _State;
            }

            set
            {
                _State = value;
            }
        }

        public string Zip
        {
            get
            {
                return _Zip;
            }

            set
            {
                _Zip = value;
            }
        }

        public string CountryCode
        {
            get
            {
                return _CountryCode;
            }

            set
            {
                _CountryCode = value;
            }
        }

        public string Phone
        {
            get
            {
                return _Phone;
            }

            set
            {
                _Phone = value;
            }
        }

        public string Email
        {
            get
            {
                return _Email;
            }

            set
            {
                _Email = value;
            }
        }

        public string DOB
        {
            get
            {
                return _DOB;
            }

            set
            {
                _DOB = value;
            }
        }

        public string IsPrimary
        {
            get
            {
                return _IsPrimary;
            }

            set
            {
                _IsPrimary = value;
            }
        }

  • Re: Need opinion / advice on an attempt at strong typing DAL using struct

    01-03-2007, 6:10 PM
    • Loading...
    • ngenius
    • Joined on 12-19-2006, 2:33 AM
    • Posts 8

    Datakix,

     Thanks for the example and code. Based on the URL you mentioned this is what I extracted from it that I had also read in teh MS Help Files for C# Express edition in that a struct is by value a class by reference.

    from the link you sent:
    "Also, consider that a struct incurs less overhead than a class because, being a value type, it is stored on the stack rather than how a class is stored, on the heap."
     

     I have two books that use different means to accomlish wrapping this type of logic,

    use of classes as a wrapper extensively: ASP.NET 2.0 Website Programming by WROX

    and

    use of structs as a wrapper: Beginning ASP.NET 2.0  E-Commerce in C# 2005 by Apress 

     I always liked Apress better than WROX.
     


     

  • Re: Need opinion / advice on an attempt at strong typing DAL using struct

    02-07-2007, 1:41 PM
    • Loading...
    • cmcbride
    • Joined on 02-07-2007, 1:29 PM
    • Posts 1

    When you use a lightweight class like this, where does the business logic lie?

    In my standard way of doing things, I would inherit your class from a DataItem class that provides a Save() and Load() method that call pure virtual StoreItem and LoadItemFromDataStore. Then in Customer, StoreItem would call into my DAL with:

    dataLayerClass.StoreCustomer(name, address1, etc...) or dataLayerClass.LoadCustomer(id)

    Thus, my classes are slightly "heavier" than yours but it seems like a clean separation of the data layer from the presentation. If you take the lightweight class and do a save you would have to do:

     dataLayerClass.StoreCustomer(Customer c)

    Doesn't that violate some rule, somewhere!? LOL!

    Finally, what exactly is too "heavy-weight" for code behind? If you move the business logic into a business class, but it's now too heavy, don't you have to call that logic in the .StoreCustomer method to validate data, etc... Now .StoreCustomer is being called from presentation and it's heavy instead...

     Thanks for clarifying.
     

     

     

     


     

     

    datakix:

    I wouldn't worry about 2 users concurrently submitting the form at the same time and getting mixed up - the .NET Framework along w/the ASP.NET SessionID will handle that for you. However, you may want to avoid using a 'struct' datatype in that fashion as it handcuffs your extensibility.

    See: http://www.csharp-station.com/Tutorials/Lesson12.aspx

    In general, when you collect user data you don't want to mix the presentation layer code and the business logic code. Moreover, you don't want to complicate the presentation layer with complex data types as it does add some overhead. Unless there is a real business need, I'd recommend keeping it simple and just populating a class you carry around in session after the user posts their data. Below is an example Customer class:

    public class Customer
    {
        private string _VisualID;
        private string _FirstName;
        private string _MiddleName;
        private string _LastName;
        private string _Street1;
        private string _Street2;
        private string _City;
        private string _State;
        private string _Zip;
        private string _CountryCode;
        private string _Phone;
        private string _Email;
        private string _DOB;

        private string _IsPrimary;

        /// //////////////////////////////////////////////////////////////
        /// <summary>
        /// Constructor
        /// </summary>
        /// //////////////////////////////////////////////////////////////
        public Customer()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        /*
        * //////////////////////////////////////////////////////////
        * Accessor / Mutator methods
        * //////////////////////////////////////////////////////////
        */

        public string VisualID
        {
            get
            {
                return _VisualID;
            }

            set
            {
                _VisualID = value;
            }
        }

        public string FirstName
        {
            get
            {
                return _FirstName;
            }

            set
            {
                _FirstName = value;
            }
        }

        public string MiddleName
        {
            get
            {
                return _MiddleName;
            }

            set
            {
                _MiddleName = value;
            }
        }

        public string LastName
        {
            get
            {
                return _LastName;
            }

            set
            {
                _LastName = value;
            }
        }

        public string Street1
        {
            get
            {
                return _Street1;
            }

            set
            {
                _Street1 = value;
            }
        }

        public string Street2
        {
            get
            {
                return _Street2;
            }

            set
            {
                _Street2 = value;
            }
        }

        public string City
        {
            get
            {
                return _City;
            }

            set
            {
                _City = value;
            }
        }

        public string State
        {
            get
            {
                return _State;
            }

            set
            {
                _State = value;
            }
        }

        public string Zip
        {
            get
            {
                return _Zip;
            }

            set
            {
                _Zip = value;
            }
        }

        public string CountryCode
        {
            get
            {
                return _CountryCode;
            }

            set
            {
                _CountryCode = value;
            }
        }

        public string Phone
        {
            get
            {
                return _Phone;
            }

            set
            {
                _Phone = value;
            }
        }

        public string Email
        {
            get
            {
                return _Email;
            }

            set
            {
                _Email = value;
            }
        }

        public string DOB
        {
            get
            {
                return _DOB;
            }

            set
            {
                _DOB = value;
            }
        }

        public string IsPrimary
        {
            get
            {
                return _IsPrimary;
            }

            set
            {
                _IsPrimary = value;
            }
        }

Page 1 of 1 (4 items)
Microsoft Communities
Page view counter