加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 与Generics结合

发布时间:2020-12-16 01:32:43 所属栏目:百科 来源:网络整理
导读:我有以下域对象: public class DomainObjectT,TRepo where T : DomainObjectT where TRepo : IRepositoryT{ public static TRepo Repository { get;private set; }} 存储库接口: public interface IRepositoryT //where T : DomainObjectT // The catch 22
我有以下域对象:

public class DomainObject<T,TRepo> 
  where T : DomainObject<T>
  where TRepo : IRepository<T>
{
      public static TRepo Repository { get;private set; }
}

存储库接口:

public interface IRepository<T> //where T : DomainObject<T> // The catch 22
{
    void Save(T domainObject);
}

2的实现:

public class User : DomainObject<User,MyRepository>
{
    public string Name { get;private set;}
}

public class MyRepository : IRepository<User>
{
    public List<User> UsersWithNameBob()
    {

    }
}

所以添加另一个不在IRepository中的方法.

我想将存储库强制为IRepository,而在它上面它可以是任何类型.

一个小旁注:我正在为很少有域对象的小型系统编写这个.我不打算创建任何使用IoC的东西,而是简单易用的东西.

谢谢

解决方法

不完全确定你想要什么,但这样的东西编译:

public class DomainObject<T,TRepo> 
     where T: DomainObject<T,TRepo> 
     where TRepo: IRepository<T,TRepo>
{
     public static TRepo Repository
     {
         get;
         private set; 
     }
}

public interface IRepository<T,TRepo>
     where T: DomainObject<T,TRepo>
     where TRepo: IRepository<T,TRepo>
{
     void Save(T domainObject);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读