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;
}
}
Instead of using Activator.CreateInstance, you should use the Csla DataPortal.
var form = (Form)Csla.DataPortal.Create(formType, new Csla.Server.EmptyCriteria);
This way you are creating your business object using the Csla way, so any rules that should be run will be.