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

前端 未结 3 735
生来不讨喜
生来不讨喜 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条回答
  •  佛祖请我去吃肉
    2021-01-13 14:09

    This is more an extensive comment than an answer to explain why an AbstractFactory seems complicated. Here is something that looks closer from the reality:

    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...
            }
        }
    }
    

    The FacetRepository is the same for any provider, but the SearchManager changes, the ConfigManager changes, and the BooleanQueryBuilder is an abstract class with different implementation for different provider (because every API doesnt use the same keyword for their queries) All those dependencies are currently resolved by structuremap, based on the type of the controller.

    I would really like to keep the benefit of StructureMap here, rather than using factories all the way, for each different pieces.'

    Please see my edit at the end of my question for another suggestion to my problem.

提交回复
热议问题