Declaring interface members with generic return types: "The type parameter [generic type] cannot be used with type arguments"
I'm trying to write an interface to define the set of operations I expect my repositories' Unit of Work implementations to have, and I want this interface to be fulfilled by EF Core's DbContext
class.
So far this is what I got: (I know the name is awful, suggestions accepted):
public interface IDbContextable<TContext, TEntityEntry>
where TContext : class, IDisposable
where TEntityEntry : class
{
public TEntityEntry Add([NotNullAttribute] object entity);
public TEntityEntry<TEntity> Add<TEntity>([NotNullAttribute] TEntity entity)
where TEntity : class;
public int SaveChanges();
}
The first Add
method's signature from EF Core is: public virtual EntityEntry Add([NotNullAttribute] object entity);
- and it works fine.
I'm having trouble with the second. The compiler complains:
Error CS0307 The type parameter 'TEntityEntry' cannot be used with type arguments
For completeness, my context class looks like this:
public partial class EntityFrameworkContext : DbContext, IDbContextable<EntityFrameworkContext, EntityEntry>
{
//... some extra methods here
}
How to properly declare the interface to get rid of the error, and so that the signature: public virtual EntityEntry<TEntity> Add<TEntity>([NotNullAttribute] TEntity entity) where TEntity : class;
from EF Core becomes valid?
Thanks.
2 answers
The short answer is that it can't be done. C# doesn't support type functions. https://github.com/dotnet/csharplang/issues/339 is probably the issue to follow if you want to track their progress.
Workarounds are going to require another type parameter and probably a factory method: e.g.
public TEntityEntry_TEntity Add<TEntity, TEntityEntry_TEntity>(
[NotNull] TEntity entity,
[NotNull] Func<TEntity, TEntityEntry_TEntity> wrap)
where TEntity : class
where TEntityEntry_TEntity : TEntityEntry;
6 comments
Thanks. Could you give an example of what the factory method implementation would look like?
... and/or, how the implementation of this interface should be done.
I think I got it, if you may have a look at my answer below, tell me if that makes sense and is correct.
@Marc.2377, your example looks like it will compile. I don't know whether the example signature is the best one for your situation, because I'm very short on context. This may be a situation where you should iterate two or three steps of asking "Why do I want to do that?" and then formulate a new question aimed at solving the higher level problem. E.g. is the underlying problem how to do unit testing of layer XYZ?
My final goal is to have an interface to be able to use EF Core in my application services layer while enforcing that whatever custom DbContext-like implementations an user might want to implement in the future provides the same set of functionality in order to require minimal changes to the application.
I believe an implementation of the answer by Peter Taylor above would be:
public override EntityEntry<TEntity> Add<TEntity>([NotNullAttribute] TEntity entity)
where TEntity : class
{
return Add(entity, base.Add);
}
public TEntityEntry_TEntity Add<TEntity, TEntityEntry_TEntity>([NotNullAttribute] TEntity entity,
[NotNullAttribute] Func<TEntity, TEntityEntry_TEntity> wrap)
where TEntity : class
where TEntityEntry_TEntity : EntityEntry
{
return wrap(entity);
}
10 comments
Not sure I follow the Q.
TEntityEntry
type isn't generic. Why can't you just writepublic TEntityEntry Add<TEntity>([NotNullAttribute] TEntity entity) where TEntity : class;
? — FoggyFinder 28 days ago@FoggyFinder I tried, but then in my class that inherits from
DbContext
I get:Error CS0738 'EntityFrameworkContext' does not implement interface member 'IDbContextable<EntityFrameworkContext, EntityEntry>.Add<TEntity>(TEntity)'. 'DbContext.Add<TEntity>(TEntity)' cannot implement 'IDbContextable<EntityFrameworkContext, EntityEntry>.Add<TEntity>(TEntity)' because it does not have the matching return type of 'EntityEntry'.
— Marc.2377 28 days ago... then the only valid implementation would be:
public EntityEntry Add<TEntity>(TEntity entity) where TEntity : class
- but I want the implementation that already exists in the base class (DbContext
) to be valid. And its signature returnsEntityEntry<TEntity>
. — Marc.2377 28 days agoah, the goal is avoiding something like that
EntityEntry IDbContextable<EntityFrameworkContext, EntityEntry>.Add<TEntity>(TEntity entity) => base.Add(entity);
in theEntityFrameworkContext
class? — FoggyFinder 28 days ago@FoggyFinder yes, the interface must be applicable to the method already defined in the base class, which in this case is part of the framework. — Marc.2377 28 days ago