我目前正在使用 Ninject 作为我的 DI 的 MVC 3 项目,业务对象存储在单独的程序集中。我遇到了控制器参数的问题,在发回 CRUD 操作时,我收到错误“无法创建接口的实例”。我知道您无法创建接口的实例,但似乎我可以解决此问题的唯一方法是使用自定义模型绑定器并通过 FormCollection。这看起来真的很乱,我想尽可能多地保留项目中的特定类型代码 - 因此到处都是接口,并且 Ninject 到 DI 混凝土。不仅自定义模型绑定看起来很乱——我不会丢失我的 DataAnnotations 吗?
一些代码来描述我所拥有的:
public ActionResult Create()
{
// I'm thinking of using a factory pattern for this part
var objectToCreate = new ConcereteType();
return (objectToEdit);
}
[HttpPost]
public ActionResult Create(IRecord record)
{
// check model and pass to repository
if (ModelState.IsValue)
{
_repository.Create(record);
return View();
}
return View(record);
}
有没有人遇到过这个?你是怎么克服的?
谢谢!