简体   繁体   English

使用CSLA.NET实现工厂模式

[英]Implementing a Factory Pattern with CSLA.NET

I would like to implement Factory Pattern in CSLA. 我想在CSLA中实现Factory Pattern。 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. C#仅允许从一个类继承,因此我可以使用BusinessBase或抽象类。 I would also like the concrete types to have their own set of business rules. 我还希望具体类型具有自己的一套业务规则。 How can this be done with CSLA? 如何使用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. 而不是使用Activator.CreateInstance,您应该使用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. 这样,您就可以使用Csla方法创建业务对象,因此应该运行的所有规则都可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM