如何在C#中实现工厂设计模式 [英] how to implement factory design pattern in C#

查看:157
本文介绍了如何在C#中实现工厂设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的应用程序中实现工厂模式

I am trying to implement Factory pattern in my application.

参考这些链接,我试图实现,但是停留在一个地方不确定如何继续。

With reference to these links, I am trying to implement but stuck at one place & not sure how to proceed.

  • How to update this class/method to improve Code Metrics
  • http://www.dofactory.com/net/factory-method-design-pattern
  • How to implement Factory pattern?

请在我的
代码中找到// confused here如何实现我被卡住了。

Please find "// confused here how do I implement here" comment in my code to get where I am stuck.



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

有人可以指导我实现工厂模式+改进我的代码片段吗?

Can somebody guide me to implement the factory pattern + improve my code-snippet?

任何帮助/建议高度赞赏。

Any help/suggestion highly appreciated.

推荐答案


  1. 不要使用这样的东西:

  1. Do not use such things:

IReportHandler dal = new ReportFactory();

IReportHandler dal = new ReportFactory();

因为它对界面和创造耦合到具体实现。而是使用依赖注入容器,并通过构造函数参数或属性注入这些工厂。最受欢迎的DI容器是Castle Windsor,Ninject,Unity,Autofac等。

Because it makes useless your dependency on interface and create coupling to concrete realization. Instead use Dependency Injection container and inject such factories via constructor parameters or properties. Most popular DI containers are Castle Windsor, Ninject, Unity, Autofac etc.

如果不想使用容器 - 至少在一个地方创建所有具体的实现在程序入口点,注册服务定位器中的所有实现(详细了解它),并通过构造函数将服务定位器传递给层次结构。

If you don't want to use containers - at least create all your concrete implementations in one place in program entry point, register all implementations in Service Locator (read more about it) and pass Service Locator to hierarchy via constructors.


  1. 尽量避免使用静态方法。改用界面你想要有代码取决于抽象,可以很容易测试和可笑。

这篇关于如何在C#中实现工厂设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆