MVC 3 中 ModelBinder 构造函数注入的 IModelBinderProvider 实现示例

发布于 2024-10-06 21:10:21 字数 743 浏览 4 评论 0原文

我需要将我的自定义 ModelBinder 连接到 MVC 3 中的 DI 容器,但我无法让它工作。

所以。这就是我所拥有的: 具有构造函数注入服务的 ModelBinder。

public class ProductModelBinder : IModelBinder{
  public ProductModelBinder(IProductService productService){/*sets field*/}
  // the rest don't matter. It works.
}

如果我像这样添加它,我的活页夹就可以正常工作:

ModelBinders.Binders.Add(typeof(Product),
     new ProductModelBinder(IoC.Resolve<IProductService>()));

但这是旧的做法,我不希望那样。

我需要的是有关如何将该 modelbinder 连接到我注册的 IDependencyResolver 的帮助。

根据 Brad Wilson 的说法,秘密是使用 IModelBinderProvider 实现,但目前还不清楚如何连接它。 (在这篇文章中

有人有例子吗?

I need to wire my custom ModelBinder up to my DI container in MVC 3, but I can't get it working.

So. This is what I have:
A ModelBinder with a constructor injected service.

public class ProductModelBinder : IModelBinder{
  public ProductModelBinder(IProductService productService){/*sets field*/}
  // the rest don't matter. It works.
}

My binder works fine if I add it like this:

ModelBinders.Binders.Add(typeof(Product),
     new ProductModelBinder(IoC.Resolve<IProductService>()));

But that is the old way of doing it, and I don't want that.

What I need is help on how to hook that modelbinder up to the IDependencyResolver I've registered.

According to Brad Wilson the secret is using a IModelBinderProvider implementation, but its very unclear as to how to wire that up. (in this post)

Does anyone have an example?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明天过后 2024-10-13 21:10:21

我在编写 MVC 3 应用程序时遇到了同样的情况。我最终得到了这样的结果:

public class ModelBinderProvider : IModelBinderProvider
{
    private static Type IfSubClassOrSame(Type subClass, Type baseClass, Type binder)
    {
        if (subClass == baseClass || subClass.IsSubclassOf(baseClass))
            return binder;
        else
            return null;
    }

    public IModelBinder GetBinder(Type modelType)
    {
        var binderType = 
            IfSubClassOrSame(modelType, typeof(xCommand), typeof(xCommandBinder)) ??
            IfSubClassOrSame(modelType, typeof(yCommand), typeof(yCommandBinder)) ?? null;

        return binderType != null ? (IModelBinder) IoC.Resolve(binderType) : null;
    }
}

然后我在我的 IoC 容器中注册了它(在我的例子中是 Unity):

_container.RegisterType<IModelBinderProvider, ModelBinderProvider>("ModelBinderProvider", singleton());

这对我有用。

I faced the same situation when coding my MVC 3 app. I ended up with something like this:

public class ModelBinderProvider : IModelBinderProvider
{
    private static Type IfSubClassOrSame(Type subClass, Type baseClass, Type binder)
    {
        if (subClass == baseClass || subClass.IsSubclassOf(baseClass))
            return binder;
        else
            return null;
    }

    public IModelBinder GetBinder(Type modelType)
    {
        var binderType = 
            IfSubClassOrSame(modelType, typeof(xCommand), typeof(xCommandBinder)) ??
            IfSubClassOrSame(modelType, typeof(yCommand), typeof(yCommandBinder)) ?? null;

        return binderType != null ? (IModelBinder) IoC.Resolve(binderType) : null;
    }
}

Then I registered this in my IoC container (Unity in my case):

_container.RegisterType<IModelBinderProvider, ModelBinderProvider>("ModelBinderProvider", singleton());

This works for me.

治碍 2024-10-13 21:10:21

您需要编写自己的 IModelBinderProvider 并将其注册到 ModelBinderProviders.BinderProviders 集合中:

public class YourModelBinderProvider : IModelBinderProvider {
    public IModelBinder GetBinder(Type modelType) {
         if(modelType == typeof(Product)) {
             return new ProductModelBinder(...);
         }
         return null;
    }
}

在 Global.asax 中:

ModelBinderProviders.BinderProviders.Add(new YourModelBinderProvider());

You need to write your own IModelBinderProvider and register it with the ModelBinderProviders.BinderProviders collection:

public class YourModelBinderProvider : IModelBinderProvider {
    public IModelBinder GetBinder(Type modelType) {
         if(modelType == typeof(Product)) {
             return new ProductModelBinder(...);
         }
         return null;
    }
}

In Global.asax:

ModelBinderProviders.BinderProviders.Add(new YourModelBinderProvider());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文