Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to build Rich Domain Model with EntityFramework
    text
    copied!<p>Im using Entity Framework 4.2 (Code First) to create my database, which works fine so far,but now im facing a problem that is very easy to overcome in Hibernate or JPA but im not able see it here.</p> <p>I have define a User object which have a property called Password, i want to customize the {get;set;} operations in order to have certain logic when setting the password (i want to stored a hash version of it, but i want that logic inside my domain object ala DDD). But im facing that when materializing an object from the database my setter is being called and is not using directly the private field.</p> <p>Im trying to build a Rich Domain Object model and avoiding DAO/Repository pattern on this.</p> <p>Is this possible thru Entity Framework, or will i be force to use DAO/Repository patterns.</p> <p>Below is an extraction of my User object:</p> <pre><code>public class User { [Key] public string LoginId { get; set; } [Required] private string password; public string Password { get { return password; } set { //Random Salt byte[] s; using (RNGCryptoServiceProvider prov = new RNGCryptoServiceProvider()) { s = new byte[20]; prov.GetBytes(s); } this.salt = Convert.ToBase64String(s); //Random salt password = ComputeHash(value); } } [Required] private string salt; public string Salt { get { return this.salt; } set { throw new InvalidOperationException("Salt is not an assignable property. Assign a password first to your model and a Salt will get created."); } } public bool ValidatePassword(string clearTextPassword) { return this.Password == this.ComputeHash(clearTextPassword); } public string ComputeHash(string value) { ... return hashVersion of value; } </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload