How to implement a Generic object of the interface?

There is a class:
public class Repository<t> where T : IDbUpdatable<t>
{
 }</t></t>


There are interface:
public interface IDbUpdatable<t>
{
 Repository<t> GetRepository();
 int Update();
 int Delete();
 }</t></t>


Actually, the idea is this - Repository should work with any type that implements IDbUpdatable. Accordingly, a class that implements IDbUpdatable should return a link to your Repository via GetRepository

The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Repository'. There is no boxing conversion or type parameter conversion from 'T' to '.IDbUpdatable'.

I do not quite understand that the compiler does not like.
June 10th 19 at 14:52
4 answers
June 10th 19 at 14:54
Solution
public class Repository<t> where T : IDbUpdatable<t>
{
}

public interface IDbUpdatable<t> where T : IDbUpdatable<t>
{
 Repository<t> GetRepository();
 int Update();
 int Delete();
}</t></t></t></t></t>
This kind of recursive dependence. Interface-the generic type that the interface itself generic form of - glenna_Hauck commented on June 10th 19 at 14:57
make it simple for yourself IDbUpdatable< T > to some abstract A, it will, we'll just say that a class that implements some interface A can only return repositories that implement this interface A. - Adriel51 commented on June 10th 19 at 15:00
Yes, that's right. Thank you! - Norberto commented on June 10th 19 at 15:03
June 10th 19 at 14:56
Solution
The type 'T' cannot be used as type parameter 'T' in the generic type or method 'Repository'. There is no boxing conversion or type parameter conversion from 'T' to '.IDbUpdatable'.


Type 'T' must be IDbUpdatable<t></t>, then this error will not.

public interface IDbUpdatable<t> where T : IDbUpdatable<t>
{
 Repository<t> GetRepository();
 int Update();
 int Delete();
 }</t></t></t>
Nothing that an hour before you I already gave this answer? - glenna_Hauck commented on June 10th 19 at 14:59
June 10th 19 at 14:58
In the interface I see some looped stuff does not work in the head correct.
You write the repository with a type T where T implements the interface IDbUpdatable despite the fact that the interface itself is typed from T(i.e. Repository<idbupdatable<idbupdatable<...<...<>>>></idbupdatable<idbupdatable<...<...<>, etc.). He himself is somewhere fails and how it is then correct I do not understand.
June 10th 19 at 15:00
At Your option:
public interface IRepository<t>
{
 T Create();
 T Read();
 T Update();
 T Delete();
}

 public class Repository<t> where T : IRepository<t>
{
 public static Repository<t> CreateRepository()
{
 return new Repository<t>();
}
 }</t></t></t></t></t>

But don't think that's the right approach. Because in this case, each POTENTIAL model will need to inherit from IRepository. Architectural not right, will look as follows
class Program
{
 static void Main(string[] args)
{
 var repository = Repository<comment>.CreateRepository();

Console.ReadKey();
}
}

public class Comment : IRepository<comment>
{
 public int Id { get; set; }
 public string Name { get; set; }

 public Comment Create()
{
 return this;
}

 public Comment Read()
{
 return this;
}

 public Comment Update()
{
 return this;
}

 public Comment Delete()
{
 return this;
}
}</comment></comment>


If you want a generic repository, you can offer your own version, which I used in some of their projects. If you wish, adapt it to your needs.
Example
public interface IRepository where T : class
{
IEnumerable GetAll();
T GetById(int id);
T Create(T model);
T Update(T model);
T Delete(int id);
}

public class EFRepository : IRepository where T : class
{
protected readonly DbContext _dbContext;

public EFRepository(DbContext dbContext)
{
_dbContext = dbContext;
}

public virtual IEnumerable GetAll()
{
return _dbContext.Set();
}

public virtual T GetById(int id)
{
return _dbContext.Set().Find(id);
}

public virtual T Create(T model)
{
var entity = _dbContext.Set().Add(model);
return entity;
}

public virtual T Update(T model)
{
_dbContext.Entry(model);
return model;
}

public virtual T Delete(int id)
{
var model = _dbContext.Set().Find(id);
var entity = _dbContext.Set().Remove(model);
return entity;
}
}

public interface IPostRepository : IRepository
{
IEnumerable GetByUserId(string id);
}

public class EFPostRepository : EFRepository, IPostRepository
{
public EFPostRepository(DbContext dbContext) : base(dbContext)
{
}

public IEnumerable GetByUserId(string id)
{
return _dbContext
.Posts
.Where(x => x.UserId == id);
}
}

Find more questions by tags C#