【问题标题】:how to implement factory design pattern in C#如何在 C# 中实现工厂设计模式
【发布时间】:2016-12-20 10:30:51
【问题描述】:

我正在尝试在我的应用程序中实现Factory pattern

参考这些链接,我正在尝试实施但卡在一个地方并且不知道如何继续。

请在我的评论中找到“// 在这里混淆我如何在此处实现” 代码可以到达我卡住的地方。

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

有人可以指导我实现工厂模式 + 改进我的 code-sn-p 吗?

非常感谢任何帮助/建议。

【问题讨论】:

  • 你的初衷是什么,是什么让你觉得你需要一个工厂?
  • 我投票决定将此问题作为离题结束,因为同一个人在代码审查中也提出了这个问题,并且他们在那里得到了一些非常好的答案codereview.stackexchange.com/questions/150382/…

标签: c# asp.net design-patterns factory-pattern


【解决方案1】:
  1. 不要使用这样的东西:

    IReportHandler dal = new ReportFactory();

因为它使您对接口的依赖变得无用,并创建了与具体实现的耦合。而是使用 Dependency Injection 容器并通过构造函数参数或属性注入此类工厂。最受欢迎的 DI 容器是 Castle Windsor、Ninject、Unity、Autofac 等。

如果您不想使用容器 - 至少在程序入口点的一个位置创建所有具体实现,在 Service Locator 中注册所有实现(阅读更多信息)并传递 Service通过构造函数定位到层次结构。

  1. 尽量避免使用静态方法。改用接口。您希望拥有依赖于抽象且易于测试和模拟的代码。

【讨论】:

  • 指出。谢谢
【解决方案2】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
相关资源
最近更新 更多