Nhibernate and decapsulating decorators
<p>I am currently working on a project with NHibernate that requires security and auditing aspects. Those two seem to be perfect fits for the decorator pattern. Therefore my first step was to extract an interface for the entities to be decorated. Next, I decorated the relevant repositories to return decorated entities that implement the required auditing and security respectively. This works as expected.</p>

<p>However, a problem arises when NHibernate is asked to save a decorator instead of the base entity. Consider the following model as a simple example. It consists of an Engine that can be composed from multiple components. </p>

<pre class="lang-cs prettyprint-override"><code>public interface IEngine { 
  void AddComponent(IComponent component);

  // Other Engine methods 
}

public interface IComponent { 
  // Component methods 
}

// Component basic entity mapped via NHibernate
public class Component : IComponent {

}

// Engine basic entity mapped via NHibernate
public class Engine {
  private IList&lt;IComponent&gt; _components;

  public Engine(IEnumerable&lt;IComponent&gt; components) {
    _components = components.ToList();
  }

  public void AddComponent(IComponent component) {
    _components.Add(component);
  }

  // Other engine methods
}

// Component security decorator
public class SecurityComponent : IComponent {
  private readonly IComponent _innerComponent;

  public SecurityComponent(IComponent innerComponent) {
    _innerComponent = innerComponent;
  }

  // delegated and changed methods
}

// Engine security decorator
public class SecurityEngine {
  private readonly IEngine _innerEngine;

  public SecurityEngine(IEngine innerEngine) {
    _innerEngine = innerEngine;
  }

  // delegated and changed methods
}
</code></pre>

<p>The code that is responsible for creating and saving Engines does not know anything about security decorators:</p>

<pre class="lang-cs prettyprint-override"><code>var components = componentRepository.RetrieveMatchingComponents(); // because the repository is decorated, this method returns SecurityComponents
var engine = new Engine(components);
engineRepository.Create(engine); // will fail because NHibernate cannot deal with the decorators referenced in the Engine
</code></pre>

<p>The only solution I can currently think of is to move the object creation into a factory that can also be decorated by the security code. The security factory would
need to decapsulate the SecurityComponents in order to construct a inner engine consisting only of basic entities. In addition the SecurityEngine would need to 
decapsulate all incoming SecurityComponents. Finally the SecurityEngineRepository would need to decapsulate incoming SecurityEngines so that the innermost repository
that calls Session.Save receives only a basic entity Engine consisting only of basic entity Components. For example:</p>

<pre class="lang-cs prettyprint-override"><code>public class SecurityComponent : IComponent {
  private readonly IComponent _innerComponent;

  public SecurityComponent(IComponent innerComponent) {
    _innerComponent = innerComponent;
  }

  public IComponent Decapsulate() { return _innerComponent; }

  // delegated and changed methods
}

public class SecurityEngine {
  private readonly IEngine _innerEngine;

  public SecurityEngine(IEngine innerEngine) {
    _innerEngine = innerEngine;
  }

  public void AddComponent(IComponent component) {
    // do security stuff (e.g check if adding components is allowed)

    IComponent result;
    if (component is SecurityComponent) {
      result = ((SecurityComponent)component).Decapsulate();
    } else {
      result = component;
    }

    _components.Add(result);
  }

  // other delegated and changed methods
}

public interface IEngineFactory {
  IEngine CreateEngine(IEnumerable&lt;IComponent&gt; components);
}

public class EngineFactory : IEngineFactory {
  public IEngine CreateEngine(IEnumerable&lt;IComponent&gt; components) { return new Engine(components); }
}

public class SecurityEngineFactory : IEngineFactory {
    // decorator constructor

    public IEngine CreateEngine(IEnumerable&lt;IComponent&gt; components) { 
      // decapsulate security components
      var innerEngine = _innerEngineFactory.CreateEngine(decapsulatedComponents);

      return new SecurityEngine(innerEngine);
    }
}   
</code></pre>

<p>The engine construction code:</p>

<pre class="lang-cs prettyprint-override"><code>var components = componentRepository.RetrieveMatchingComponents(); // because the repository is decorated, this method returns SecurityComponents
var engine = engineFactory.CreateEngine(components); // SecurityEngineFactory will return a SecurityEngine with a well formed inner Engine
engineRepository.Create(engine); // SecurityEngineRepository will decapsulate the SecurityEngine
</code></pre>

<p>This solution seems like a code smell to me. Is there a general pattern to solve this problem? Any suggestions on how to improve this solution?</p>