Inject different repository depending on a querystring / derive controller and inject the repository depending on the controller type / ASP.NET MVC

前端 未结 3 733
生来不讨喜
生来不讨喜 2021-01-13 13:36

I have a search form that can search in different provider. I started out by having a base controller

public SearchController : Controller
{

    protected r         


        
3条回答
  •  猫巷女王i
    2021-01-13 13:55

    I was trying to figure out how to use the abstract factory pattern and still let structuremap resolve all the dependencies of my components.

    I believe that is the way I am going to implement it, but I submit this here to get some feedback if someone would read this.

    As explain in the previous answer, I do not want to build the whole object graph depending on which provider I need in the Abstract factory.

    ie :

    class StatServiceFactory : IStatServiceFactory
    {
        public IStatService Create(string provider)
        {
            switch(provider)
            {
                case "blog":
                    return new  StatService(IFacetRepository,ISearchManager,IConfigManager,BooleanQueryBuilder);
                           //How to resolve the Config, the SearchManager, and BooleanQueryBuilder?   
                           //Add more abstract factories? It starts to get messy in my opinion...
             }
        }
    
    }
    

    What I can do is have the abstract factory use my container to create an instance of my search managers depending on a parameter (coming from the querystring in my case)

    Structuremap allows to create named instances this way :

    x.For().Use().Named("Abc");
    x.For().Use().Named("Def");
    

    I need a way to inject the container in my Abstract factory. I would probably wrap the container in a wrapper defined like this. That would keep me from leaking Structuremap into my project. I dont need more that those 2 features within the abstract factory anyway, but it is not necessary:

    public interface IContainerWrapper
    {
        object GetInstance();
        object GetNamedInstance(string key);
    }
    

    and the implementation :

    public class ContainerImpl : IContainerWrapper
    {
         private readonly Container _container
         public ContainerImpl(Container container)
         {
              _container = container;
         }
    
         ...
    }
    

    And setup StructureMap to resolve dependencies to my abstract factory like that :

    x.For.Use(new ContainerImpl(this));
    x.For.Use()
    

    My factory would be then much simpler and would create my instance like that :

    public class SearchmanagerFactory
    {
        private readonly IContainerWrapper _container;
    
        public SearchmanagerFactory(IContainerProvider containerProvider)
        {
            _container = containerProvider;
        }
    
        public ISearchManager Create(string provider)
        {
           //eed to handle the bad input for provider.
            return (ISearchManager)
                _container.Resolve(provider);
        }
    }
    

    That seems pretty clean this way :). Thoughts?

提交回复
热议问题