DAL or BLL? How to implement objects

Discussion in 'ASP .Net' started by Nemisis, Oct 3, 2006.

  1. Nemisis

    Nemisis Guest

    Hi everyone,

    i am creating my own DAL and BLL, and i am not using typed datasets.
    My problem is that in my DAL i have a Save method whos signiture looks
    something like:


    Save(ID as integer, name as string, .......


    Within my BLL, i call then same method (the business layer version)
    something like this


    Save (pCompany as company)


    The latter of the two seems better to me as u dont have to write out
    all the variables, and a problem could occur if someone passed in a
    value of the wrong type.


    I think it would be alot better to have both the DAL and BLL signiture
    look like


    Save (pCompany as company)


    If i wanted to do this, were would i store the business objects? The
    DAL?? The BLL?? Or do i even create a separate class library that
    both the DAL and BLL must reference??


    Also, if i do this, i will have to change my DAL to return objects,
    instead of returning DataRows, DataTables and DataSets, like it does
    at the moment, is this right? i am not merging the two by accident am
    i??


    I do not want to using any of the microsoft code that you can download,

    but if someone could please let me know about the above, i would be
    very greatful.


    Cheers
     
    Nemisis, Oct 3, 2006
    #1
    1. Advertising

  2. Nemisis

    Mikeon Guest

    > If i wanted to do this, were would i store the business objects? The
    > DAL?? The BLL?? Or do i even create a separate class library that
    > both the DAL and BLL must reference??


    If you want to return business objects from DAL you have to reference
    them from DAL and since you want also to be able to reference DAL from
    BLL you get a circular reference.
    What you need is a some kind of Data Transfer Object which you will use
    to move the data between BLL and DAL. There are few possible choices
    here. Either you use what is provided to you by Microsoft in a form of
    DataSet/DataTable/Something which are not strongly typed and do not
    hide the underlying database structure from you. The other choice is
    creating your own - strongly typed DTO's and package the data inside
    such an object before moving between layers.
    The DTO should of course be defined in a separate project as the
    DataSet is.

    Hope this helps a little.

    --
    Michal
    http://vaultofthoughts.net
     
    Mikeon, Oct 3, 2006
    #2
    1. Advertising

  3. Nemisis

    Mikeon Guest

    > The DTO should of course be defined in a separate project as the
    > DataSet is.


    I should have mentioned that using custom DTOs means a lot of code that
    is required to populate and read data to and from such objects.

    --
    Michal
    http://vaultofthoughts.net
     
    Mikeon, Oct 3, 2006
    #3
  4. Nemisis

    sloan Guest

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/BOAGag.asp
    Find the heading labeled
    Deploying Business Entities
    (about 2/3's the way down)

    by defining custom entity classes in a common assembly to be deployed on
    multiple tiers.

    There needs to be an assembly that lives outside of the tiers.

    This is where you'd but your Company object.


    Also see my blog at:
    http://sholliday.spaces.live.com/?_...ogview&_c=blogpart&partqs=amonth=5&ayear=2006

    I have a downloadable example. I chose the #1 (in the "Deploying Business
    Entities" section)

    The entire microsoft article (above url) is good to read.





    "Nemisis" <> wrote in message
    news:...
    > Hi everyone,
    >
    > i am creating my own DAL and BLL, and i am not using typed datasets.
    > My problem is that in my DAL i have a Save method whos signiture looks
    > something like:
    >
    >
    > Save(ID as integer, name as string, .......
    >
    >
    > Within my BLL, i call then same method (the business layer version)
    > something like this
    >
    >
    > Save (pCompany as company)
    >
    >
    > The latter of the two seems better to me as u dont have to write out
    > all the variables, and a problem could occur if someone passed in a
    > value of the wrong type.
    >
    >
    > I think it would be alot better to have both the DAL and BLL signiture
    > look like
    >
    >
    > Save (pCompany as company)
    >
    >
    > If i wanted to do this, were would i store the business objects? The
    > DAL?? The BLL?? Or do i even create a separate class library that
    > both the DAL and BLL must reference??
    >
    >
    > Also, if i do this, i will have to change my DAL to return objects,
    > instead of returning DataRows, DataTables and DataSets, like it does
    > at the moment, is this right? i am not merging the two by accident am
    > i??
    >
    >
    > I do not want to using any of the microsoft code that you can download,
    >
    > but if someone could please let me know about the above, i would be
    > very greatful.
    >
    >
    > Cheers
    >
     
    sloan, Oct 3, 2006
    #4
  5. Nemisis

    Nemisis Guest

    Thanks for the comments so far, I have tried to download the code on
    your blog, but i get an error when i try to extract the file?? Says
    File Not Found. I would like to see your SQLHelper class, can you
    explain this a lil more for me??

    This may sound a lil stupid but i guess a DTO is a Data Transfer
    Object? And what does this do exactly? Or is this what is ment when
    you store your business objects ina separate project?

    Sorry if the questions sound a bit ..... well stupid, :)

    Thanks again
     
    Nemisis, Oct 3, 2006
    #5
  6. Nemisis

    Nemisis Guest

    Thanks for the comments so far, I have tried to download the code on
    your blog, but i get an error when i try to extract the file?? Says
    File Not Found. I would like to see your SQLHelper class, can you
    explain this a lil more for me??

    This may sound a lil stupid but i guess a DTO is a Data Transfer
    Object? And what does this do exactly? Does it populate the business
    object with data from a dataset/datareader, or is this what is ment
    when you store your business objects ina separate project?

    Sorry if the questions sound a bit ..... well stupid, :)

    Thanks again
     
    Nemisis, Oct 3, 2006
    #6
  7. Nemisis

    sloan Guest

    I just redownloaded it. Thru IE and Firefox. Both worked.

    Make sure you download it local ~then open it up in WinZip.

    SQLHelper is just the
    Data Access Application Block v 2.0 (specific to Sql Server )
    http://aspnet.4guysfromrolla.com/articles/062503-1.aspx
    That's like 3 year old stuff.

    Now, the flavor is the EnterpriseLibrary application blocks.

    SQLHelper just a helper , that encapsulates common data access logic. it is
    not the "Assembly living outside the Tiers"

    The
    Data.DataSets project/assembly is (my) assembly living outside of the other
    Tiers.


    ................






    "Nemisis" <> wrote in message
    news:...
    > Thanks for the comments so far, I have tried to download the code on
    > your blog, but i get an error when i try to extract the file?? Says
    > File Not Found. I would like to see your SQLHelper class, can you
    > explain this a lil more for me??
    >
    > This may sound a lil stupid but i guess a DTO is a Data Transfer
    > Object? And what does this do exactly? Or is this what is ment when
    > you store your business objects ina separate project?
    >
    > Sorry if the questions sound a bit ..... well stupid, :)
    >
    > Thanks again
    >
     
    sloan, Oct 3, 2006
    #7
  8. Nemisis

    Nemisis Guest

    I have downloaded the code again, but it says that some of the files
    are password protected??

    I have had a look at the other code, and it should be bale to answer a
    few questions i have had, regarding how to populate your data with
    relationships etc.

    I will give this ago, and let you know if i need your EXPERTISE again.
    lol

    Thanks again
     
    Nemisis, Oct 3, 2006
    #8
  9. Nemisis

    Nemisis Guest

    Sloan,

    Thanks again for the examples, this is my first real OOP project so
    things are changing rapidly, but for the better, i think.

    I am going to take your advice on creating a separate class library to
    hold my business objects.

    One thing i am not sure about, is that, for the DAL, the object only
    needs a certain amount of fields, as these are the fields that will be
    saved in the database, but in my business layer the object may need
    some additional fields and properties. Should i put all the fields and
    properties into the class object, or would it be better practice to
    create a separate library of class objects, and then in my business
    layer, i could create another company object that inherits from the
    separate class object?? Or should the separate library be a library of
    interface objects??
     
    Nemisis, Oct 4, 2006
    #9
  10. Nemisis

    Mikeon Guest

    > by defining custom entity classes in a common assembly to be deployed on
    > multiple tiers.


    In my opinion Business Entities should live only in the business layer.
    Pushing those entities between layers has some major disadvantages. For
    example suppose you would like to have some kind of lazy load on your
    properties. If we had a simple n-tier layout where BLL is right above
    the DAL we can call DAL methods directly from BLL to load our lazy
    loaded properties. Such as a ClassRoom object with GetStudents method.
    How would you implement this in a lazy load manner if you have no
    referrence (direct or indirect) to DAL?
    More problems appear when you try to return your entities from web
    service, where you can only return data and not behavior.
    And speaking of behavior, the most important thing about business
    entities is the behavior they encapsulate I do not think it would be
    possible to move the behavior between layers in one entity and still
    keep the application logically consistent.
    Take for example a Teacher object with a GradeStudents method, what
    would you need this method for in a DAL?

    Of course I'm not saying the approach is bad. I'm only trying to show
    that this is a very complex issue and MS articles are not always the
    best source - they are DataSet/DataTable lovers after all ;-)

    --
    Michal
    http://vaultofthoughts.net
     
    Mikeon, Oct 4, 2006
    #10
  11. Nemisis

    Nemisis Guest

    Mikeon,

    Thanks for your comments, but can you explain it a lil more, i am not
    sure if i completely understand?

    Am i right in thinking, that i should create a separate library of
    interface objects then?? Then reference these interfaces in my DAL and
    BLL??
     
    Nemisis, Oct 4, 2006
    #11
  12. Nemisis

    Mikeon Guest

    > Thanks for your comments, but can you explain it a lil more, i am not
    > sure if i completely understand?
    >
    > Am i right in thinking, that i should create a separate library of
    > interface objects then?? Then reference these interfaces in my DAL and
    > BLL??


    Take a look at my blog where I describe the idea and there is also a
    sample solution there:

    http://vaultofthoughts.net/MovingDataBetweenTiers.aspx

    --
    Michal
    http://vaultofthoughts.net
     
    Mikeon, Oct 4, 2006
    #12
  13. Nemisis

    Nemisis Guest

    Mikeon,

    Thanks for that, your article and code makes alot of sense, and i am
    confident now that i am doing the right thing, in creating the DTO.

    One other question, if you dont mind, if i create the DTO object in the
    separate library, and then reference the DTO library in my BLL, is it
    ok if the BLL object inherits the properties etc of the DTO object??
    This would save typing most data types etc twice? Is this not a good
    idea for a reason i havent thought of??

    Thanks again
     
    Nemisis, Oct 5, 2006
    #13
  14. Nemisis

    Nemisis Guest

    Mikeon,

    One thing you dont show in your example, is the retrieval of data via
    the DTO? I would be interested in seeing how you do that.
     
    Nemisis, Oct 5, 2006
    #14
  15. Nemisis

    Nemisis Guest

    Mikeon,

    I didnt mean retreval sorry, i ment how do you return a collection of
    objects from the DAL when your collections are defined in BLL??
     
    Nemisis, Oct 5, 2006
    #15
  16. Nemisis

    Mikeon Guest

    > One other question, if you dont mind, if i create the DTO object in the
    > separate library, and then reference the DTO library in my BLL, is it
    > ok if the BLL object inherits the properties etc of the DTO object??
    > This would save typing most data types etc twice? Is this not a good
    > idea for a reason i havent thought of??



    When doing OOP always try to think in terms of what is logical first
    and not what would help you write less code.
    So in this case when you take into consideration that inheritance is a
    "is a" relationship which means that if type B inherits from A then B
    is a type of A like in BiologyTeacher is a Teacher. Does Teacher is a
    kind of DTOTeacher? I think not and so I would preffer more code over
    the non-logical design.
    More over there is also a good advice to favor composition over
    inheritance, which means that you can store the DTO object inside your
    BLL object and just expose some properties which will use the DTO as a
    data store. This way you will save some in terms of code needed for
    constructing and moving the DTO's.
    Additionally inheriting from DTO would close all other possibilities of
    inheritance due to the single inheritance constraint.
    But of course it is you how will decide. I can only give you some
    theory to base your decision on.
    --
    Michal
     
    Mikeon, Oct 6, 2006
    #16
  17. Nemisis

    Mikeon Guest

    > I didnt mean retreval sorry, i ment how do you return a collection of
    > objects from the DAL when your collections are defined in BLL??


    I don't know if I understand, but if you have a group of teachers to
    return then I would return an array of DTOTeacher, and then load a
    collection from this in BLL.
    Does this answer your question?

    --
    Michal
    http://vaultofthoughts.net
     
    Mikeon, Oct 6, 2006
    #17
  18. Nemisis

    Nemisis Guest

    Mikeon wrote:
    > > I didnt mean retreval sorry, i ment how do you return a collection of
    > > objects from the DAL when your collections are defined in BLL??

    >
    > I don't know if I understand, but if you have a group of teachers to
    > return then I would return an array of DTOTeacher, and then load a
    > collection from this in BLL.
    > Does this answer your question?
    >


    Mikeon,

    Thanks for the first reply, but on the second i mean about loading
    relationship collections.

    So if you have a Teacher and a teacher has many students how would you
    load that if i was loading in ALL teachers.

    Loading many teachers with many students? Does that make sense?
     
    Nemisis, Oct 6, 2006
    #18
  19. Nemisis

    Mikeon Guest

    > So if you have a Teacher and a teacher has many students how would you
    > load that if i was loading in ALL teachers.
    >
    > Loading many teachers with many students? Does that make sense?


    There are few possible solutions here. Either you do a lazy load for
    the sub-properties (Students) - i.e.: return a collection of teachers
    without students loaded and then in the Students property when it is
    accessed for a given teacher, load it from the database the same way as
    you would load a collection of all students, but this time do it only
    for the students for the current teacher.
    If you have a scenario where you know that you will be using each of
    the teacher and each of his students it would be not performance-wise
    to make a call to the database every time. In this case when returning
    a teacher I would also return his students as a field of the DTOTeacher
    class. I would try to get the whole result in the DAL as a single
    database query.
    But I possible and if it would hurt not performance I personally
    preffer the lazy load approach.

    --
    Michal
    http://vaultofthoughts.net
     
    Mikeon, Oct 9, 2006
    #19
  20. Nemisis

    Nemisis Guest

    Mikeon wrote:
    > There are few possible solutions here. Either you do a lazy load for
    > the sub-properties (Students) - i.e.: return a collection of teachers
    > without students loaded and then in the Students property when it is
    > accessed for a given teacher, load it from the database the same way as
    > you would load a collection of all students, but this time do it only
    > for the students for the current teacher.
    > If you have a scenario where you know that you will be using each of
    > the teacher and each of his students it would be not performance-wise
    > to make a call to the database every time. In this case when returning
    > a teacher I would also return his students as a field of the DTOTeacher
    > class. I would try to get the whole result in the DAL as a single
    > database query.
    > But I possible and if it would hurt not performance I personally
    > preffer the lazy load approach.
    >
    > --
    > Michal
    > http://vaultofthoughts.net


    I understand, i thnk the last approach would be better for us, but may
    have to have an option to load all the information as well.

    Since i would be loading data for multiple students, is it not good
    practice to define a collection of students within the DTO layer? I
    have made a collection class within my business layer for my student
    collections, it seems like duplicate code to define another collectin
    of students with the dto layer? Or is it better to just use a generic
    list for DTO types?
     
    Nemisis, Oct 9, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    1,356
    sloan
    Jun 15, 2006
  2. Roy

    BLL and DAL.

    Roy, Jun 15, 2006, in forum: ASP .Net
    Replies:
    10
    Views:
    17,615
    =?Utf-8?B?Q2hyaXM=?=
    Jun 16, 2006
  3. sloan
    Replies:
    0
    Views:
    431
    sloan
    Dec 5, 2006
  4. sloan
    Replies:
    1
    Views:
    756
    Mark Rae
    Dec 5, 2006
  5. =?Utf-8?B?TWF0dE0=?=

    DAL is physical mapping and BLL logical?

    =?Utf-8?B?TWF0dE0=?=, Dec 7, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    420
    sloan
    Dec 7, 2006
Loading...

Share This Page