mardi 20 décembre 2016

how to implement factory design pattern in 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.

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.

Aucun commentaire:

Enregistrer un commentaire