beantownace
Active member
- Joined
- Feb 15, 2019
- Messages
- 26
- Programming Experience
- 5-10
Hello all,
I am trying to figure this out so I can move a switch statement into the factory side versus in the controller I have. I want to somehow use generics with this so I can just have one method that will return the correct worker type based on the switch statement. Thanks for any assist on this one.
Code example:
I am trying to figure this out so I can move a switch statement into the factory side versus in the controller I have. I want to somehow use generics with this so I can just have one method that will return the correct worker type based on the switch statement. Thanks for any assist on this one.
Code example:
Sample Code:
// Controller:
public CustomerController(ICustomerFactory customerFactory)
{
_customerFactory = customerFactory ?? throw new ArgumentNullException(nameof(customerFactory));
}
public override async Task<IActionResult> GetCustomerStatus([FromBody] CustomerRequest request)
{
try
{
//I WANT TO MOVE THIS SWITCH STATEMENT TO THE FACTORY CLASS SOMEHOW AND BE ABLE TO USE GENERICS TO HANDLE WHAT INSTANCE TO PASS BACK AS SHOWN IN CUSTOMERFACTORY BELOW AS
//I REPEAT THIS SWITCH THROUGHOUT THE CONTROLLER SO HOPING I CAN USE ONE METHOD AND
switch (request.Oms)
{
case Customer.CompanyAEnum:
await _customerFactory.CompanyAWorker().GetCustomerStatus(request);
break;
case Customer.CompanyBEnum:
await _customerFactory.CompanyBWorker().GetCustomerStatus(request);
break;
default:
break;
}
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
//Factory Class:
public class CustomerFactory : ICustomerFactory
{
public CompanyA.Workers.Interfaces.ICustomerWorker CompanyAWorker()
{
return new CompanyA.Workers.CustomerWorker();
}
public CompanyB.Workers.Interfaces.ICustomerWorker CompanyBWorker()
{
return new CompanyB.Workers.CustomerWorker();
}
}
//Factory Interface:
public interface ICustomerFactory
{
CompanyA.Workers.Interfaces.ICustomerWorker CompanyAWorker();
CompanyB.Workers.Interfaces.ICustomerWorker CompanyBWorker();
}