Pro Asp.Net MVC3 - Freeman和Sanderson的Apress书

时间:2011-08-22 19:40:43

标签: asp.net-mvc-3

从“清单7-7添加操作方法” 在本书的第165页

当我添加 “return View(repository.Products);” 我明白了 错误:无法在此上下文中使用属性或索引器“SportsStore.Domain.Abstract.IProductRepository.Products”,因为get访问器不可访问。 然后,当我试图跑 - 我得到 “不一致的可访问性:属性类型'System.Linq.IQueryable'的可访问性低于property'SportsStore.Domain.Abstract.IProductRepository.Products'

我只想尝试第7章的第一部分,其中action方法依赖于存储库接口的模拟实现,它生成一些简单的测试数据。 这是我的代码

NinjectControllerFactory.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;

namespace 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()
        {
            // put additional bindings here
            // Mock implementation of the IProductRepository Interface
            Mock<IProductRepository> mock = new Mock<IProductRepository>();
            mock.Setup(mock => 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);
        }

    }

}

Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System

.Web.Routing;
using WebUI.Infrastructure;

namespace WebUI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Product", action = "List", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
        }
    }
}

Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace SportsStore.Domain.Entities

{
    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; }
    }
}

IProductRepository.cs
using System.Linq;
using SportsStore.Domain.Entities;



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

ProuctController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;

namespace WebUI.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        private IProductRepository repository;

        public ProductController(IProductRepository productRepository){

        repository = productRepository;

        }

            public ViewResult List() {
            return View(repository.Products);
           }       
     }


}

List.cshtml
@model IEnumerable<SportsStore.Domain.Entities.Product>

@{
    ViewBag.Title = "Products";
}

@foreach (var p in Model)
{
    <div class= "item">
        <h3>@p.Name</h3>
        @p.Description
        <h4>@p.Price.ToString("c")</h4>
    </div>
}

Assemblyinfo.cs
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Domain")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("Domain")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2011")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")]

// Version information for an assembly consists of the following four values:
//
//      Major Version
//      Minor Version 
//      Build Number
//      Revision
//
// You can specify all the values or you can default the Build and Revision Numbers 
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

IProductRepository.cs    
using System.Linq;
using SportsStore.Domain.Entities;



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

6 个答案:

答案 0 :(得分:4)

让您的产品类公开

public class Product { 
   blah blah blah 
}

答案 1 :(得分:1)

我没有这本书,但让我们看一下它说的内容 - 获取访问器是不可访问的。公共或私人财产也是如此?没有我猜测的代码,但确实听起来你正在尝试访问未明确标记为public的属性。

答案 2 :(得分:1)

访问修饰符。某处未指定或私有。

IProductRepository必须从IDisposable实现。与ObjectContext一样

答案 3 :(得分:1)

他们在不同的项目中。因此,在当前项目中添加对SportsStore.Domain项目的引用(右键单击项目 - >添加引用 - >项目选项卡)。你将获得可见性。

答案 4 :(得分:0)

好书。到目前为止它对我有用.... 你错过了具体的课程“EFProductRepository”吗?

using System.Linq;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;

namespace SportsStore.Domain.Concrete 
{
    public class EFProductRepository : IProductRepository 
    {
        private EFDbContext context = new EFDbContext();

        public IQueryable<Product> Products 
        {
            get { return context.Products; }
        }
    }
}

答案 5 :(得分:0)

我有同样的编译问题。我通过将Product类设置为Public来修复它。