views:

271

answers:

2

Is it possible to have a single implementation of business logic layer (based on CSLA) and consume it from different presentation technoligies like winform, silverlight etc. There exists CSLA for silverlight. Does that mean implementing and maintaining different BLL for different presentation technologies.

Any help would be appreciated.

A: 

This is very possible with CSLA. The main difference with using your BLL with different UI technologies is the use of the Begin & Callback mechanism of data portal calls in CSLA for Silverlight. If you use this mechanism for all your UI technologies, this would allow you to use the same BLL.

I would suggest that you use separate Factory objects for your BLL so you can decide when to use the BeginInvoke & Callback mechanism and your business objects would be untouched.

You could also create duplicate factory methods if you felt that was necessary. For example if you had a business object of Customer:

public class Customer : Csla.BusinessBase<Customer>
{
    private Customer() { }

    private void DataPortal_Fetch(SingleCriteria<Customer, int> criteria)
    { // populate here }

}

Then you could have a CustomerFactory object that contained both methods for Silverlight and the other UI technologies:

public static class CustomerFactory
{
    // Handle most UI needs
    public static Customer Fetch(int id)
    {
        return DataPortal.Fetch<Customer>(new SingleCriteria<Customer, int>(id));
    }

    // Handle Silverlight needs
    public static void Fetch(int id)
    {
        DataPortal.BeginFetch<Customer>(id, FetchCallback); 
    }

    private static FetchCallback(object sender, DataPortalResult<Customer> result)
    {
        // notify the UI of result.Object
    }
}

You could also get a little more clever and implement dependency injection via an IoC container to make the call to your factory methods a little more smart.

The bottom line is that your actual business objects will not change but your factory objects may have duplicate CRUD methods in certain circumstances.

Rocky actually has a sample (ProjectTracker) that implements a single BLL with multiple UI layers at his web site (listed under 'Samples' at http://www.lhotka.net/cslanet/Download.aspx)

Jamie Wright
Does the same mechanism needs to apply to the below methods...class Customer{ public static Customer Create() { return DataPortal.Create<Customer>(); } public static void Delete(Guid customerID) { DataPortal.Delete(new SingleCriteria<Customer, Guid>(customerID)); }}
Anand Patel
In you support:"The differences are primarily due to the asynchronous nature of Silverlight programming, and the more synchronous nature of traditional .NET programming. If you are willing to apply the same asynchronous designs to your .NET implementation you can achieve 99% or 100% code sharing between the two platforms." from http://www.silverlight-travel.com/blog/2008/11/29/csla-net-for-silverlight/
Anand Patel
@Anand - Yes, any factory method, this pattern would be applied to. That is a very accurate and great quote in your second answer.
Jamie Wright
+2  A: 

Hello,

This is very easy todo with the CodeSmith CSLA templates. We have a PetShop Silverlight MVVM and Web sample application located here that shows off the generated code.

Thanks -Blake Niemyjski (Author of the CodeSmith CSLA Templates)

Blake Niemyjski