In a node with generics

I have the following domain object:

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

Repository Interface:

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

Implementation 2:

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

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

    }
}

So, adding another method that is not inside the IRepository.

I want to use the repository as an IRepository, while any type can be above it.

A small bonus: I write this for small systems with very few domain objects. I do not want to create anything that uses IoC, but rather something that is easy and simple to consume.

thanks

+3
source share
2 answers

Not quite sure what you want, but something like this compiles:

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);
}
+3
source

In your DomainObject implementation, only one generic type argument is specified instead of two. Why is this not so:

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

, , , ?

+4

Source: https://habr.com/ru/post/1720280/


All Articles