EDIT
我是否应该将服务层和存储库层放入一个项目中,以便 Web 项目能够引用 DbContext 对象?现在我的网络(控制器)无法引用 dbcontext 对象。什么是正确的方法?
// service and repository are together
(View <- Controller) -> (Service -> Repository -> EF DbContext) -> (DB)
// separate service and repository layer
(View <- Controller) -> (Service) -> (Repository -> EF DbContext) -> (DB)
以下是原始问题
我知道 SO 是一个很好的社区,可以发布有关 MVC 设计模式的问题。请给我您的建议,我将感谢您的帮助。谢谢你!
我们正在计划一个新项目,我们的首要任务是开发一个可扩展且松散耦合的应用程序。
我是软件开发新手;我读了一些书MVC 音乐商店教程 http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-1,然后是一本书叫Pro ASP.NET MVC 3 框架,作者:Steven Sanderson (Apress) https://rads.stackoverflow.com/amzn/click/com/1430234040,从书中,我了解了DDD(领域驱动设计)以及其他一些概念,例如存储库和依赖注入。我按照这本书搭建了SportsStore网站,对DI有了一些基本的了解。但我个人认为这个例子没有分离业务逻辑层,所以我对此进行了研究,我发现了一种称为服务层模式的模式,根据我的理解,它分离了业务逻辑层。基于此,我为我的新项目制定了一个结构(下面的示例项目)。
我需要实施吗I一次性界面?如果是,在哪里以及为什么?
这种结构对于规模较大的项目是否可行?
数据库设计示例:产品(一个)----(多个)产品类别Rs(多个)----(一个)类别
该解决方案包含3个项目:Repository、Service、Web
存储库:
定义IRepository接口,基本CRUD操作
这些签名足够吗?我应该添加TEntity GetById(对象id);?
public interface IRepository<TEntity>
{
IQueryable<TEntity> All { get; }
void Create(TEntity item);
void Update(TEntity item);
void Delete(TEntity item);
void SaveChanges();
}
实现通用存储库类
public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
STOREEntities context;
public Repository()
{
context = new STOREEntities();
}
public IQueryable<TEntity> All
{
get
{
return context.Set<TEntity>();
}
}
public void Create(TEntity item)
{
context.Set<TEntity>().Add(item);
}
public void Update(TEntity item)
{
context.Entry<TEntity>(item).State = System.Data.EntityState.Modified;
}
public void Delete(TEntity item)
{
context.Set<TEntity>().Remove(item);
}
public void SaveChanges()
{
context.SaveChanges();
}
}
服务:
定义IProductService接口,在这里扩展业务逻辑。
public interface IProductService
{
IEnumerable<Product> Products { get; }
IEnumerable<Product> Get(Expression<Func<Product, Boolean>> filter);
Product GetByProductId(int productId);
void AddProduct(Product product);
void EditProduct(Product product);
void RemoveProduct(Product product);
void SaveChanges();
}
实施产品服务
public class ProductService : IProductService
{
IRepository<Product> repository; //Inject
public ProductService(IRepository<Product> repo)
{
repository = repo;
}
public IEnumerable<Product> Products
{
get { return repository.All; }
}
public IEnumerable<Product> Get(Expression<Func<Product, bool>> filter)
{
return repository.All.Where(filter);
}
public Product GetByProductId(int productId)
{
return repository.All.SingleOrDefault(p => p.ProductID == productId);
}
public void AddProduct(Product product)
{
repository.Create(product);
}
public void EditProduct(Product product)
{
repository.Update(product);
}
public void RemoveProduct(Product product)
{
repository.Delete(product);
}
public void SaveChanges()
{
repository.SaveChanges();
}
}
Web项目,从服务中检索数据并转换为视图模型并显示。
产品控制器代码
public class ProductController : Controller
{
IProductService productService; //inject
public ProductController(IProductService service)
{
productService = service;
}
public ActionResult Index()
{
var products = productService.Products; //retrieve from service layer
return View(products);
}
}