【问题标题】:How to use IService in architecture with UnitOfWork and Repository in ASP.NET MVC?如何在 ASP.NET MVC 中通过 UnitOfWork 和 Repository 在架构中使用 IService?
【发布时间】:2016-09-15 09:45:04
【问题描述】:

我使用工作单元和存储库实现了一个正常工作的架构。 我有一个名为 Product 的实体,我在控制器中使用 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);
    }

}

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

公共接口 IProductService:ServiceBase

我不想再写这些方法了。

【问题讨论】:

  • 请分享您尝试执行的 ServiceBase 代码。您的问题太宽泛,无法回答。
  • @Bygonaut 我说的是例子,我没有ServiceBase代码,我需要你说我怎么写。
  • 有什么问题?

标签: asp.net-mvc design-patterns repository-pattern


【解决方案1】:

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

假设你的意思是让你的接口实现一个抽象类:

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

Similar question

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

谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多