根据查询字符串注入不同的版本库/导出控制器并注入取决于控制器类型/ ASP.NET MVC库 [英] Inject different repository depending on a querystring / derive controller and inject the repository depending on the controller type / ASP.NET MVC

查看:130
本文介绍了根据查询字符串注入不同的版本库/导出控制器并注入取决于控制器类型/ ASP.NET MVC库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在不同的供应商搜索搜索表单。
我开始通过具有基本控制器

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

public SearchController : Controller
{

    protected readonly ISearchService _searchService

    public SearchController(ISearchService searchService)
    {
        _searchService= searchService;
    }

    public ActionResult Search(...)
    {
        // Use searchService to query and return a view.
    }

}

和子控制器

TwitterController : SearchController
{
    ...
}

NewsController : SearchController
{
    ...
}

我用StructureMap插入我的控制器中的所有依赖。有了这个设置,我能够根据被实例化控制器的类型来改变SearchService。

I use StructureMap to insert all my dependencies in the controller. With this setup, I was able to change the SearchService depending on the type of the controller being instanciated.

x.For<ISearchService>().ConditionallyUse(o =>
      {
            o.TheDefault.Is.OfConcreteType<NewsSearchService>();

            o.If(c => c.ParentType == typeof(TwitterController))
             .ThenIt.Is.OfConcreteType<TwitterSearchService>();

             ...

      });

这甚至让我可以为每个控制器设置不同的意见,(只是把相应的文件夹(微博,新闻...)和父控制器仍是处理所有的搜索,用一个简单的

That even allowed me to set different Views for each controller, (just putting the corresponding folder (Twitter, News...) and the Parent controller is still handling all the Search, with a simple

return View(results) 

这是显示正确的观点具体到Twitter,新闻或其他

which is displaying the correct view specific to twitter, news, or other

现在很冷静,看起来很棒,我一个单一的形式和不同的视图显示在同一页面上的标签。这就是它开始变得这种方法复杂。该表格​​必须张贴到/ Twitter的Twitter的搜索,到/新闻在新闻搜索...这意味着根据我的选项卡,我应该改变形式的操作参数,并显示在表格返回时正确的选项卡根据..网址是什么?疯狂如下:

Now that was cool and looked great, I a single form and the different views are displayed in tabs on the same page. That's where it starts to get complicated with this approach. The form has to post to /Twitter to search in twitter, to /News to search in news... which means I should change the action parameter of the form depending on which tab I am and display the correct tab on when the form returns depending on.. the url? craziness follows.

如果您已经构建了这样的事情已经或者知道什么是解决这个问题的最好的方法,请建议都欢迎。

If you have built something like this already or know what's the best approach to this, please advices are welcome.

现在我想我会的形式使用参数,并张贴到一个控制器具有痛苦少。我想注入根据此参数的正确SearchService的。什么是最好的方法?我想用一个模型粘结剂,

Now I think I would have less pain using a parameter in the form and posting to a single controller. I am thinking of injecting the correct SearchService depending on this parameter. What would be the best approach? I thought of using a model binder,

所以,我有我的ActionMethod看起来像这样的:

So I would have my ActionMethod that look like this:

public ActionResult Search(ISearchService service, Query query)
{
    var results = service.Find(query);
}

不过,我认为需要做这样的呼吁在ModelBinder的

But I think would need to make a call like this in the ModelBinder

ObjectFactory.GetInstance(...);

根据描述要使用的供应商,这似乎并没有更优雅我的查询参数。我觉得卡住,帮助:(

Based on the querystring parameter that describe which provider to use, and that doesn't seem more elegant to me. I feel stuck, help :(.

推荐答案

我试图找出如何使用抽象工厂模式,仍然让structuremap解决我的组件的所有依赖关系。

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.

作为在previous答案解释,我不想因我需要在抽象工厂,供应商建立整个对象图。

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.

例如:

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允许创建命名实例是这样的:

Structuremap allows to create named instances this way :

x.For<ISearchManager>().Use<AbcSearchManager>().Named("Abc");
x.For<ISearchManager>().Use<DefSearchManager>().Named("Def");

我需要一种方法在我的抽象工厂注入容器。
我可能会换在这样定义的包装容器。这将让我泄漏Structuremap到我的项目。我不需要更多的抽象工厂内的2功能无论如何,但它是没有必要的:

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<T>();
    object GetNamedInstance<T>(string key);
}

和实施

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

     ...
}

和设置StructureMap来解决依赖于我的抽象工厂这样的:

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

x.For<IContainer>.Use(new ContainerImpl(this));
x.For<IFactory>.Use<Factory>()

我厂是那么简单得多,会创造我的实例这样的:

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<ISearchManager>(provider);
    }
}

这似乎是pretty清理这种方式:)。
思考?

That seems pretty clean this way :). Thoughts?

这篇关于根据查询字符串注入不同的版本库/导出控制器并注入取决于控制器类型/ ASP.NET MVC库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆