mercredi 18 novembre 2020

Factory Design Pattern: Hello guys! I would like to do a refactor over my C# code to implement some kind of Factory Design Pattern

Here is the thing:

I have a Web API App(.Net Core) that is part of my Back-End. I have implemented the Unit of Work and Repository pattern into a N-Layer 1 Tier Architecture. The way how the Client App and the Back-End communicates is thru' sending DTO's (Data Transfer Object). So, I have different DTO's obviously with a different structure and composition according to what the Repository Method is requesting. This is because at design time I define the type of DTO the Client is expecting, something like this:

public EmployeeDto GetEMployeeDto()
{
    public EmployeeDto employee = new EmployeeDto();
    
    .
    .
    .
    \\ Code to get data from data base and full fill the fields of the DTO.
    .
    .
    .

    return EmployeeDto;

}

Where EmployeeDto is something like this:

public cladd EmployeeDto()
{
     public int Employee Id {get;set;}
     public string EmployeeName {get;set;}
}

What I would like to do is remove the instantiating code (public EmployeeDto employee = new EmployeDto();) what is thigh couple and implement some kind of factory pattern where I just call the Factory object, pass the type of DTO I want(because I already know it) and that the Factory return me the type of DTO I requested and with the DTO state(data).

Just keep on mind that I have several types of DTO's with different structure, so the way how they are fill is different and that the Factory object could be called in different methods and could be requested different DTO's.

Something like:

var employee = FactoryDto.GetDto(EmployeDto);
var employeeHistory = FactoryDto.GetDto(EmployeeHistoryDto);

With just one line get the whole "package" the DTO it's self and the data.

I just need a guidance to which Design Pattern could be the best option and an example, I have read about the Creational Design pattern Factory and the different flavors it has but I have not understood yet how could this achieve.

Any help will be welcome and thank. Regards.

Aucun commentaire:

Enregistrer un commentaire