StrataFrame Forum

Filling a BO from a BBS

http://forum.strataframe.net/Topic16155.aspx

By Bill Cunnien - 5/5/2008

I have created several custom BBS's for use in my DX report.  So far, all is going quite well.  One thing that would make my life a lot easier is being able to access my custom fill methods from the BO.  Here is an example of my code to fill a BO which fills the BBS which provides data for my report:

public partial class PackingListDS : BusinessBindingSource
{
   
public PackingListDS()
    {
        InitializeComponent();
       
BusinessObject = new PackingListBO();
    }

   
public PackingListDS(IContainer container)
    {
        container.Add(
this);
        InitializeComponent();
       
BusinessObject = new PackingListBO();
    }

   
public void Fill(int pPLIndex)
    {
       
BusinessObject.FillByPrimaryKey(pPLIndex);
    }
}

This works great!  But, what if I want to fill the BO with a different method than the base methods (MyBO.FillWithMyReallyCoolMethod())?  If I could somehow cast the BBSs BO as the BO of choice, then I could get to them.  How do I do this?

Thanks,
Bill
By Trent L. Taylor - 5/5/2008

Really, the best way would be to create your ReportBaseBBS class as a generic class that must be inherited and accepts the BOs that it will represent as generic inheritance definitions.  This would allow you to talk straight to the business objects in strong-typed code without having to CType the object.  Here is what the report BBS base class would look like:

Step 1 - Base Report BBS Class
This clas is the abstract class that all other report BBS classes would inherit from.  You'll see that the definition requires a primary business object to be specified (you can add all of the required BOs that you want to, but this is not necessary as other BBS classes could be exposed on the actual classes [i.e. MyCustomersBBS, MyOrdersBBS, etc.]).  But we know that we are going to need at least one business object for each BBS.  So you can see that the class defined a generic parm called TPrimaryBO (you can call this whatever you want).  This will require a BusinessLayer class with a constructor (that is what the new() is doing).

/// <summary>
/// Create the base class of the reporting engine
/// </summary>
/// <remarks></remarks>
public abstract class ReportBaseBBS<TPrimaryBO> : MicroFour.StrataFrame.Business.BusinessBindingSource where TPrimaryBO : BusinessLayer, new()
{

#region  Constructors

/// <summary>
/// Default Constructor
/// </summary>
/// <remarks></remarks>
public ReportBaseBBS()
{
    //-- Create a new instance of the primary business object that this class wraps
    this.BusinessObject = _PrimaryBO;
}

#endregion

#region  Declare Privates

private TPrimaryBO _PrimaryBO = new TPrimaryBO();

#endregion

#region  Public Properties

/// <summary>
/// Exposes the primary business object so that you can reference it in code
/// </summary>
/// <value></value>
/// <returns></returns>
/// <remarks></remarks>
public TPrimaryBO PrimaryBO
{
    get
    {
        return _PrimaryBO;
    }
}

#endregion

}

Step 2 - Creating a Customers BBS Class
The next step would be creating the custom class that inherits the ReportBaseBBS and specifies which business object to wrap.  In the below example, you can see that we are inheriting the ReportBaseBBS class and supplying the business object type that will be the primary BO in this class (TPrimaryBO).

/// <summary>
/// This class is a BBS wrapper for the customers business object type
/// </summary>
/// <remarks></remarks>
public class CustomersReportBBS : ReportBaseBBS<CustomersBO>
{

}

Step 3 - Now you can directly access anything on the Primary BO
You will see in the below code sample, that once the CustomersReportBBS class is created, you can directly access the wrapped BO now through the property that was created in the ReportBaseBBS class (PrimaryBO).

public class TestCode
{

/// <summary>
/// Just shows how to use the code
/// </summary>
/// <remarks></remarks>
public TestCode()
{
    CustomersReportBBS cust = new CustomersReportBBS();

    //-- You can now access every method, etc. strong-typed through code since it is exposed
    //   through the PrimaryBO property using the typed generic.
    cust.PrimaryBO.FillWithMyCoolMethod();
}

}

This would be the cleanest way to achieve what you are trying to do.  For those VB.NET developers, here is the VB code:

VB.NET - Step 1

''' <summary>
''' Create the base class of the reporting engine
''' </summary>
''' <remarks></remarks>
Public MustInherit Class ReportBaseBBS(Of TPrimaryBO As {BusinessLayer, New})
    Inherits MicroFour.StrataFrame.Business.BusinessBindingSource

#Region " Constructors "

    ''' <summary>
    ''' Default Constructor
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        '-- Create a new instance of the primary business object that this class wraps
        Me.BusinessObject = _PrimaryBO
    End Sub

#End Region

#Region " Declare Privates "

    Private _PrimaryBO As New TPrimaryBO()

#End Region

#Region " Public Properties "

    ''' <summary>
    ''' Exposes the primary business object so that you can reference it in code
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property PrimaryBO() As TPrimaryBO
        Get
            Return _PrimaryBO
        End Get
    End Property

#End Region

End Class

VB.NET - Step 2

''' <summary>
''' This class is a BBS wrapper for the customers business object type
''' </summary>
''' <remarks></remarks>
Public Class CustomersReportBBS
    Inherits ReportBaseBBS(Of CustomersBO)

End Class

VB.NET - Step 3

Public Class TestCode

''' <summary>
''' Just shows how to use the code
''' </summary>
''' <remarks></remarks>
Public Sub New()
Dim cust As New CustomersReportBBS()

'-- You can now access every method, etc. strong-typed through code since it is exposed
'   through the PrimaryBO property using the typed generic.
cust.PrimaryBO.FillWithMyCoolMethod()
End Sub

End Class

By Bill Cunnien - 5/5/2008

Totally Awesome! w00t w00t w00t

(bows)

By Trent L. Taylor - 5/5/2008

LOL...thanks...hope it helps Smile