how to implement factory design pattern in C#

[亡魂溺海] 提交于 2020-01-05 05:59:13

问题


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?

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();

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. Try to avoid static methods. Use interfaces instead. You want to have code which depends on abstractions and can be easy testable and mockable.



回答2:


Your UI layer class need instance of 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;
    }
}


来源:https://stackoverflow.com/questions/41240184/how-to-implement-factory-design-pattern-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!