Я пытаюсь использовать шаблон репозитория для работы с Entity Framework.
В моем модельном домене под названием SportsStore.domain у меня есть три важных файла: Entities/Product.cs, Concrete/EFDbContext.cs, Concrete/EFProductRepository.cs и Abstract/IProductsRepository.cs
IProductsRepository — это интерфейс, который реализует EFProductRepository.
IProductsRepository:
Код: Выделить всё
using SportsStore.domain.Entities;
namespace SportsStore.domain.Abstract
{
public interface IProductRepository
{
IEnumerable
Products { get; }
}
}
Код: Выделить всё
using SportsStore.domain.Entities;
namespace SportsStore.domain.Concrete
{
//Associate the model with the database
//This class then automatically defines a property for each table in the database that I want to work with.
public class EFDbContext : DbContext {
public DbSet
Products { get; set; }
}
}
Код: Выделить всё
using SportsStore.domain.Abstract;
using SportsStore.domain.Entities;
namespace SportsStore.domain.Concrete
{
public class EFProductRepository : IProductRepository
{
private EFDbContext context = new EFDbContext();
public IEnumerable
Products
{
get { return context.Products; }
}
}
}
Код: Выделить всё
kernel.Bind().To();
Код: Выделить всё
using SportsStore.domain.Abstract;
using SportsStore.domain.Entities;
namespace SportsStore.WebUI.Controllers
{
public class ProductController : Controller
{
private IProductRepository repository;
//Declar the dependency on IProductRepository
public ProductController(IProductRepository productRepository)
{
this.repository = productRepository;
}
// GET: Product
public ViewResult List()
{
return View(repository.Products.ToList());
}
}
}
Вот мое мнение:
Код: Выделить всё
@using SportsStore.domain.Entities
@model IEnumerable
@{
ViewBag.Title = "Products";
}
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.Description);
@Html.DisplayFor(modelItem => item.Price);
}
Код: Выделить всё
Как решить проблему? Где мне смотреть?
Подробнее здесь: https://stackoverflow.com/questions/332 ... -framework
Мобильная версия