c# - 装饰者模式 - 向接口(interface)添加方法

标签 c# decorator

我目前正在努力实现 IUnitOfwork。 假设我有一个包含 2 个方法的接口(interface):

public interface IRepository<TEntity, in TKey>
{

    TEntity Get(TKey id);

    IQueryable<TEntity> All();
}

现在,假设我有几个实现此接口(interface)的类(实体)。 但是,某些实体可能需要具有其他方法来查询,例如 GetById(int id)。

这可以通过创建一个名为 IRepositoryWithGetById 的新接口(interface)轻松解决

public interface IRepository<TEntity, in TKey>
{

    TEntity Get(TKey id);

    TEntity GetById(int id);

    IQueryable<TEntity> All();
}

如果这将导致维护代码的噩梦。 我在考虑使用装饰器模式,但我没有找到很好的解决方案。

注意:我正在使用接口(interface),因为我应该能够模拟它。

根据用户的建议,我正在使用接口(interface)继承,因此这是更新后的代码:

public class Wrapper
{
    public IRepository standardRepository = new Repository();
    public IDeleteRepository deleteRepository = new DeleteRepository();
    public ICreateRepository createRepository = new CreateRepository();    
}

public class Repository : IRepository
{
    public void GetAll() { }
    public void GetById(int id) { }
}

public class DeleteRepository : Repository, IDeleteRepository
{
    public void Delete() { }
}

public class CreateRepository : Repository, ICreateRepository
{
    public void Create() { }
}

public interface IRepository
{
    void GetAll();
    void GetById(int id);
}

public interface IDeleteRepository : IRepository
{
    void Delete();
}

public interface ICreateRepository : IRepository
{
    void Create();
}

有人知道我该如何解决这个问题吗?

最佳答案

我认为 KrishnaDhungana 建议的解决方案效果很好。这是我理解的一个例子,以简单的类(class)学生为例。很容易构建存储库并测试您是否注入(inject)到您的实现方法中......

public class Student
{
    int NoOfParties;
    int NoOfHangOvers;
}

public interface IRepo<T>
{
    IEnumerable<T> GetAll();
    T GetByID();
}

public interface IRepoCreate<T>
{
    Int32 Create();
}

public interface IRepoDelete<T>
{
    void Delete();
}

public interface IStudentRepo : IRepo<Student>, IRepoCreate<Student>, IRepoDelete<Student>
{
    IEnumerable<Student> GetAll();
    Student GetByID();
    int Create();
    void Delete();
    Student GetByParty();
}

public class MSSQLStudentRepo : IStudentRepo
{
    public IEnumerable<Student> GetAll() { \\stuff }
    public Student GetByID() { \\stuff }
    public int Create() { \\stuff }
    public void Delete() { \\stuff }
    public Student GetByParty() { \\stuff }
} 

public class MySQLStudentRepo : IStudentRepo
{
    public IEnumerable<Student> GetAll() { \\stuff }
    public Student GetByID() { \\stuff }
    public int Create() { \\stuff }
    public void Delete() { \\stuff }
    public Student GetByParty() { \\stuff }
}

public void ImplementationExample()
{
    IStudentRepo Repo = new MSSQLStudentRepo();
    var Bob = Repo.GetByParty();
}   

关于c# - 装饰者模式 - 向接口(interface)添加方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24405611/

相关文章:

c# - File.AppendAllText 与 StreamWriter

javascript - TypeScript 中是否可以有通用的装饰器,可以根据其输入/输出类型进行链接?

python - 装饰方法赋值前引用的局部变量

c# - 为什么我无法从资源中获取自定义字体?

c# - 不调用 return 直接跳转到 finally

c# - 在 C# 中对这种行为的解释是什么

c# - 非静态类中的私有(private)静态变量是否可用于整个应用程序?

design-patterns - 为什么装饰器设计模式的名称为 "Decorator"?

java - IO 的 GoF 装饰器模式用例和示例

javascript - @Get 装饰器在 node.js 中无法正常工作