使用结构图3注入自定义模型绑定器

时间:2014-11-04 07:22:32

标签: c# .net asp.net-mvc structuremap

我已经找到了几个使用ASP.NET MVCStructureMap项目中注入自定义模型绑定器的指南,并按照本教程进行了操作:http://benfoster.io/blog/model-binder-dependency-injection-structuremap

我安装了StructureMap.MVC5个包。并在默认注册表中添加了类型绑定:

public class DefaultRegistry : Registry {
    public DefaultRegistry() {
        Scan(scan => {
            scan.Assembly(System.Reflection.Assembly.GetExecutingAssembly());
            scan.WithDefaultConventions();
            scan.With(new ControllerConvention());
        });

        For(typeof(IRepository<>)).Use(typeof(MongoDbRepository<>));

        var dictionary = new ModelBinderMappingDictionary();
        dictionary.Add<Car, CarModelBinder>();

        For<ModelBinderMappingDictionary>().Use(dictionary);
        For<IModelBinderProvider>().Use<StructureMapModelBinderProvider>();
    }
}

public class StructureMapModelBinderProvider : IModelBinderProvider {
    private readonly IContainer container;

    public StructureMapModelBinderProvider(IContainer container) {
        this.container = container;
    }

    public IModelBinder GetBinder(Type modelType) {
        var mappings = container.GetInstance<ModelBinderMappingDictionary>();
        if(mappings != null && mappings.ContainsKey(modelType))
            return container.GetInstance(mappings[modelType]) as IModelBinder;

        return null;
    }
}

现在我得到一个指向该行的NullReferenceException

var mappings = container.GetInstance<ModelBinderMappingDictionary>();

这些内部异常文本:

  

在c:\ BuildAgent \ work \ 996e173a8ceccdca \ src \ StructureMap \ SessionCache.cs中的StructureMap.SessionCache.GetDefault(Type pluginType,IPipelineGraph pipelineGraph):43      C:\ BuildAgent \ work \ 996e173a8ceccdca \ src \ StructureMap \ Container.cs中的StructureMap.Container.GetInstance(Type pluginType):331      C:\ BuildAgent \ work \ 996e173a8ceccdca \ src \ StructureMap \ Container.cs中的StructureMap.Container.GetInstance():201      d:\ projects \ unicorn \ unicorn.web \ infrastructure \ model_binding \ StructuremapModelBinderProvider.cs中的my_project_name.StructureMapModelBinderProvider.GetBinder(Type modelType):15      System.Web.Mvc.ModelBinderProviderCollection.GetBinder(Type modelType)+57      System.Web.Mvc.ModelBinderDictionary.GetBinder(TypeTypeType,IModelBinder fallbackBinder)+27      System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType,Boolean fallbackToDefault)+71      等等...

该代码有什么问题?

2 个答案:

答案 0 :(得分:1)

看起来您正在尝试将ModelBinder Mapping Dictionary注册为Add方法的返回值,该方法为void。 试试这个(默认注册表):

    var modelBinderMappingDictionary = new ModelBinderMappingDictionary();
    modelBinderMappingDictionary.Add<Car, CarModelBinder>();
    For<ModelBinderMappingDictionary>().Use(modelBinderMappingDictionary);

答案 1 :(得分:1)

好的我明白了!

我尝试为StructureMapModelBinderProvider使用另一种生命周期类型:

public class ModelBindingRegistry : Registry {
    public ModelBindingRegistry() {
        Scan(scan => {
            scan.TheCallingAssembly();
        });

        var dictionary = new ModelBinderMappingDictionary();
        dictionary.Add<Car, CarModelBinder>();

        For<ModelBinderMappingDictionary>().Use(dictionary);
        For<IModelBinderProvider>().Singleton().Use<StructureMapModelBinderProvider>();
    }
}

使用Singleton一切正常!不确定Singleton范围是否适合这里?