Note that there are some explanatory texts on larger screens.

plurals
  1. POInjecting Dependencies into Domain Model classes with Nhibernate (ASP.NET MVC + IOC)
    text
    copied!<p>I'm building an ASP.NET MVC application that uses a DDD (Domain Driven Design) approach with database access handled by NHibernate. I have domain model class (Administrator) that I want to inject a dependency into via an IOC Container such as Castle Windsor, something like this:</p> <pre><code>public class Administrator { public virtual int Id { get; set; } //.. snip ..// public virtual string HashedPassword { get; protected set; } public void SetPassword(string plainTextPassword) { IHashingService hasher = IocContainer.Resolve&lt;IHashingService&gt;(); this.HashedPassword = hasher.Hash(plainTextPassword); } } </code></pre> <p>I basically want to inject IHashingService for the SetPassword method without calling the IOC Container directly (because this is suppose to be an IOC Anti-pattern). But I'm not sure how to go about doing it. My Administrator object either gets instantiated via <code>new Administrator();</code> or it gets loaded via NHibernate, so how would I inject the IHashingService into the Administrator class?</p> <p>On second thoughts, am I going about this the right way? I was hoping to avoid having my codebase littered with...</p> <pre><code>currentAdmin.Password = HashUtils.Hash(password, Algorithm.Sha512); </code></pre> <p>...and instead get the domain model itself to take care of hashing and neatly encapsulate it away. I can envisage another developer accidently choosing the wrong algorithm and having some passwords as Sha512, and some as MD5, some with one salt, and some with a different salt etc. etc. Instead if developers are writing...</p> <pre><code>currentAdmin.SetPassword(password); </code></pre> <p>...then that would hide those details away and take care of those problems listed above would it not?</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