I have a service endpoint initialized using DI. It is of the following style. This end point is used across the app.  public class CustomerService : ICustomerService{    private IValidationService ValidationService { get; set; }    private ICustomerRepository Repository { get; set; }    public CustomerService(IValidationService validationService,ICustomerRepository repository)    {        ValidationService = validationService;        Repository = repository;    }    public void Save(CustomerDTO customer)    {        if (ValidationService.Valid(customer))            Repository.Save(customer);    }Now, With the changing requirements, there are going to be different types of customers (Legacy/Regular). The requirement is based on the type of the customer I have to validate and persist the customer in a different way (e.g. if Legacy customer persist to LegacyRepository). The wrong way to do this will be to break DI and do somthing like   public void Save(CustomerDTO customer)    {        if(customer.Type == CustomerTypes.Legacy)        {            if (LegacyValidationService.Valid(customer))                LegacyRepository.Save(customer);        }        else        {            if (ValidationService.Valid(customer))                Repository.Save(customer);        }    }My options to me seems likeDI all possible IValidationService and ICustomerRepository and switch based on type, which seems wrong.The other is to change the service signature to Save(IValidationService validation, ICustomerRepository repository, CustomerDTO customer) which is an invasive change.Break DI. Use the Strategy pattern approach for each type and do something like:validation= CustomerValidationServiceFactory.GetStratedgy(customer.Type);validation.Valid(customer)but now I have a static method which needs to know how to initialize different services.I am sure this is a very common problem, What is the right way to solve this without changing service signatures or breaking DI?
How to do dependency Injection and conditional object creation based on type?
design;dependency injection
If you have only two types of customers i would use the ugly version to let CustomerService.Save(CustomerDTO customer) decide which repository to use. Of course this violates the open-closed-priciple but obeys the keepItSimpleStupid (kiss).If you really have several types of customers I would implement one customerservice for every type of customer and one customerservice on top that decides which spezialized service to use using a strategy pattern. > but now I have a static method which needs to know how to initialize different services.It is not necessary that the strategy is a static method.Instead of letting the strategy know every service you can reverse the dependency and let every spezialized service register at the stragey like this public class LegacyCustomerService : ICustomerService {    LegacyCustomerService(IStrategy strategy)    {       strategy.register(this, typeof(LegacyCustomer));    } }The strategy uses a hash/dictionary from customer-type to service.This is an open-closed-priciple solution that violates the keepItSimpleStupid (kiss) principle.