簡體   English   中英

使用CSLA.NET實現工廠模式

[英]Implementing a Factory Pattern with CSLA.NET

我想在CSLA中實現Factory Pattern。 我可以使用抽象基類或抽象接口。 我決定使用抽象類,只是因為我具有某些通用功能,例如保存到存儲,從存儲中檢索以及刪除記錄。 同樣,一些屬性將應用於所有已實現的對象。

C#僅允許從一個類繼承,因此我可以使用BusinessBase或抽象類。 我還希望具體類型具有自己的一套業務規則。 如何使用CSLA?

如果我做下面列出的事情,那么抽象類和具體類中的規則都會被解雇嗎?

一些代碼...

抽象類:

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
   }
}

具體實現:

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
   }
}

廠:

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;
   }
}

而不是使用Activator.CreateInstance,您應該使用Csla DataPortal。

var form = (Form)Csla.DataPortal.Create(formType, new Csla.Server.EmptyCriteria);

這樣,您就可以使用Csla方法創建業務對象,因此應該運行的所有規則都可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM