IRepository 与 IRepository

发布于 2024-11-02 22:03:15 字数 1845 浏览 4 评论 0原文

我试图为我的所有存储库实现定义一个特定的接口,而不仅仅是这样:

public abstract class GeneralizedRepository
{
    readonly IDataModel _Model;

    public GeneralizedRepository(IDataModel Model) {
        if (Model == null)
            throw new NullReferenceException();
        _Model = Model;
    }

    public IDataModel DataModel { get { return _Model; } }

    public abstract IEnumerable<T> GetAll<T>();
    public abstract T GetOne<T>(Func<T, bool> predicate);

    public abstract bool Contains<T>(Func<T, bool> predicate);
    public abstract void Add<T>(T entity);
    public abstract void Update<T>(T entity);
    public abstract bool Remove<T>(Func<T, bool> predicate);
}

这是一个类,因为我的派生存储库共有实例 DataModel

现在我有具体的实现:

public class DetailRep : GeneralizedRepository
{
    public DetailRep(IDataModel Model) : base(Model) { }
    public DetailRep(UnitOfWork Unit) : base(Unit.Model) { }

    public override IEnumerable<T> GetAll<T>() {
        throw new NotImplementedException();
    }

    public override T GetOne<T>(Func<T, bool> predicate) {
        throw new NotImplementedException();
    }

    public override bool Contains<T>(Func<T, bool> predicate) {
        throw new NotImplementedException();
    }

    public override void Add<T>(T entity) {
        throw new NotImplementedException();
    }

    public override void Update<T>(T entity) {
        throw new NotImplementedException();
    }

    public override bool Remove<T>(Func<T, bool> predicate) {
        throw new NotImplementedException();
    }
}

但它是一个 详细存储库,我的意思是我想替换 Detail 类型的所有 T

但在编译时会抛出错误:

类型参数声明必须是标识符而不是类型

I'm trying to define one specific interface for all my repositories implement, than is just this:

public abstract class GeneralizedRepository
{
    readonly IDataModel _Model;

    public GeneralizedRepository(IDataModel Model) {
        if (Model == null)
            throw new NullReferenceException();
        _Model = Model;
    }

    public IDataModel DataModel { get { return _Model; } }

    public abstract IEnumerable<T> GetAll<T>();
    public abstract T GetOne<T>(Func<T, bool> predicate);

    public abstract bool Contains<T>(Func<T, bool> predicate);
    public abstract void Add<T>(T entity);
    public abstract void Update<T>(T entity);
    public abstract bool Remove<T>(Func<T, bool> predicate);
}

This is a class because my derived repositories have in common the instance DataModel

Now I have concrete implementations as:

public class DetailRep : GeneralizedRepository
{
    public DetailRep(IDataModel Model) : base(Model) { }
    public DetailRep(UnitOfWork Unit) : base(Unit.Model) { }

    public override IEnumerable<T> GetAll<T>() {
        throw new NotImplementedException();
    }

    public override T GetOne<T>(Func<T, bool> predicate) {
        throw new NotImplementedException();
    }

    public override bool Contains<T>(Func<T, bool> predicate) {
        throw new NotImplementedException();
    }

    public override void Add<T>(T entity) {
        throw new NotImplementedException();
    }

    public override void Update<T>(T entity) {
        throw new NotImplementedException();
    }

    public override bool Remove<T>(Func<T, bool> predicate) {
        throw new NotImplementedException();
    }
}

But it is a Detail Repository, what i mean is that i want replace all T's for the type Detail

But an error is thrown at compile time:

Type parameter declaration must be an identifier not a type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡大本事 2024-11-09 22:03:15

将您的抽象类声明为泛型:

public abstract class GeneralizedRepository<T>
{
    readonly IDataModel _model;
    public GeneralizedRepository(IDataModel model)
    {
        if (model == null)
            throw new NullReferenceException();
        _model = model;
    }
    public IDataModel DataModel { get { return _model; } }
    public abstract IEnumerable<T> GetAll();
    public abstract T GetOne(Func<T, bool> predicate);
    public abstract bool Contains(Func<T, bool> predicate);
    public abstract void Add(T entity);
    public abstract void Update(T entity);
    public abstract bool Remove(Func<T, bool> predicate);
}

然后在派生类上用您的详细信息填写类型:

public class DetailRep  : GeneralizedRepository<Detail>
{
    public DetailRep(IDataModel model)
        : base(model)
    {
    }
    public override IEnumerable<Detail> GetAll(){}
    public override Detail GetOne(Func<Detail, bool> predicate){}
    public override bool Contains(Func<Detail, bool> predicate){}
    public override void Add(Detail entity){}
    public override void Update(Detail entity){}
    public override bool Remove(Func<Detail, bool> predicate){}
}

Declare your abstract class as a generic:

public abstract class GeneralizedRepository<T>
{
    readonly IDataModel _model;
    public GeneralizedRepository(IDataModel model)
    {
        if (model == null)
            throw new NullReferenceException();
        _model = model;
    }
    public IDataModel DataModel { get { return _model; } }
    public abstract IEnumerable<T> GetAll();
    public abstract T GetOne(Func<T, bool> predicate);
    public abstract bool Contains(Func<T, bool> predicate);
    public abstract void Add(T entity);
    public abstract void Update(T entity);
    public abstract bool Remove(Func<T, bool> predicate);
}

And than on your derived class fill in the Type with your Detail:

public class DetailRep  : GeneralizedRepository<Detail>
{
    public DetailRep(IDataModel model)
        : base(model)
    {
    }
    public override IEnumerable<Detail> GetAll(){}
    public override Detail GetOne(Func<Detail, bool> predicate){}
    public override bool Contains(Func<Detail, bool> predicate){}
    public override void Add(Detail entity){}
    public override void Update(Detail entity){}
    public override bool Remove(Func<Detail, bool> predicate){}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文