由adam freeman和steven sanderson撰写的Pro ASP.net MVC3框架书

时间:2012-08-29 08:54:15

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

我在使用pro asp.net mvc 3框架书时遇到了麻烦。在页175上创建产品存储库。

我有错误:错误1'SportsStore.Domain.Concrete.EFProductRepository'未实现接口成员'SportsStore.Domain.Abstract.IProductRepository.Products.set' SportsStore \ SportsStore.Domain \ Concrete \ EFProductRepository.cs 11 18 SportsStore.Domain #

错误2命名空间'SportsStore.Domain'中不存在类型或命名空间名称'Concrete'\ SportsStore.WebUI \ Infrastructure \ NinjectControllerFactory.cs 11 26 SportsStore.WebUI

这是我的存储库:

using System; using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using SportsStore.Domain.Abstract; 
using SportsStore.Domain.Entities; 
//using System.Web;

namespace SportsStore.Domain.Concrete 
{
    public class EFProductRepository : IProductRepository
    {
        private EFDbContext context = new EFDbContext();
        public IQueryable<Product> Products
        {
            get { return context.Products; }
        }
    } 
}

我公开的产品类:

namespace SportsStore.Domain.Entities 
{
  public class Product
  {
      public int ProductID { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
      public decimal Price { get; set; }
      public string Category { get; set; }
  } 
}

我的界面:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using SportsStore.Domain.Entities;

namespace SportsStore.Domain.Abstract 
{
  public interface IProductRepository
  {
      IQueryable<Product> Products { get; set; }
  } 
}

我的EFDBContext类:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using SportsStore.Domain.Entities; 
using System.Data.Entity;

namespace SportsStore.Domain.Concrete 
{
  public class EFDbContext : DbContext 
  {
      //DBset<type>   Property name is Products
      public DbSet<Product> Products { get; set; }
  }
}

我的ninjectcontrollerfactory类:

using System; using System.Collections.Generic; 
using System.Linq; using System.Web; 
using System.Web.Mvc; 
using System.Web.Routing; 
using Ninject; 
using Moq; 
using SportsStore.Domain.Abstract; 
using SportsStore.Domain.Entities; 
using SportsStore.Domain.Concrete;

namespace SportsStore.WebUI.Infrastructure 
{
    public class NinjectControllerFactory : DefaultControllerFactory 
    {
        private IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
        }

        private void AddBindings()
        {
            //P163 - Add a mock implementation of the IProductRepository Interface
            //Mock<IProductRepository> _mock = new Mock<IProductRepository>();
            //_mock.Setup(m => m.Products).Returns(new List<Product> 
            //{
            //    new Product {Name = "Football",Price = 25},
            //    new Product {Name = "Surf Board",Price = 179},
            //    new Product {Name = "Running Shoes",Price = 95},

            //}.AsQueryable());
            //_ninjectKernel.Bind<IProductRepository>().ToConstant(_mock.Object);


            ninjectKernel.Bind<IProductRepository>().To<EFProductRepository>();    //P176 Bind to our Live repository

        }

    } 
}

有3个参考项目: SportsStore.Domain SportsStore.UnitTests =参考Moq,Ninject,SportsStore.Domain,SportsStore.WebUI SportsStore.WebUI =参考Ninject,SportsStore.Domiain

我有搜索谷歌,并说它将SportsStore.Domain项目的参考添加到WebUI项目,但当我这样做时,它说它不能,因为它会导致循环依赖。

上面的错误1和错误2,我不明白,因为接口已实现,命名空间也在NinjectControllerFactory中,我将IProductRepository绑定到接口EFProductRepository

由于

1 个答案:

答案 0 :(得分:2)

远离

取消'set'
public interface IProductRepository 
{ 
    IQueryable<Product> Products { get; set; } 
}  

它应该解决你的问题