https://www.google.com/accounts/o8/id

https://www.google.com/accounts/o8/id

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


maxg13 posted on Monday, June 25, 2012

I would like to implement Factory Pattern in CSLA. I can use an abstract base class or an interface for the abstraction. I have decided to use an abstract class, only because I have certain common functionality such as, saving to store, retrieving from store, and deletion of the record. Also, some properties that would apply to all implemented objects.

C# only allows for inheritance from one class, so I can either use BusinessBase or the abstract class. I would also like the concrete types to have their own set of business rules. How can this be done with CSLA?

If I do what I have listed below, will the rules in both the abstract class as well as the concrete class get fired?

 

 

Some code ...

Abstract class:

public class Form : BusinessBase<Form> {

   private static PropertyInfo<string> FormNameProperty = RegisterProperty<string>(c => c.FormName);

   public string FormName

   {

      get { return GetProperty(FormNameProperty); }

   }

 

   public abstract void LoadContent();

   protected override void AddBusinessRules()

   {

      // business rules that are commmon for all implementations

   }

}

 

Concrete implementation:

 

public class FormA : Form {

   private static PropertyInfo<string> FirstNameProperty = RegisterProperty<string>(c => c.FirstName);

   public string FirstName

   {

      get { return GetProperty(FirstNameProperty); }

   }

 

   public override void LoadContent(){

      // some custom code

   }

 

   protected override void AddBusinessRules()

   {

      // business rules that only apply to this class

   }

}

 

Factory:

 

public static class FormFactory{

   public static Form GetForm(string formanmae) {

      Type formType = GetFormType(formName);

      if(formType == null)

         return null;

 

      var form = Activator.CreateInstance(formType) as ReferralForm;

         return form;

   }

}

RockfordLhotka replied on Monday, June 25, 2012

To create a base class you really need to make it MyBusinessBase<T> so the real business class is

public class MyRealClass : MyBusinessBase<MyRealClass>

The way CSLA uses generics in the base classes assumes T is always the type of the real business class.

maxg13 replied on Tuesday, June 26, 2012

Rock, thanks for your reply.  How about this post?

http://forums.lhotka.net/forums/p/10711/50007.aspx

The solution provided by JonnyBee is similar to what I was looking for.  I am currently in the middle of coding that solution in my app.  Can you provide your insight on this solution?

Thanks

RockfordLhotka replied on Tuesday, June 26, 2012

Jonny is describing exactly what I am saying, yes.

Copyright (c) Marimer LLC