c# - Validating Entity Framework 6 entities that have similar properties -
i'm using entity framework 6 (created database model), , have table called users, of have related tables called userxml, userextranet, usercustomer, of these tables have same fields.
the tables linked foreign keys.
based on user type e.g xml/extranet/customer, need perform validation userxml enabled, or userextranet enabled.
here snippet of have @ moment
//find user user ouser = context.user.firstordefault(u => u.email == this.email); if (ouser != null) { var ousertype = (dynamic)null; int imaxattempts; bool bvalidip = false; //authenticate type of user switch (this.type) { case usertype.xml: ousertype = ouser.userxml; //do validation break; case usertype.api: ousertype = ouser.userapi; //do validation break; default: ousertype = null; break; }
}
here entities.
the user class
public partial class user { public user() { } public long userid { get; set; } public string email { get; set; } public virtual usercustomer usercustomer { get; set; } public virtual userextranet userextranet { get; set; } public virtual userxml userxml { get; set; } }
the customer class
public partial class usercustomer { public usercustomer() { } public long userid { get; set; } public long siteid { get; set; } public string email { get; set; } public string password { get; set; } public string salt { get; set; } public byte failedattempts { get; set; } public system.datetime lastlogin { get; set; } public bool enabled { get; set; } public bool deleted { get; set; } public system.datetime createdon { get; set; } public system.datetime lastchanged { get; set; } public virtual user user { get; set; } }
the userextranet class
public partial class userextranet { public userextranet() { } public long userid { get; set; } public long siteid { get; set; } public string password { get; set; } public string salt { get; set; } public byte failedattempts { get; set; } public bool enabled { get; set; } public bool deleted { get; set; } public system.datetime createdon { get; set; } public system.datetime lastchanged { get; set; } public virtual user user { get; set; } }
the userxml class
public partial class userxml { public userxml() { } public long userid { get; set; } public string password { get; set; } public string salt { get; set; } public byte failedattempts { get; set; } public bool enabled { get; set; } public bool deleted { get; set; } public system.datetime createdon { get; set; } public system.datetime lastchanged { get; set; } public virtual user user { get; set; } }
how best can achieve without doubling code each table.
create fourth class called usermodel , cast various user objects it, validate usermodel.
public partial class usermodel { public usermodel() { } public long userid { get; set; } public long siteid { get; set; } public string email { get; set; } public string password { get; set; } public string salt { get; set; } public byte failedattempts { get; set; } public system.datetime lastlogin { get; set; } public bool enabled { get; set; } public bool deleted { get; set; } public system.datetime createdon { get; set; } public system.datetime lastchanged { get; set; } public virtual user user { get; set; } }
and then
//find user user ouser = context.user.firstordefault(u => u.email == this.email); if (ouser != null) { var ousertype = (dynamic)null; int imaxattempts; bool bvalidip = false; var modeluser = new usermodel(){ userid = ouser,userid, etc.}; //authenticate type of user switch (this.type) { case usertype.xml: ousertype = ouser.userxml; break; case usertype.api: ousertype = ouser.userapi; break; default: ousertype = null; break; } modeluser.validate(); }
Comments
Post a Comment