Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 对MVC3代码优先/存储库的混淆_Asp.net Mvc 3_Dependency Injection_Ninject - Fatal编程技术网

Asp.net mvc 3 对MVC3代码优先/存储库的混淆

Asp.net mvc 3 对MVC3代码优先/存储库的混淆,asp.net-mvc-3,dependency-injection,ninject,Asp.net Mvc 3,Dependency Injection,Ninject,有人能帮帮我吗?我有点糊涂了 我有这样一个实体: public class Code { public int ID { get; set; } public int UserID { get; set; } public string CodeText { get; set; } } public interface ICodeRepository { IQueryable<Code> Codes { get; } void AddCode

有人能帮帮我吗?我有点糊涂了

我有这样一个实体:

public class Code
{
    public int ID { get; set; }
    public int UserID { get; set; }
    public string CodeText { get; set; }
}
public interface ICodeRepository
{
    IQueryable<Code> Codes { get; }
    void AddCode(Code code);
    void RemoveCode(Code code);
    Code GetCodeById(int id);
}
public class SQLCodeRepository : ICodeRepository
{
    private EFSQLContext context;

    public SQLCodeRepository()
    {
        context = new EFSQLContext();
    }

    public IQueryable<Code> Codes
    {
        get { return context.Codes; }
    }

    public void AddCode(Code code)
    {
        context.Codes.Add(code);
        context.SaveChanges();
    }

    public void RemoveCode(Code code)
    {
        context.Codes.Remove(code);
        context.SaveChanges();
    }

    public Code GetCodeById(int id)
    {
        return context.Codes.Where(x => x.ID == id).FirstOrDefault();
    }
}
public class EFSQLContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Code> Codes { get; set; }
    public DbSet<PortfolioUser> PortfolioUsers { get; set; }
}
public class SearchController : Controller
{
    private ICodeRepository cRepo;

    public SearchController(ICodeRepository codeRepository)
    {
        cRepo = codeRepository;
    }
}
还有这样一个界面:

public class Code
{
    public int ID { get; set; }
    public int UserID { get; set; }
    public string CodeText { get; set; }
}
public interface ICodeRepository
{
    IQueryable<Code> Codes { get; }
    void AddCode(Code code);
    void RemoveCode(Code code);
    Code GetCodeById(int id);
}
public class SQLCodeRepository : ICodeRepository
{
    private EFSQLContext context;

    public SQLCodeRepository()
    {
        context = new EFSQLContext();
    }

    public IQueryable<Code> Codes
    {
        get { return context.Codes; }
    }

    public void AddCode(Code code)
    {
        context.Codes.Add(code);
        context.SaveChanges();
    }

    public void RemoveCode(Code code)
    {
        context.Codes.Remove(code);
        context.SaveChanges();
    }

    public Code GetCodeById(int id)
    {
        return context.Codes.Where(x => x.ID == id).FirstOrDefault();
    }
}
public class EFSQLContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Code> Codes { get; set; }
    public DbSet<PortfolioUser> PortfolioUsers { get; set; }
}
public class SearchController : Controller
{
    private ICodeRepository cRepo;

    public SearchController(ICodeRepository codeRepository)
    {
        cRepo = codeRepository;
    }
}
还有这样一个存储库:

public class Code
{
    public int ID { get; set; }
    public int UserID { get; set; }
    public string CodeText { get; set; }
}
public interface ICodeRepository
{
    IQueryable<Code> Codes { get; }
    void AddCode(Code code);
    void RemoveCode(Code code);
    Code GetCodeById(int id);
}
public class SQLCodeRepository : ICodeRepository
{
    private EFSQLContext context;

    public SQLCodeRepository()
    {
        context = new EFSQLContext();
    }

    public IQueryable<Code> Codes
    {
        get { return context.Codes; }
    }

    public void AddCode(Code code)
    {
        context.Codes.Add(code);
        context.SaveChanges();
    }

    public void RemoveCode(Code code)
    {
        context.Codes.Remove(code);
        context.SaveChanges();
    }

    public Code GetCodeById(int id)
    {
        return context.Codes.Where(x => x.ID == id).FirstOrDefault();
    }
}
public class EFSQLContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Code> Codes { get; set; }
    public DbSet<PortfolioUser> PortfolioUsers { get; set; }
}
public class SearchController : Controller
{
    private ICodeRepository cRepo;

    public SearchController(ICodeRepository codeRepository)
    {
        cRepo = codeRepository;
    }
}
在这样的背景下:

public class Code
{
    public int ID { get; set; }
    public int UserID { get; set; }
    public string CodeText { get; set; }
}
public interface ICodeRepository
{
    IQueryable<Code> Codes { get; }
    void AddCode(Code code);
    void RemoveCode(Code code);
    Code GetCodeById(int id);
}
public class SQLCodeRepository : ICodeRepository
{
    private EFSQLContext context;

    public SQLCodeRepository()
    {
        context = new EFSQLContext();
    }

    public IQueryable<Code> Codes
    {
        get { return context.Codes; }
    }

    public void AddCode(Code code)
    {
        context.Codes.Add(code);
        context.SaveChanges();
    }

    public void RemoveCode(Code code)
    {
        context.Codes.Remove(code);
        context.SaveChanges();
    }

    public Code GetCodeById(int id)
    {
        return context.Codes.Where(x => x.ID == id).FirstOrDefault();
    }
}
public class EFSQLContext : DbContext
{
    public DbSet<User> Users { get; set; }
    public DbSet<Code> Codes { get; set; }
    public DbSet<PortfolioUser> PortfolioUsers { get; set; }
}
public class SearchController : Controller
{
    private ICodeRepository cRepo;

    public SearchController(ICodeRepository codeRepository)
    {
        cRepo = codeRepository;
    }
}
然后尝试执行
cRepo.GetCodeById(1)
什么也不发生。但是如果我声明private
ICodeRepository rep=new SQLCodeRepository
,然后调用
rep.GetCodeById(1)
,我可以看到正在调用的存储库中的方法


我做错了什么?

从构造函数签名看,您将要进行依赖项注入。您缺少的步骤是使用Castle Windsor之类的工具设置DI容器。然后将MVC解析器配置为使用DI容器为您提供正确的
ICodeRepository
实现

您需要创建一个实现
idependencysolver
IDependencyScope
的解析器,以及一个继承
DefaultControllerFactory

一旦你有了这些,你可以做如下事情:

MyContainer container; // this needs to be a class level member of the asax
var configuration = GlobalConfiguration.Configuration;

container = new MyContainer() // may need additional stuff here depending on DI tool used

configuration.DependencyResolver = new MyDependancyResolver(container);
var mvcControllerFactory = new MyFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(mvcControllerFactory);
您可以从asax
应用程序\u Start()调用上述代码


有关使用Ninject和MVC3的更多详细信息,请参阅。抱歉,不是Ninject专家,但也许其他人可以加入?我认为您缺少的是将容器添加到解析器。(OP发表评论说他正在使用ninject)。好的。。。我正在使用Ninject进行DI。我在NinjectControllerFactory类中有这样的代码:
Mock mockCode=new Mock();Setup(m=>m.Codes).Returns(new List{new Code{ID=1,UserID=1,CodeText=“A700”}}.AsQueryable());ninjectKernel.Bind().ToConstant(mockCode.Object)
你能详细解释一下如何使用MVC解析器吗?请参阅我的编辑部分,了解如何进行此操作的一般、非ninject特定的示例。如果我通过NinjectControllerFactory注入Moq接口(我是否使用了正确的术语),那么对GetCodeById的方法调用将不起作用。但是如果我注入SQlCodeRepository,那么方法调用确实有效。为什么方法调用不能在模拟接口上工作?这也影响了我的单元测试——我正在遵循一个指南,它告诉我如何使用Moq编写测试,但是当我来测试控制器时,测试失败了,因为GetCodeById方法调用不起作用。我想你的Moq会有某种问题。您的容器是否正确地将Moq解析为ICodeRespository?如果你把DI从等式中去掉,只做cRepo=new mockCode,会发生什么?OP在下面说他正在使用NinjectWnat时添加了Ninject标记“然后尝试做cRepo.GetCodeById(1)什么都没发生”意味着?这意味着我需要做更多的阅读和研究。Microsoft ASP.NET团队写道:“模拟存储库实现IContactManagerRepository接口的方法,但这些方法实际上什么都不做。”[link]正确,模拟的目的是假装发生了方法调用并显示了一些预期行为,例如,在单元测试中,您只测试被测系统的行为,而不是下游的所有其他行为。我建议在Moq中使用“Verify”方法来验证您的测试是否调用了GetCodeById(1)方法,然后在GetCodeById方法上编写单独的测试。