Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 装饰器模式-向接口添加方法_C#_Decorator - Fatal编程技术网

C# 装饰器模式-向接口添加方法

C# 装饰器模式-向接口添加方法,c#,decorator,C#,Decorator,我目前正在努力实现一个IUnitOfwork。 假设我有一个接口,它有两种方法: public interface IRepository<TEntity, in TKey> { TEntity Get(TKey id); IQueryable<TEntity> All(); } 有谁知道我如何解决这个问题吗?我认为克里希纳德胡加纳提出的解决方案很好。这是我理解的一个例子,以简单班学生为例。构建存储库和测试是否将注入到实现方法中是很容易的 publi

我目前正在努力实现一个IUnitOfwork。 假设我有一个接口,它有两种方法:

public interface IRepository<TEntity, in TKey>
{

    TEntity Get(TKey id);

    IQueryable<TEntity> All();
}

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

我认为克里希纳德胡加纳提出的解决方案很好。这是我理解的一个例子,以简单班学生为例。构建存储库和测试是否将注入到实现方法中是很容易的

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();
}   
公共班级学生
{
国际无党派人士;
int NoOfHangOvers;
}
公共接口IRepo
{
IEnumerable GetAll();
T GetByID();
}
公共接口IRepoCreate
{
Int32创建();
}
公共接口IRepoDelete
{
作废删除();
}
公共接口是udentrepo:IRepo、IRepoCreate、IRepoDelete
{
IEnumerable GetAll();
学生GetByID();
int Create();
作废删除();
学生聚会();
}
公共类MSSQLStudentRepo:IStudentRepo
{
public IEnumerable GetAll(){\\stuff}
公立学生GetByID(){\\stuff}
public int Create(){\\stuff}
public void Delete(){\\stuff}
公立学生GetByParty(){\\stuff}
} 
公共类MySQLStudentRepo:IStudentRepo
{
public IEnumerable GetAll(){\\stuff}
公立学生GetByID(){\\stuff}
public int Create(){\\stuff}
public void Delete(){\\stuff}
公立学生GetByParty(){\\stuff}
}
public void实现示例()
{
IStudentRepo Repo=新的MSSQLStudentRepo();
var Bob=Repo.GetByParty();
}   

在decorator模式中,其他方法可能不会出现在图片中。装饰器模式意味着方法将保持不变,但行为可能会改变。我正在写一篇关于它的博客,一旦完成,我会更新这个回复

下面的代码可能会对您有所帮助,因为所有函数都可以很容易地放入一个sigle类中,您可以将其转换为相应的returnrepsitory

using System.Linq;

public interface IRepository<TEntity, in TKey>
{

    TEntity Get(TKey id);

    IQueryable<TEntity> All();
}

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

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

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


public class Wrapper
{
    private readonly CombinedRepository standardRepository = new CombinedRepository();

    public IRepository Repository
    {
        get { return standardRepository; }
    }

    public ICreateRepository CreateRepository
    {
        get { return standardRepository; }
    }

    public IDeleteRepository DeleteRepository
    {
        get { return standardRepository; }
    }
}

public class CombinedRepository : IRepository, IDeleteRepository, ICreateRepository
{
    public void GetAll()
    {
    }

    public void GetById(int id)
    {
    }

    public void Delete()
    {
    }

    public void Create()
    {
    }
}
使用System.Linq;
公共接口假定
{
TEntity-Get(TKey-id);
IQueryable All();
}
公共接口假定
{
void GetAll();
无效GetById(int-id);
}
公共接口IDeleteRepository:IRepository
{
作废删除();
}
公共接口存储库:IRepository
{
void Create();
}
公共类包装器
{
private readonly CombinedRepository standardRepository=新建CombinedRepository();
公共电子存储库
{
获取{return standardRepository;}
}
公共ICreateRepository CreateRepository
{
获取{return standardRepository;}
}
公共IDeleteRepository删除存储库
{
获取{return standardRepository;}
}
}
公共类组合存储库:IRepository、IDeleteRepository、ICreateRepository
{
public void GetAll()
{
}
public void GetById(int id)
{
}
公共作废删除()
{
}
公共void Create()
{
}
}

我认为
抽象类对于需要模拟的存储库非常有效,可以使用
虚拟
实现,而不是根据需要进行重写

public interface IRepository<TEntity, in TKey>
{
    TEntity Get(TKey id);
    TEntity GetById(int id);
    IQueryable<TEntity> All();
}

public abstract class Repository : IRepository
{
    public virtual TEntity Get(TKey id) { throw new NotImplementedException(); }
    public virtual TEntity GetById(int id) { throw new NotImplementedException(); }
    public virtual IQueryable<TEntity> All() { throw new NotImplementedException(); }
}

public class FullRepo : Repository
{
    public virtual TEntity Get(TKey id) { /*Implement it!*/ }
    public virtual TEntity GetById(int id) { /*Implement it!*/ }
    public virtual IQueryable<TEntity> All() { /*Implement it!*/ }
}

// No GetById here
public class PartialRepo : Repository
{
    public virtual TEntity Get(TKey id) { /*Implement it!*/ }
    public virtual TEntity GetById(int id) { throw new NotSupportedException(); }
    public virtual IQueryable<TEntity> All() { /*Implement it!*/ }
}

传递
true
会使方法的调用成为编译错误(VS会说它是“过时的”,所以它不是完全正确的,但它不会让方法被无条件调用)。

接口继承呢?我更新了我原来的问题,使用接口继承,但现在这意味着,如果我需要一个存储库,而不是GetAll()、GetById()、Create(),那么我需要创建一个名为“DeleteCreateRepository将实现IDeleteRepository和ICreateRepository。你明白我的意思了吗?为什么不使用扩展方法来扩展接口呢?基本上,在存储库模式中,非常通用的接口应该具有所有基本方法,这些方法可以由类实现,也可以由其他接口继承。比如说,IRepository是最通用的一个,CustomerRepository可以从IRepository继承所有常见操作。关于工作单元,它通常涉及诸如Save之类的提交逻辑。感谢您提供的信息。这正是我目前使用的方法。我不喜欢这种方法,因为我会有很多方法抛出NotSupportedException()。你也可以/相反地将它们标记为过时,以实现“这不起作用”的目标。扩展答案以符合要求。仍在等待您的博文:D
using System.Linq;

public interface IRepository<TEntity, in TKey>
{

    TEntity Get(TKey id);

    IQueryable<TEntity> All();
}

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

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

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


public class Wrapper
{
    private readonly CombinedRepository standardRepository = new CombinedRepository();

    public IRepository Repository
    {
        get { return standardRepository; }
    }

    public ICreateRepository CreateRepository
    {
        get { return standardRepository; }
    }

    public IDeleteRepository DeleteRepository
    {
        get { return standardRepository; }
    }
}

public class CombinedRepository : IRepository, IDeleteRepository, ICreateRepository
{
    public void GetAll()
    {
    }

    public void GetById(int id)
    {
    }

    public void Delete()
    {
    }

    public void Create()
    {
    }
}
public interface IRepository<TEntity, in TKey>
{
    TEntity Get(TKey id);
    TEntity GetById(int id);
    IQueryable<TEntity> All();
}

public abstract class Repository : IRepository
{
    public virtual TEntity Get(TKey id) { throw new NotImplementedException(); }
    public virtual TEntity GetById(int id) { throw new NotImplementedException(); }
    public virtual IQueryable<TEntity> All() { throw new NotImplementedException(); }
}

public class FullRepo : Repository
{
    public virtual TEntity Get(TKey id) { /*Implement it!*/ }
    public virtual TEntity GetById(int id) { /*Implement it!*/ }
    public virtual IQueryable<TEntity> All() { /*Implement it!*/ }
}

// No GetById here
public class PartialRepo : Repository
{
    public virtual TEntity Get(TKey id) { /*Implement it!*/ }
    public virtual TEntity GetById(int id) { throw new NotSupportedException(); }
    public virtual IQueryable<TEntity> All() { /*Implement it!*/ }
}
// No GetById here
public class PartialRepo : Repository
{
    public virtual TEntity Get(TKey id) { /*Implement it!*/ }
    [Obsolete("This method can't be used by this repository", true)]
    public virtual TEntity GetById(int id) { /*Can be empty*/ }
    public virtual IQueryable<TEntity> All() { /*Implement it!*/ }
}