2016-12-20 5 views
1

私のアプリケーションにFactory patternを実装しようとしています。Cで工場設計パターンを実装する方法

これらのリンクを参照して、私は実装しようとしていますが、1つの場所に貼り付けています&続行方法がわかりません。

私は立ち往生午前どこ得るために、私 コード内のコメントを "どのように私はここに実装するのですここで混乱し//" を見つけてください。

//DAL Layer 
public interface IReportHandler 
{ 
    IEnumerable<DocumentMapper> FetchDocumentsList(Guid clientId, int pager = 0); 


} 



public class ReportHandler : IReportHandler 
{ 
     public IEnumerable<DocumentMapper> FetchDocumentsList(Guid clientId, int pager = 0) 
    { 
      //implentation of the method 
    } 
} 



//BLL 
public interface IReportFactory 
{ 
    IReportHandler Create(int factoryId); 
} 

public class ReportFactory : IReportFactory 
{ 
    private IReportHandler reportObj; 

    public override IReportHandler Create(int factoryId) 
    { 
     switch (factoryId) 
     { 
      case 1: 
       reportObj = new ReportHandler(); 
       return reportObj; 
      default: 
       throw new ArgumentException(""); 
     } 


    } 
} 

//UI Layer 
    public String GetAllDocuments(string url,int pager =0) 
    { 
     if (SessionInfo.IsAdmin) 
     { 
      string documents ; 
      //call GetDocumentIntoJson() private method 

     } 
     else 
     { 
      return "Sorry!! You are not authorized to perform this action"; 
     } 
    } 


    private static string GetDocumentIntoJson(int clientId, int pager) 
    { 
     // confused here how do I implement here 
     IReportHandler dal = ReportFactory 
     var documents = dal.FetchDocumentsList(clientId, pager); 
     string documentsDataJSON = JsonConvert.SerializeObject(documents); 

     return documentsDataJSON; 
    } 

誰かが工場出荷時のパターンを実装+私のコード・スニペットを改善するために私を導くことはできますか?

非常に高く評価されているヘルプ/提案。

+0

あなたの元の意図は何ですか?あなたは工場が必要と思いますか? – guillaume31

+0

コードレビューで同一人物からも尋ねられているので、この質問を議論の対象外としています。http://codereview.stackexchange.com/questions/150382/工場向けレポートハンドラー –

答えて

0

あなたのUI層のクラスは、このようなものを使用しないでくださいReportFactory

public class UIclass 
{ 
    private readonly IReportFactory _reportFactory; 

    public UIclass(IReportFactory reportFactory) 
    { 
     _reportFactory = reportFactory; 
    } 

    private string GetDocumentIntoJson(int clientId, int pager) 
    { 
     // Use factory to get a ReportHandler 
     // You need provide "factoryId" from somewhere too 
     IReportHandler dal = _reportFactory.Create(factoryId); 

     var documents = dal.FetchDocumentsList(clientId, pager); 
     string documentsDataJSON = JsonConvert.SerializeObject(documents); 

     return documentsDataJSON; 
    } 
} 
+0

これが本質的に正しいので、なぜこれが投票されたのか分かりません。あなたがそれを行うことができる唯一の他の方法は、インターフェイスを完全に無関係にする方法の中でクラスを立てることです。 –

+0

'static'を除いて - これは奇数でOPのコードにあります。 –

+0

@MichaelCoxon、私の更新された実装を確認してください。http://codereview.stackexchange.com/questions/150382/is-this-the-right-way-to-implement-factory-pattern –

1
  1. のインスタンスが必要になります。

    IReportHandlerのダル=新しいReportFactory();

インターフェイスに依存することを無意味にし、具体的な実現につなげるためです。代わりにDependency Injectionコンテナを使用し、そのようなファクトリをコンストラクタのパラメータまたはプロパティを介して注入します。最も普及しているDIコンテナは、Castle Windsor、Ninject、Unity、Autofacなどです。サービスでコンテナを使用したくない場合は、少なくともプログラムエントリポイントの1か所に具体的な実装をすべて作成してください。 Locator(詳細はこちら)、コンストラクタ経由でService Locatorを階層に渡します。

  1. 静的メソッドを避けるようにしてください。インターフェイスを代わりに使用してください。あなたは、抽象化に依存し、容易にテスト可能でモック可能なコードを持ちたいと思っています。
+0

指摘しています。ありがとう –

関連する問題