views:

34

answers:

1

I have a user entity with the following properties that map to a field in the database:

Id         int  
Username   varchar(25)  
Password   binary(64)   
Salt       binary(64)   
Name       varchar(50)  
Locked     bit  

What I don't want to do is always return the Password and Salt for every query. But for certain queries, I do want to be able to check the password (from u in db.Users where u.Password == password select u) or set the Password/Salt fields. I don't want 128B always going over the wire when it's never needed.

I tried setting the property's getter to private, but that stopped me from using it in LINQ. Basically it would be cool if I could set a property to always lazy load.

+1  A: 

Create a User Repository that uses a POCO to store the User without the Password and Salt fields.

The Repository handles calling Entity Framework and populating your POCO with its data.

namespace MyCompany.Data.Repositories
{
    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Name { get; set; }
        public bool Locked { get; private set; }
    }

    public class UserRepository
    {
        public User GetAll() { }
        public User GetById() { }

        // Add your check password method here
    }
}
Justin Niessner
So then what happens to the auto-generated User class?
TheCloudlessSky
@TheCloudlessSky - You utilize it inside your Repository class (and restrict its use elsewhere).
Justin Niessner
@Justin - The problem then becomes that I have two User classes (though from different namespaces). Isn't this cumbersome?
TheCloudlessSky
@TheCloudlessSky - When using Repositories, the consumer shouldn't need access to the EF generated classes at all. Therefore they should only see your MyCompany.Data.Repositories.User class.
Justin Niessner
Ok cool, thanks.
TheCloudlessSky