Dependency injection 使用StructureMap和IDependencyResolver,可以';t在强类型视图上使用接口并创建操作

Dependency injection 使用StructureMap和IDependencyResolver,可以';t在强类型视图上使用接口并创建操作,dependency-injection,asp.net-mvc-3,structuremap,Dependency Injection,Asp.net Mvc 3,Structuremap,我有一个全新的项目,asp.NETMVC3 使用StructureMap和Nhibernate,这一切都非常标准 我有3个项目,核心,基础设施和用户界面 StructureMap连接工作正常,示例控制器上的索引操作工作正常 但是,在我的create视图中,我将@model设置为 @model Project.Core.Domain.ISample 在控制器上,我有一个正常的post方法: [HttpPost] public ActionResult Create(ISample

我有一个全新的项目,asp.NETMVC3

使用StructureMap和Nhibernate,这一切都非常标准

我有3个项目,核心,基础设施和用户界面

StructureMap连接工作正常,示例控制器上的索引操作工作正常

但是,在我的create视图中,我将@model设置为

@model Project.Core.Domain.ISample
在控制器上,我有一个正常的post方法:

    [HttpPost]
    public ActionResult Create(ISample sample)
    {
        try
        {
            _repo.Save(sample);

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
但我不断得到“无法创建接口实例”错误

堆栈上最后执行的行是:

[MissingMethodException:无法创建接口的实例。] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型、Boolean publicOnly、Boolean noCheck、Boolean&canBeCached、RuntimeMethodHandleInternal&ctor、Boolean&bNeedSecurityCheck)+0 System.RuntimeType.CreateInstanceSlow(布尔publicOnly、布尔skipCheckThis、布尔fillCache)+98 System.RuntimeType.CreateInstanceDefaultCtor(布尔publicOnly、布尔skipVisibilityChecks、布尔skipCheckThis、布尔fillCache)+241 System.Activator.CreateInstance(类型,布尔非公共)+69 CreateModel(ControllerContext ControllerContext,ModelBindingContext bindingContext,Type modelType)+199 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)+572 System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext ControllerContext,ModelBindingContext bindingContext)+449 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext ControllerContext,ParameterDescriptor ParameterDescriptor)+317

我希望mvc会在内部使用DependencyResolver,如果它这样做了,它将能够创建我的ISample接口的一个具体实例…但是我很有可能理解了一些完全错误的东西,这毫无意义

如果我对控制器进行此简单更改,一切正常:

public ActionResult Create(Sample sample)
我可能错了,但这对我来说似乎是错的,其他一切都可以使用接口进行通信,为什么我必须在Create操作上使用具体的类?这将剥夺接口给我的一些灵活性

有没有人知道如何继续,或者我是否走错了方向

谢谢你的关注

在Darin的帮助下,我就是这样得到了我想要的

我已经创建了一个新的GenericModelBinder(可能名称更好)

在global.asax中,我添加了:

ModelBinders.Binders.DefaultBinder = new GenericModelBinder();

谢谢你的帮助

您需要为
ISample
类型编写一个自定义模型活页夹,这样才能工作。ASP.NET MVC在调用控制器操作时使用默认的模型绑定器,以便从请求值实例化操作参数

public class MyISampleModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        // Here you need to return the proper instance of the ISample interface
        // based on the request values or some other rules you need
    }
}
然后在
Application\u Start
中注册此活页夹:

ModelBinders.Binders.Add(typeof(ISample), new MyISampleModelBinder());

您需要为
ISample
类型编写一个自定义模型活页夹,这样才能工作。ASP.NET MVC在调用控制器操作时使用默认的模型绑定器,以便从请求值实例化操作参数

public class MyISampleModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        // Here you need to return the proper instance of the ISample interface
        // based on the request values or some other rules you need
    }
}
然后在
Application\u Start
中注册此活页夹:

ModelBinders.Binders.Add(typeof(ISample), new MyISampleModelBinder());

谢谢你们的帮助,我在研究过程中偶然发现了类似的东西,但让我思考的是,为什么我需要为每个模型准备一个特殊的模型活页夹?为什么默认实现不使用DependecyResolver?它能够创建具体的类,它应该只调用该实例的依赖解析程序……否则这将是完全错误的?再次感谢!谢谢你们的帮助,我在研究过程中偶然发现了类似的东西,但让我思考的是,为什么我需要为每个模型准备一个特殊的模型活页夹?为什么默认实现不使用DependecyResolver?它能够创建具体的类,它应该只调用该实例的依赖解析程序……否则这将是完全错误的?再次感谢!