Extending IdentityUser with 2 classes

Viewed 19

In my project, I have a base Repository class defined like the following:

internal abstract class Repository<TDomain, TEntity> : IReadWriteRepository<TDomain>
    where TDomain : Model
    where TEntity : Entity
{
    public IQueryable<TDomain> Get()
    {
        var entities = _context.Set<TEntity>();
        return entities.Select(e => _mapper.ToDomain(e));
    }
}

As you can see, TEntity has a constraint and must be of type Entity

The Entity class:

public abstract class Entity
{
    public int Id { get; set; }
}

And then I have a User class which extends IdentityUser<int>

public class User : IdentityUser<int>, Entity
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

This complains and gives an error that Class 'User' cannot have multiple base classes: 'IdentityUser<int>' and 'Entity'

Since my Repository is constrained to have Entity type I'm wondering how can I get around this?

1 Answers

You should create an interface called IEntity and use the interface to define the constraint.

Then, implement the interface creating your abstract Entity class.

You can use Entity in your domain models with no problem, but in your User class, you can use the interface to make it an entity.

This way your user class and all your other domain models would satisfy the constraint.

Related