简体   繁体   English

用 2 个类扩展 IdentityUser

[英]Extending IdentityUser with 2 classes

In my project, I have a base Repository class defined like the following:在我的项目中,我有一个基本Repository class 定义如下:

internal abstract class Repository<TDomain, TEntity> : IReadWriteRepository<TDomain>
    where TDomain : Model
    where TEntity : Entity
{
   ...
}

As you can see, TEntity has a constraint and must be of type Entity如您所见, TEntity有一个约束,并且必须是Entity类型

The Entity class: Entity class:

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

And then I have a User class which extends IdentityUser<int>然后我有一个扩展IdentityUser<int>User class

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'这会抱怨并给出一个错误,即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?由于我的Repository被限制为Entity类型,我想知道如何解决这个问题?

You should create an interface called IEntity and use the interface to define the constraint.您应该创建一个名为IEntity的接口并使用该接口来定义约束。

Then, implement the interface creating your abstract Entity class.然后,实现创建抽象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.您可以在域模型中毫无问题地使用Entity ,但在您的用户 class 中,您可以使用接口将其设为实体。

This way your user class and all your other domain models would satisfy the constraint.这样,您的用户 class 和所有其他域模型将满足约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM