locked
Custom classes vs dataset (Business logic layers) RRS feed

  • Question

  • User-1833623472 posted

     Hi All

    I have been playing around with creating DataSets and TableAdaptors to select, insert, update and delete records in an SQL DB. I have noted that you can also create custom classes which can do the same functions however there is a bit more coding involved.

    My question is why use custom classes in the app_code folder instead of using DataSets with TableAdaptors. Is there limitations when using DataSets with TableAdaptors created by wizardas instead of hard coding.

     Any responses will be appreciated.

    Thanks


     

    Wednesday, July 4, 2007 4:50 AM

Answers

  • User-1611549905 posted

    It depends mainly on how large your application is. DataSets and TableAdaptors are the "out of the box" solution for database access in the .net framework, and they work fairly well with the drag and drop way of working that you get in Visual Studio, especially for small, simple projects with small amounts of data.

    The problem with DataSets is that they tend to be pretty memory hungry. They also get a bit difficult to manage as your application gets larger. When you need more complex solutions, the initial effort that you put into getting custom classes up and running can pay off with shorter development cycles if you do it right. That's where O/R mappers such as NHibernate come into their own, since they cut out a lot of the SQL code for basic CRUD operations.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 4, 2007 8:32 AM
  • User-2018949055 posted

    Going with custom classes is certainly better in many ways. 

        i) easier to add logic

        ii) business validation is easier. This is tougher when using DataSet

        iii) if you are using DataSet,  even when the number of rows returned are small,   the whole dataset has to be costructed which is quite an overhead. Custom classes are more lightwieght

     MOreover, tools like nHibernate helps you to convert tables into custom classes, you can then easily modify them.

     

    However using Dataset has its own advantages

    i) easier for developer

    ii) in built methods for sorting etc...

    It depends on what you want..match it with your requirements

     

    visit this link http://www.informit.com/articles/article.asp?p=31099&rl=1  its the part 1 of a 5 part series on this topic. May be helpful

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 4, 2007 9:02 AM
  • User-1833623472 posted

    Hi

    I understand what you mean. I am just struggling to figure out how to actually use and actually write a custom class. I am new to asp.net but quite jacked with the old ASP.

    I need a tutorial that explains what custom classes are all about and what you can use them for. I am struggling to find one. 

    What are o/r mappers?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 4, 2007 2:15 PM
  • User-1833623472 posted

    Hi

    What are business validations. Are u talking about "business Logic layer" rules for setting user permissions. I am still new at asp.net. I am familiar with visual basic, vscript programming.

    Can you explain to me an example of what type of application would use custom classes.

    I would appreciate that.

    THanks 

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 4, 2007 2:22 PM
  • User197122282 posted

    Hi wstevens,

    Here are answers to your questions:

    1. I am just struggling to figure out how to actually use and actually write a custom class ???

    Reffer to the following link which will give you an idea of custom classes:
    http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416

    You can also have a look at Micrsoft Pet Shop 4.0 (Sample application) which is based on the same idea.

    Custom classes are simple classes with getters and setters. We can also include business logic into these classes. The idea is that we create objects of these classes and instead of passing simple attributes between layers, we pass in the entire objects. That makes our life easier.  The one downside of using such classes is that you have to code everything whereas with DataSet we simple fill them up with data.


    2. What are business validations ???

    Yes they are rules but not just for user permissions. Business related rules (if you have a balance < 250, you cannot draw any amout, if purchase > 2000 you get gift voucher) validates logic used to run the business which exists in almost every business.


    3. What are o/r mappers ???

    Object/Relational Mapping is used to store class objects in any relational databases. e.g. If you have a product information, typically it will pass from different layers such as UI (User Interface) >> BLL (Business Logic Layer) >> DAL (Data Access Layer). The DAL is responsible for persisting the information to the database. Once it gets the information, we typically write queries to store all the attributes in the database. This has to be repeated over and over for all DAL Operations. Such tasks become laborious for methods which have to store a large amount of attributes into the database.

    One clean (but hard to get working) solution is to use O/R Mapping. With O/R mapping inserting the data into the database may be as simple as Mapper.Insert (object). Similarly we can retrieve the same data into an object like Mapper.GetProduct (id). Now the mapper is responsible for the Create, Read, Update and Delete operations. As i said, they make life easy for you but they are a bit hard to get going with.

    Hope these have answered your questions, Regards...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2007 12:15 AM
  • User-2018949055 posted

    Using datasets, the validation most easy to do is string length and stuff like that in the xml that is created for the dataset.

    But, lets say you want to update a row only only if status column of that row contains "active"  or in any similar situations. Classifying these as business rules, in these cases, when you want to add this logic to your dataset, it becomes tough, because, the class that is generated by the Dataset is too big and could be difficult to understand and sometimes to add some logic like i mentioned before, you might have to subscribe to rowchanging event etc etc to do it.

     Its much easier if to add these logic if you are using Custom classes
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2007 1:56 AM

All replies

  • User-1611549905 posted

    It depends mainly on how large your application is. DataSets and TableAdaptors are the "out of the box" solution for database access in the .net framework, and they work fairly well with the drag and drop way of working that you get in Visual Studio, especially for small, simple projects with small amounts of data.

    The problem with DataSets is that they tend to be pretty memory hungry. They also get a bit difficult to manage as your application gets larger. When you need more complex solutions, the initial effort that you put into getting custom classes up and running can pay off with shorter development cycles if you do it right. That's where O/R mappers such as NHibernate come into their own, since they cut out a lot of the SQL code for basic CRUD operations.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 4, 2007 8:32 AM
  • User-2018949055 posted

    Going with custom classes is certainly better in many ways. 

        i) easier to add logic

        ii) business validation is easier. This is tougher when using DataSet

        iii) if you are using DataSet,  even when the number of rows returned are small,   the whole dataset has to be costructed which is quite an overhead. Custom classes are more lightwieght

     MOreover, tools like nHibernate helps you to convert tables into custom classes, you can then easily modify them.

     

    However using Dataset has its own advantages

    i) easier for developer

    ii) in built methods for sorting etc...

    It depends on what you want..match it with your requirements

     

    visit this link http://www.informit.com/articles/article.asp?p=31099&rl=1  its the part 1 of a 5 part series on this topic. May be helpful

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 4, 2007 9:02 AM
  • User-1833623472 posted

    Hi

    I understand what you mean. I am just struggling to figure out how to actually use and actually write a custom class. I am new to asp.net but quite jacked with the old ASP.

    I need a tutorial that explains what custom classes are all about and what you can use them for. I am struggling to find one. 

    What are o/r mappers?

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 4, 2007 2:15 PM
  • User-1833623472 posted

    Hi

    What are business validations. Are u talking about "business Logic layer" rules for setting user permissions. I am still new at asp.net. I am familiar with visual basic, vscript programming.

    Can you explain to me an example of what type of application would use custom classes.

    I would appreciate that.

    THanks 

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 4, 2007 2:22 PM
  • User197122282 posted

    Hi wstevens,

    Here are answers to your questions:

    1. I am just struggling to figure out how to actually use and actually write a custom class ???

    Reffer to the following link which will give you an idea of custom classes:
    http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=416

    You can also have a look at Micrsoft Pet Shop 4.0 (Sample application) which is based on the same idea.

    Custom classes are simple classes with getters and setters. We can also include business logic into these classes. The idea is that we create objects of these classes and instead of passing simple attributes between layers, we pass in the entire objects. That makes our life easier.  The one downside of using such classes is that you have to code everything whereas with DataSet we simple fill them up with data.


    2. What are business validations ???

    Yes they are rules but not just for user permissions. Business related rules (if you have a balance < 250, you cannot draw any amout, if purchase > 2000 you get gift voucher) validates logic used to run the business which exists in almost every business.


    3. What are o/r mappers ???

    Object/Relational Mapping is used to store class objects in any relational databases. e.g. If you have a product information, typically it will pass from different layers such as UI (User Interface) >> BLL (Business Logic Layer) >> DAL (Data Access Layer). The DAL is responsible for persisting the information to the database. Once it gets the information, we typically write queries to store all the attributes in the database. This has to be repeated over and over for all DAL Operations. Such tasks become laborious for methods which have to store a large amount of attributes into the database.

    One clean (but hard to get working) solution is to use O/R Mapping. With O/R mapping inserting the data into the database may be as simple as Mapper.Insert (object). Similarly we can retrieve the same data into an object like Mapper.GetProduct (id). Now the mapper is responsible for the Create, Read, Update and Delete operations. As i said, they make life easy for you but they are a bit hard to get going with.

    Hope these have answered your questions, Regards...

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2007 12:15 AM
  • User-2018949055 posted

    Using datasets, the validation most easy to do is string length and stuff like that in the xml that is created for the dataset.

    But, lets say you want to update a row only only if status column of that row contains "active"  or in any similar situations. Classifying these as business rules, in these cases, when you want to add this logic to your dataset, it becomes tough, because, the class that is generated by the Dataset is too big and could be difficult to understand and sometimes to add some logic like i mentioned before, you might have to subscribe to rowchanging event etc etc to do it.

     Its much easier if to add these logic if you are using Custom classes
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 5, 2007 1:56 AM
  • User-1548088970 posted

     I'm not sure how much this will help you, but I always think of Custom Classes (business objects) as objects that are easier to understand and have real business meaning. Datasets have to be managed on a table by table basis, whereas a custom object can constructed of N number of tables.

    For instance, if you have an Order Table and a Line Items table. You have two separate tables relationally, but object wise you have an Order object which contains a List of associated Line Items. Let your DAL handle the relational part.
     

    Thursday, July 5, 2007 2:05 PM
  • User1017907314 posted

    The typed datasets is bad, bad and bad! Don't use it!

    Use NHibernate or Active Record instead

    Another options may be: CodeSmith, MyGeneration or using the Enterprise DAAB

    Sunday, July 8, 2007 3:08 PM