2011-04-19 68 views
1

我是新来的MVC和依赖注入,所以我使它成为一个资源库时有关于脚手架几个问题:MVC与脚手架-repository依赖注入

1)您不能删除PersonController()构造函数否则你得到一个错误:没有为此对象定义的无参数构造函数

2)如果您的确按照原样使用它,是不是会将您的存储库紧密地耦合到PersonRepository?

代码示例:

//如果使用依赖注入,你可以删除下面的构造

public PersonsController() : this(new PersonRepository()) 
    { 
    } 

    public PersonsController(IPersonRepository PersonRepository) 
    { 
     this.PersonRepository = PersonRepository; 
    } 

回答

1

我会写一篇博客文章这样。什么你需要做的是注册一个DI容器MVC 3。例如,使用的NuGet,可以

安装,包装Ninject.Mvc3

然后在App_Start文件夹中,有一个文件,你可以添加您的Ninject绑定到。例如:

Bind()。To();

然后你可以删除默认的ctor。如果你无法实现它,我会尽快写出它的博客。