2

I have a service called as IDataService which has implementations like IUserDataService , IOrderDataService which are used to get the data from an Api Controller.

All of these are generic, so if I have a controller like the one below,

public class DataController 
{   
    private readonly IDataService _dataSvc;     
    public DataController(IDataService dataSvc)
    {   
        _dataSvc = dataSvc;     
    }       
    public void GetData(int limit = 1000)
    {       
        return _dataSvc.GetData(limit);     
    } 
}

public interface IDataService
{   
    IEnumerable<T> GetData<T>(int limit = 1000); 
}

public class UserDataService : IDataService
{   
    IEnumerable<User> GetData<User>(int limit = 1000)   
    {       
        return Set<User>().Take(limit); //this is just a placeholder    
    } 
}

public class ProjectDataService : IDataService
{   
    IEnumerable<Project> GetData<Project>(int limit = 1000)     
    {       
        return Set<Project>().Take(limit); //this is just a placeholder     
    } 
}

I am reading about the usage of the HttpRequest so that if i am able to take the value from header and use it to resolve somehow the service, all the flow will be purely dynamic.

Please let me know if any options is possible on this scenario.

slow
  • 566
  • 4
  • 17
Saran
  • 99
  • 2
  • 14
  • I am sure Autofac allows to use additional data when resolving the dependency. You can read the documentation and try find what will work for you. I also saw this https://stackoverflow.com/questions/22384884/autofac-with-multiple-implementations-of-the-same-interface question which may be of some help. – Leron Mar 21 '18 at 07:30
  • 1
    See [this answer](https://stackoverflow.com/a/32415954). You could have a parameter passed in through a header decide which service to select. – NightOwl888 Mar 21 '18 at 07:36
  • @NightOwl888: The answer talks about Unity IoC, but i am looking for a solution in Autofac – Saran Mar 22 '18 at 06:59
  • With that answer, it doesn't matter what DI container you use. It solves the issue using design patterns instead of DI. The only thing you would need for Autofac is to translate the DI registration. – NightOwl888 Mar 22 '18 at 07:33
  • @NightOwl888: Thanks, I am trying to apply that in Autofac – Saran Mar 22 '18 at 10:37

0 Answers0