【发布时间】:2016-12-20 10:30:51
【问题描述】:
我正在尝试在我的应用程序中实现Factory pattern。
参考这些链接,我正在尝试实施但卡在一个地方并且不知道如何继续。
- How to update this class/method to improve Code Metrics
- http://www.dofactory.com/net/factory-method-design-pattern
- How to implement 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