I have a class called UserInfo that contains details about a given user.
There are several places in code where the data might be queried and I’d like to have a single function to fill the UserInfo object with the corresponding data from the Linq Query.
var userData = dc.Users.Where(λ => (λ.Login == username) && λ.Active)
.Select(λ => new { λ.ID, Salt = λ.Seasonings.Single().Salt, λ.Login, λ.PassHash, λ.Admin, λ.Trusted, λ.E_mail, λ.Name, λ.Phone, λ.Note, λ.RegistrationDate }).SingleOrDefault();
string tmppass = generatePassHash(password, userData.Salt);
if (userData.PassHash.Trim() == tmppass.Trim())
{
ID = userData.ID;
// Here is the stuff i'd like to move to a function
_user._id = userData.ID;
_user._userState = State.NotAuthorized;
_user._username = userData.Login;
_user._name = userData.Name;
_user._email = userData.E_mail;
_user._phone = userData.Phone;
_user._notes = userData.Note;
...
}
How do I properly set up a function to accept this anonymous type as an argument? Do I need to declare a new interface or is there a simpler way?
Thanks for the help!
PS- sorry for the excessive underscores, nested classes make things a bit messy.
For simplicity’s sake, couldn’t you just have all your routines accept the entity object itself? E.g. if
dc.Usersis a table of typeUserEntity, skip theSelect():And if that isn’t acceptable, encapsulate a more limited object which takes UserEntity as a ctor parameter:
This abstracts the messy conversion away from the rest of your application. However, in general I’d recommend simply using the entity object, since it makes it much easier to go in reverse when you need (passing a modified entity back to the DAL).