gpt4 book ai didi

asp.net-mvc - 如何在 ASP.NET MVC 中使用 UnitOfWork 和 Repository 架构中的 IService?

转载 作者:行者123 更新时间:2023-12-05 06:42:24 33 4
gpt4 key购买 nike

我使用 unitofwork 和 Repository 实现了一个可以正常工作的架构。我有一个名为 Product 的实体,我在 Controller 中使用 ProductService 进行 CRUD 操作。

 public interface IProductService
{
IList<Product> GetAll();
Product GetById(int id);
void Create(Product product);
void Update(Product product);
void Delete(int id);
}

public class ProductService : IProductService
{
private IRepository<Product> _productRepository;

public ProductService(IRepository<Product> productRepository)
{
_productRepository = productRepository;
}

public IList<Product> GetAll()
{
return _productRepository
.GetAll()
.ToList();
}

public Product GetById(int id)
{
return _productRepository.GetById(id);
}

public void Create(Product product)
{
_productRepository.Create(product);
}

public void Update(Product product)
{
_productRepository.Update(product);
}

public void Delete(int id)
{
_productRepository.Delete(id);
}

}

现在我写了一个包含所有方法的服务基础并写下:

公共(public)接口(interface) IProductService:ServiceBase

我不想再写这些方法了。

最佳答案

首先,这个话题似乎根本没有任何具体的问题。

假设您的意思是让您的接口(interface)实现一个抽象类:

你有什么理由不只使用抽象类吗?您可以使用默认方法创建一个抽象类,并且可以将先前包含在您的接口(interface)中的方法声明为抽象方法。你可以在那里看到一个这样的例子:

Similar question

如果这不能回答您的问题,请提供更多信息,以便我们了解您在此尝试实现的目标。

谢谢。

关于asp.net-mvc - 如何在 ASP.NET MVC 中使用 UnitOfWork 和 Repository 架构中的 IService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37307509/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com