【问题标题】:Inheritance for ApplicationUserApplicationUser 的继承
【发布时间】:2015-10-22 00:30:58
【问题描述】:

我正在尝试正确设置我的类并学习如何正确使用继承。使用 MVC 5、实体框架和身份模型来做到这一点。我有两个想要从不同页面(商家和关注者)注册的用户。我想继承 ApplicationUser,但对继承的工作原理感到困惑,因为 ApplicationUser 继承自 IdentityUser。

我希望能够访问 IdentityUser 中的电子邮件和用户名字段以进行注册。那么在创建 Merchant 和 Follower 类时,我是继承 AppUser 还是 IdentityUser?

应用用户类:

    namespace IdentitySample.Models

{

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    static ApplicationDbContext()
    {
        // Set the database intializer which is run once during application start
        // This seeds the database with admin user credentials and admin role
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
    }

身份类别:

    namespace Microsoft.AspNet.Identity.EntityFramework
    {
// Summary:
//     Default EntityFramework IUser implementation
//
// Type parameters:
//   TKey:
//
//   TLogin:
//
//   TRole:
//
//   TClaim:
public class IdentityUser<TKey, TLogin, TRole, TClaim> : IUser<TKey>
        where TLogin :      Microsoft.AspNet.Identity.EntityFramework.IdentityUserLogin<TKey>
    where TRole : Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole<TKey>
    where TClaim : Microsoft.AspNet.Identity.EntityFramework.IdentityUserClaim<TKey>
{
    // Summary:
    //     Constructor
    public IdentityUser();

    // Summary:
    //     Used to record failures for the purposes of lockout
    public virtual int AccessFailedCount { get; set; }
    //
    // Summary:
    //     Navigation property for user claims
    public virtual ICollection<TClaim> Claims { get; }
    //
    // Summary:
    //     Email
    public virtual string Email { get; set; }
    //
    // Summary:
    //     True if the email is confirmed, default is false
    public virtual bool EmailConfirmed { get; set; }
    //
    // Summary:
    //     User ID (Primary Key)
    public virtual TKey Id { get; set; }
    //
    // Summary:
    //     Is lockout enabled for this user
    public virtual bool LockoutEnabled { get; set; }
    //
    // Summary:
    //     DateTime in UTC when lockout ends, any time in the past is considered not
    //     locked out.
    public virtual DateTime? LockoutEndDateUtc { get; set; }
    //
    // Summary:
    //     Navigation property for user logins
    public virtual ICollection<TLogin> Logins { get; }
    //
    // Summary:
    //     The salted/hashed form of the user password
    public virtual string PasswordHash { get; set; }
    //
    // Summary:
    //     PhoneNumber for the user
    public virtual string PhoneNumber { get; set; }
    //
    // Summary:
    //     True if the phone number is confirmed, default is false
    public virtual bool PhoneNumberConfirmed { get; set; }
    //
    // Summary:
    //     Navigation property for user roles
    public virtual ICollection<TRole> Roles { get; }
    //
    // Summary:
    //     A random value that should change whenever a users credentials have changed
    //     (password changed, login removed)
    public virtual string SecurityStamp { get; set; }
    //
    // Summary:
    //     Is two factor enabled for the user
    public virtual bool TwoFactorEnabled { get; set; }
    //
    // Summary:
    //     User name
    public virtual string UserName { get; set; }
}
    }

【问题讨论】:

  • 您应该告诉我们一些关于您的应用程序域中 Merchant 和 Follower 之间的预期差异。到目前为止,我无法确定有限的信息,但是使用附加到用户帐户而不是子类的商人和追随者角色可能会更好地建模。但这取决于您的应用程序的要求。
  • 我考虑过使用角色,但认为没有必要。商家将刚刚创建商家页面并管理商家菜单项。关注者将是关注商家菜单项的客户。我想我的问题归结为如果我继承 Application User 是否意味着它也继承了 Identity User?
  • @JPHochbaum 你错过了 Davids 的观点,对两个不同的 ApplicationUser 类进行建模可能有点矫枉过正。除非商家拥有与追随者不同的信息,否则您可以将 AppUser 用于两者,将角色应用于每个人,然后从那里开始而不会感到头疼。
  • 是的,商人持有的信息与追随者不同。追随者只会有姓名、电子邮件、电话号码。商户会有地址、menuItem 数组等......

标签: asp.net-mvc inheritance


【解决方案1】:

我认为您应该简单地从 ApplicationUser 类继承,因此您将拥有以下内容:

public class Merchant : ApplicationUser{ //...}
public class Follower: ApplicationUser{ //...}

现在,Merchant 和 Follower 都继承了 IdentityUser 和 ApplicationUser 的所有属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2023-03-27
    相关资源
    最近更新 更多