I have two EF generated classes.
public partial class admin
{
public admin()
{
this.user = new HashSet<user>();
}
public int id { get; set; }//PK
//Other properties omitted for brevity
public virtual ICollection<user> user { get; set; }
}
And
public partial class user
{
public string username { get; set; }//PK
public string passwd { get; set; }
public int admin_id { get; set; }//FK
//Other properties omitted for brevity
public virtual admin admin { get; set; }
}
users belong to an admin by using FK admin_id. If admin_id and username are equal then the user is an admin. Example: users 'Messi', 'Neymar', 'Suarez' & '123' all have admin_id 123. So user '123' is an admin. (Maybe not the best approach but it is not relevant for the question.)
Since these two classes are EF auto-generated and can change in the future I have another partial user class with the same namespace but in a different folder(so the methods keep preserved):
public partial class user
{
public bool isAdmin()
{
return admin_id.ToString().Equals(username);
}
}
I also have this user repository:
public class EFUserRepo : IUserRepo
{
private Entities context = new Entities();
public IQueryable<user> Users { get { return context.user; } }
public user getUserByPK(string username)
{
return context.user.Find(username);
}
public user deleteUser(string username){ return null; }//Yet to implement
public bool saveUser(user user){ return false; }//Yet to implement
}
And I want to make another method for obtaining the useradmin of a given user, like this:
public user getUserAdmin(string username){ }//Note the return type is user, not admin!
My question is, where do I put this method?
I can put it in the EFUserRepo like this:
public user getUserAdmin(string username)
{
user user = getUserByPK(username);
if (user == null) return null;
return context.user.Find(user.admin_id);
}
public user getUserAdmin(user user)//Not relevant for question, but might be insightful for others
{
return getUserAdmin(user.username);
}
And in my controller call this:
user adminUser = repo.getUserAdmin(loggedOnUser.username);//or use repo.getUserAdmin(loggedOnUser) for same result.
Or I can put it in the partial user class like this:
public user getUserAdmin()
{
return this.admin.user.Where(x => x.isAdmin()).FirstOrDefault();
//Due to DB setup always returns 1 user.
}
And in my controller call this:
user adminUser = loggedOnUser.getUserAdmin();
I absolutely have no idea what's the best approach.
What if I also want to make a method like:
public admin getAdmin(string username){ }//Note the return type is admin here, not user
Then I can add this to my user repository:
public admin getAdmin(string username)
{
user user = getUserByPK(username);
if (user == null) return null;
return user.admin;
//return context.admin.Find(user.admin_id);//Also works, but is it best practise to access the admin collection from within the userrepo, think not
//return new EFAdminRepo().getAdminByPK(user.admin_id)//Also works, but seams really ugly
}
public admin getAdmin(user user)//Again, not relevant for question, but might be insightful for others
{
return getAdmin(user.username);
}
And in my controller call this:
admin admin = repo.getAdmin(loggedOnUser.username);//or use repo.getAdmin(loggedOnUser) for same result.
Or I can put it in the partial user class like this:
public admin getAdmin()
{
return this.admin.user.Where(x => x.isAdmin()).First().admin;
}
And in my controller call this:
admin admin = loggedOnUser.getAdmin();
Real question might be, do I use
ObjectX obj = repo.obtainMethodForObjectX(entity.params);
or
ObjectX obj = entity.obtainMethodForObjectX();
Aucun commentaire:
Enregistrer un commentaire