locked
Data Tables vs Business Objects ? RRS feed

  • Question

  • Hello,

    Your kind attention and expert advice is required please, here I want to disucss using Data Tables instead of Business objects for passing data between layers and to bind controls (I am not saying DataSet vs BusinessObjects rather DataTables vs BusinessObjets)

    In my architechture I have (1) User Interface, (2) a project containing Custom Types or business objects, (3) a business logic layer and (4) data access layer. Further more I use stored procedure in database.

    As a developer of course my privorities are code reuseability, fast development and quality of work. I have felt that using business object saves development time in the following ways.

    1. It allows immplementing business logic through properties

    2. They are easy to create in VS 2008

    3. They save time of passing seprate parameters to functions and reduce errors that can raised by passing a lot of  parameters individually.

    But antoher fact I observed is that using Business Objects for filling data from DataReader waste some time as well. Suppose my stored procedure is returing 20 columns. I will have to catch and fill every column individually but if I just start using DataTable just writing one line of code will save me from writing 20 lines of code. I am in team as senior and I felt every one waste some time doing this.

     

    If as a developer we value code reuseability, fast development and quality of work what you say about using DataTable instead of using Business Objects. Another tpositive thing is that in DataTable there are some built in featurs that we may have to do by overself if we go for buliness objects. Using DataTable is also less bulky than Data Set and  speedy than Data Adapter.

    Please advice me how you see using business object for implemnting logic, busines rules and passing arguments and use data table (typed or may be untyped) to pass data taken from db to different layers and to bind controls. I am using Enter Prize Library DAAB for database communication.

    Please also guide can we use Data Table well with Linq/ Silver Light / WCF and other technologies ? Is there any loss that I will have to bear If I stop using business objects now ?

    Thanks in anticipation for your attention, advice and precious time.

    Haansi

     

     


    <input id="gwProxy" type="hidden"></input> <input id="jsProxy" onclick="jsCall();" type="hidden" />
    Monday, February 22, 2010 7:03 AM

Answers

  • Hi,

    You can use DataTables in Linq, WCF as well. Silverlight I have no idea. I always appreciate using Business Entities in enterprise applications.

    Instead of speaking things in DataSet and DataTables it looks good speaking a customer as customer. What I understand your problem is the time taken in filling the business entities from DataReader or other ADO.NET source.

    I think you haven't checked the Linq to Sql. There are ways you can directly map business entities to sql tables that avoids of filling the entities from a DataReader or other.

    Regards
    Dnana
    • Marked as answer by Haansi Thursday, February 25, 2010 8:02 AM
    Wednesday, February 24, 2010 12:53 PM
  • Hi Haansi,

    In one of my old project. We have used DLinq. but I'm not familiar with it. The architecture consists of Business Entities, UI, Service and Data Access Layers. The DLinq acts as the core in DAL

    We used stored procedures that are directly called by DLinq and filled the business entities without the additional work that involved.

    Most of the samples available in the internet shows tables that are directly dragged and dropped to the designer and querying them. But I don't have interest in that, still I wonder how my previous team members have done that using the custom business entities and filling by DLinq without that overhead. You have to analyze that.

    Regards
    Dnana


    • Marked as answer by Haansi Saturday, February 27, 2010 8:38 AM
    Thursday, February 25, 2010 10:43 AM
  • May I also suggest Entity Framework as opposed to Linq to SQL, it seems that EF is the future as far as ORMs go on the MS side.  You can create data access objects like you were probably doing before, the only difference is now you're not using ADO.NET (connection, command, datareader, etc.) you're using LINQ to query an EDM (entity data model).
    • Marked as answer by Haansi Saturday, February 27, 2010 6:27 AM
    Thursday, February 25, 2010 4:25 PM
  • Hi Haansi,

    Some of your questions have answers from my side.

    Q1. Can Linq fill List of objects ? Like in general we create objects and fill there list for data access.

            In my old project we filled the business entities using DLinq but I don't have idea about this.

    Q2. Can we use Linq with DAAB ? If we do this any advantage or disadvantages ?

          No Idea.

    Q3. In examples I got Linq returns var. Can It return other data structures ? Like array or list again ?

          You can convert a linq query easily into a list or array. There are methods called ToList(), ToArray(). By using this from DAL layer you can return as an array of objects.

    Q4. Using Linq what will be return type of DAL functions ?

          You can execute stored procedures directly from the linq. So if a SPROC is returning an integer value say the no. of rows affected that can be returned from Linq.

    Q5. After LInq dont we use business types ? 

           Still we can use our Custom Business Objects.

    Q6. What are typical layers (Architecture) for Linq projects.

           Linq comes every where but the one we are discussing here DLinq that comes in the DAL. In the DAL layer you have to do the modifications if you are planning to use Linq.

    Regards
    Dnana
    • Marked as answer by Haansi Wednesday, March 3, 2010 7:07 AM
    Monday, March 1, 2010 5:42 AM
  • Haansi,

    I personally don't get boxed into a "one or the other" approach and ultimately let my database structure dictate my business object development.  I agree that having a custom business entity's public properties exposed to the presentation tier is nice for development (as long as those properties are "logically" mapped to their corresponding db fields).  But I might also have DataTable(s) reside on that business entity as well!

    For example:  I might have a "locations" business entity that represents a land parcel/property in a city.  Now as long as that single parcel/property represents ONE single property owner then there really isn't an issue.  BUT, what if that parcel/property entity is actually a Condominium that represents many property owners?  In this instance I will maintain not just the one owner that fills the public properties of that entity, but also include a DataTable that has the many owners that are related to that one parcel.  This also allows me to "do stuff" to that DataTable of property/parcel owners as well.

    I haven't messed around much with LINQ yet, I'm still building my own business entities from scratch.  But I still let the Database dictate the structure of those entities.


    jfc
    • Marked as answer by Haansi Wednesday, March 3, 2010 7:06 AM
    Wednesday, March 3, 2010 2:47 AM

All replies

  • Hi,

    You can use DataTables in Linq, WCF as well. Silverlight I have no idea. I always appreciate using Business Entities in enterprise applications.

    Instead of speaking things in DataSet and DataTables it looks good speaking a customer as customer. What I understand your problem is the time taken in filling the business entities from DataReader or other ADO.NET source.

    I think you haven't checked the Linq to Sql. There are ways you can directly map business entities to sql tables that avoids of filling the entities from a DataReader or other.

    Regards
    Dnana
    • Marked as answer by Haansi Thursday, February 25, 2010 8:02 AM
    Wednesday, February 24, 2010 12:53 PM
  • Hi,

    thanks for guiding me and you really got my issue, you are right I have not checked Linq to SQL. I will check it as well.

    I have another relevant query here. It is that what will architecture if I use Linq to SQL ? Currently I am using User Interface layer, business logic layer, data access layer and custom types. Where I will implement Linq in DAL ? or where

    Secondly please guide can we use Linq with any database ?

    thanks

    <input id="gwProxy" type="hidden"></input> <input id="jsProxy" onclick="jsCall();" type="hidden" />
    Thursday, February 25, 2010 8:02 AM
  • Hi Haansi,

    In one of my old project. We have used DLinq. but I'm not familiar with it. The architecture consists of Business Entities, UI, Service and Data Access Layers. The DLinq acts as the core in DAL

    We used stored procedures that are directly called by DLinq and filled the business entities without the additional work that involved.

    Most of the samples available in the internet shows tables that are directly dragged and dropped to the designer and querying them. But I don't have interest in that, still I wonder how my previous team members have done that using the custom business entities and filling by DLinq without that overhead. You have to analyze that.

    Regards
    Dnana


    • Marked as answer by Haansi Saturday, February 27, 2010 8:38 AM
    Thursday, February 25, 2010 10:43 AM
  • May I also suggest Entity Framework as opposed to Linq to SQL, it seems that EF is the future as far as ORMs go on the MS side.  You can create data access objects like you were probably doing before, the only difference is now you're not using ADO.NET (connection, command, datareader, etc.) you're using LINQ to query an EDM (entity data model).
    • Marked as answer by Haansi Saturday, February 27, 2010 6:27 AM
    Thursday, February 25, 2010 4:25 PM


  • I am new to Linq after your advice I searched about Linq, I was developing with Enterprise

    Library (DAAB) that is why I dont know even basics of Linq. Please guide me.

    Q1. Can Linq fill List of objects ? Like in general we create objects and fill there list for data access.


    Q2. Can we use Linq with DAAB ? If we do this any advantage or disadvantages ?

    Q3. In examples I got Linq returns var. Can It return other data structures ? Like array or list again ?

    Q4. Using Linq what will be return type of DAL functions ?

    Q5. After LInq dont we use business types ?

    Q6. What are tipical layers (Architecture) for Linq projects.


    please guide , thanks in anticipation

    haansi
    Saturday, February 27, 2010 8:37 AM
  • Hi Haansi,

    Some of your questions have answers from my side.

    Q1. Can Linq fill List of objects ? Like in general we create objects and fill there list for data access.

            In my old project we filled the business entities using DLinq but I don't have idea about this.

    Q2. Can we use Linq with DAAB ? If we do this any advantage or disadvantages ?

          No Idea.

    Q3. In examples I got Linq returns var. Can It return other data structures ? Like array or list again ?

          You can convert a linq query easily into a list or array. There are methods called ToList(), ToArray(). By using this from DAL layer you can return as an array of objects.

    Q4. Using Linq what will be return type of DAL functions ?

          You can execute stored procedures directly from the linq. So if a SPROC is returning an integer value say the no. of rows affected that can be returned from Linq.

    Q5. After LInq dont we use business types ? 

           Still we can use our Custom Business Objects.

    Q6. What are typical layers (Architecture) for Linq projects.

           Linq comes every where but the one we are discussing here DLinq that comes in the DAL. In the DAL layer you have to do the modifications if you are planning to use Linq.

    Regards
    Dnana
    • Marked as answer by Haansi Wednesday, March 3, 2010 7:07 AM
    Monday, March 1, 2010 5:42 AM
  • Haansi,

    I personally don't get boxed into a "one or the other" approach and ultimately let my database structure dictate my business object development.  I agree that having a custom business entity's public properties exposed to the presentation tier is nice for development (as long as those properties are "logically" mapped to their corresponding db fields).  But I might also have DataTable(s) reside on that business entity as well!

    For example:  I might have a "locations" business entity that represents a land parcel/property in a city.  Now as long as that single parcel/property represents ONE single property owner then there really isn't an issue.  BUT, what if that parcel/property entity is actually a Condominium that represents many property owners?  In this instance I will maintain not just the one owner that fills the public properties of that entity, but also include a DataTable that has the many owners that are related to that one parcel.  This also allows me to "do stuff" to that DataTable of property/parcel owners as well.

    I haven't messed around much with LINQ yet, I'm still building my own business entities from scratch.  But I still let the Database dictate the structure of those entities.


    jfc
    • Marked as answer by Haansi Wednesday, March 3, 2010 7:06 AM
    Wednesday, March 3, 2010 2:47 AM
  • Hi Haansi,

    After playing with the code I got how you can fill the custom business entities from database without iterating through the DataReader or DataSet.

    1. I have created a business entity called Agency that maps exactly with a table in my sql server. Below is the entity I have created and done some modifications to match it with the sql table.

    public partial class Agency
        {
            private int _AgencyID;
    
            private string _AgencyName;
    
            private string _email;
    
            private System.Nullable<int> _CreatedByUserId;
    
            private System.Nullable<System.DateTime> _CreatedDate;
    
            private System.Nullable<int> _UpdatedByUserId;
    
            private System.Nullable<System.DateTime> _UpdatedDate;
    
            public Agency()
            {
            }
    
            public int AgencyID
            {
                get
                {
                    return this._AgencyID;
                }
                set
                {
                    if ((this._AgencyID != value))
                    {
                        this._AgencyID = value;
                    }
                }
            }
    
            public string AgencyName
            {
                get
                {
                    return this._AgencyName;
                }
                set
                {
                    if ((this._AgencyName != value))
                    {
                        this._AgencyName = value;
                    }
                }
            }
    
            public string email
            {
                get
                {
                    return this._email;
                }
                set
                {
                    if ((this._email != value))
                    {
                        this._email = value;
                    }
                }
            }
    
            public System.Nullable<int> CreatedByUserId
            {
                get
                {
                    return this._CreatedByUserId;
                }
                set
                {
                    if ((this._CreatedByUserId != value))
                    {
                        this._CreatedByUserId = value;
                    }
                }
            }
    
            public System.Nullable<System.DateTime> CreatedDate
            {
                get
                {
                    return this._CreatedDate;
                }
                set
                {
                    if ((this._CreatedDate != value))
                    {
                        this._CreatedDate = value;
                    }
                }
            }
    
            public System.Nullable<int> UpdatedByUserId
            {
                get
                {
                    return this._UpdatedByUserId;
                }
                set
                {
                    if ((this._UpdatedByUserId != value))
                    {
                        this._UpdatedByUserId = value;
                    }
                }
            }
    
            public System.Nullable<System.DateTime> UpdatedDate
            {
                get
                {
                    return this._UpdatedDate;
                }
                set
                {
                    if ((this._UpdatedDate != value))
                    {
                        this._UpdatedDate = value;
                    }
                }
            }
        }

    2. I have created a DataHelper class.

    namespace LearnDlinq
    {
        using System.Data.Linq;
        using System.Data.Linq.Mapping;
        using System.Reflection;
    
        public partial class StudentClassesDataContext : System.Data.Linq.DataContext
        {
    
            private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
    
    
            public StudentClassesDataContext() :
                base(global::LearnDlinq.Properties.Settings.Default.StudentDB_IT2_ST4_DevConnectionString, mappingSource)
            {
    
            }
    
            [Function(Name = "dbo.p_LoadAllAgency")]
            public ISingleResult<Agency> LoadAgencies()
            {
                IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
                return ((ISingleResult<Agency>)(result.ReturnValue));
            }
        }
    }
    In the helper class you see I have created a Context class that inherits DataContext. The class has a method called LoadAgencies() that maps with a stored procedure called dbo.p_LoadAllAgency. See in the generic parameter I'm just passing the business entity I have created.

    3. I have set the connection string settings in my config file.

        <connectionStrings>
            <add name="LearnDlinq.Properties.Settings.StudentDB_IT2_ST4_DevConnectionString"
                connectionString="Data Source=MyServer;Initial Catalog=MyDataBase;User ID=sa;Password=pass"
                providerName="System.Data.SqlClient" />
        </connectionStrings>
    4. See how I'm getting all the agencies in the Program.cs file of my console application

        class Program
        {
            static void Main(string[] args)
            {
                using (StudentClassesDataContext context = new StudentClassesDataContext())
                {
                    List<Agency> agencies = context.LoadAgencies().ToList();
                }
            }
        }
    That's all. Very simple right. The important thing to note down is the method LoadAgencies() having two lines just does what we are desiring for a long time.

    Regards
    Dnana
    Wednesday, March 3, 2010 12:52 PM
  • EF is still quite new compared to NHibernate. I'm also new to ORM and would rather try NHibernate and wait for EF to mature and improve in performance. As a personal preference, i value performance and scalability > speed of development. DataSets & DataTables are notoriously huge objects and might impede performance as well as overusing Reflection.. my 2c
    Never stop learning.
    Wednesday, March 10, 2010 1:32 PM