Parent Class for Business Layer

Parent Class for Business Layer

Old forum URL: forums.lhotka.net/forums/t/6126.aspx


bpb2221 posted on Monday, January 12, 2009

I want to be able to call my Data Layer from my CSLA business layer.  My data layer classes are passed business objects instead of values.  I cannot create an instance of my business class inside of itself to pass to the data layer (maybe I just don't know how).  How can I create an instance of my business class and pass it to the data layer, without calling the data layer directly from the UI?    Someone suggested that I use a parent class for all of my business classes. Does anyone have any experience with this issue.

Here is how I am doing it now on the form

 Private Sub btn_Save_Click _
    (ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles btn_Save.Click
        Try

                      'new instance of DAL
           Dim auditRecord As New DataLayer.AuditBusinessObject()
          
            'Save or Update

            RebindUI(True, True)
           
             'Add Audit Record

             auditRecord.AddHistoryRecord(mBusinessObject)

            'Set rules to form

            ApplyAuthorizationRules()
        Catch ex As Exception
        'Run some exception handling
       
End Try
 End Sub

The auditRecord.AddHistoryRecord is actually inside of the RebindUI. Obviously the record has to be updated before I add a history record.

RockfordLhotka replied on Monday, January 12, 2009

As a general rule the UI should never interact directly with the DAL. Doing that will prevent you from ever using the data portal, which means you can't switch to an n-tier model.

In other words, you can call the DAL from the UI, but you'll block the use of one entire feature area of CSLA. If you are OK with losing those features, then have at it :)

But if you want to stick with the supported CSLA architectural model, you have two options (in version 3.6):

  1. Call the DAL from your DataPortal_XYZ methods, keeping in mind that DataPortal_Fetch() is actually running inside an instance of your object, so you can always pass Me (this in C#) to your DAL as a parameter
  2. Use the new ObjectFactory feature to have the data portal invoke an object factory object that is responsible for coordinating the interaction between the data portal, your DAL and your business objects

These options are discussed in Chapter 18 of the Expert 2008 Business Objects book.

SonOfPirate replied on Monday, January 12, 2009

The other thing I would caution you about is that it sounds like you are designing around your data and not behavior.  I've seen the type of architecture you are describing before and it is typically some sort of entity model where AuditBusinessObject is simply a data transfer object (DTO) containing properties that match the underlying data source.  While this is fine, AuditBusinessObject is not a CSLA business object in this case.

As Rocky says, with CSLA, your business objects purposely sit between your UI and DAL.  You can still use the factory approach to instantiate your business objects but how they interact with the DAL is hidden internal to the business object (or externalized using an ObjectFactory as Rocky explains).

Definitely take some time with the book and ProjectTracker sample application.  I think they will answer your questions.

Copyright (c) Marimer LLC