C#:方法的通用实现不满足接口

发布于 2024-09-16 20:55:13 字数 827 浏览 3 评论 0原文

这个 帖子我谈到了使用通用基类使我能够创建存储库类,而无需重复加载基本的管道代码。

每个存储库都可以通过接口访问。在下面的代码中,为了简洁起见,我将仅显示其中一种方法:

接口:

IQueryable<Suggestion> All { get; }

通用基类

public IQueryable<T> All
    {
      get { return _unitOfWork.GetList<T>(); }
    }

具体类(实现接口并扩展通用基类)

public IQueryable<Suggestion> All
    {
      get { return _unitOfWork.GetList<Suggestion>(); }
    }

我预计我能够简单地从具体类中剥离该方法,并且编译器将使用通用基类实现来代替并计算出旨在满足接口的方法。但不!

当我删除该方法时,我收到旧的“未实现接口成员”错误。

如果我做不到这一点,我使用通用基类的努力不是毫无意义吗?或者有办法解决这个问题吗?

In this post I talked about using a generic base class to enable me to create repository classes without duplicating loads of basic plumbing code.

Each Repository is accessed through an interface. In the code below, I will only show one of the methods for the sake of brevity:

Interface:

IQueryable<Suggestion> All { get; }

Generic base class

public IQueryable<T> All
    {
      get { return _unitOfWork.GetList<T>(); }
    }

Concrete class (implements the interface and extends the generic base class)

public IQueryable<Suggestion> All
    {
      get { return _unitOfWork.GetList<Suggestion>(); }
    }

I anticipated that I would be able to simply strip the method out of the concrete class, and the compiler would use the generic base class implementation instead and work out that was intended to satisfy the interface. But no!

When I strip the method out I get the old 'does not implement interface member' error.

If I can't do this, have my efforts to use a generic base class not been pointless? Or is there a way around this?

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

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

发布评论

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

评论(2

唐婉 2024-09-23 20:55:13

您可以使接口本身通用,然后在具体类中实现类型化版本吗?

public interface IRepository<T>
{
    List<T> All { get; }
}

public class Repository<T>
{
      public List<T> All 
      {
          get { return new List<T>(); }
      }
}

public class SuggestionRepository : Repository<Suggestion>, IRepository<Suggestion>
{ }

我仍然建议使用通用接口,因为它可以让您免于重复,但这也有效。

public interface ISuggestionRepository
{
    List<Suggestion> All { get; }
}

public class Repository<T>
{
      public List<T> All 
      {
          get { return new List<T>(); }
      }
}

public class SuggestionRepository : Repository<Suggestion>, ISuggestionRepository
{ }

Can you make the interface itself generic then implement a typed version in your concrete class?

public interface IRepository<T>
{
    List<T> All { get; }
}

public class Repository<T>
{
      public List<T> All 
      {
          get { return new List<T>(); }
      }
}

public class SuggestionRepository : Repository<Suggestion>, IRepository<Suggestion>
{ }

I'd still suggest using the generic interface since it will save you from repeating yourself, but this works too.

public interface ISuggestionRepository
{
    List<Suggestion> All { get; }
}

public class Repository<T>
{
      public List<T> All 
      {
          get { return new List<T>(); }
      }
}

public class SuggestionRepository : Repository<Suggestion>, ISuggestionRepository
{ }
请叫√我孤独 2024-09-23 20:55:13

使用 virtual 关键字并将您的接口放在具体的实现上。

public interface IMyInterface<T>
{
    IQueryable<T> All { get; }
}

public abstract class MyBaseClass<T> : IMyInterface<T>
{
    public virtual IQueryable<T> All
    {
        get { return _unitOfWork.GetList<T>(); ; }
    }
}

public class MyClass : MyBaseClass<Suggestion>, IMyInterface<Suggestion>
{

}

Use the virtual keyword and put your interface on your concrete implementation..

public interface IMyInterface<T>
{
    IQueryable<T> All { get; }
}

public abstract class MyBaseClass<T> : IMyInterface<T>
{
    public virtual IQueryable<T> All
    {
        get { return _unitOfWork.GetList<T>(); ; }
    }
}

public class MyClass : MyBaseClass<Suggestion>, IMyInterface<Suggestion>
{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文