title: Can I safely query using the base type of an entity's proxy interface in NHibernate?
body: <p>I'm currently in the design phase of multiple rather complex systems which share common functionality (e.g. both have customer-relationship management (CRM) and sales functionality). Therefore I'm trying to extract the common part of the domain and reuse it in both applications. </p>
  
  <p>Let's say I have application A and application B, both using CRM functionality. For the most part, the CRM functionality is the same, but both applications like to augment some things related to CRM. </p>
  
  <p>I'd like to create a library implementing a basic version of the CRM system, e.g. a customer is abstracted to</p>
  
  <pre><code>interface ICustomer {
    string CustomerNumber {get;set;}
  }
  </code></pre>
  
  <p>and the base library has a basic implementation of ICustomer</p>
  
  <pre><code>class Customer: ICustomer
  </code></pre>
  
  <p>Application A can now augment this:</p>
  
  <pre><code>interface IACustomer : ICustomer {
      bool ReceivesNewsletter {get;set;}
  }
  
  class ACustomer : Customer, IACustomer {
     ...
  }
  </code></pre>
  
  <p>Likewise, B has its own variation:</p>
  
  <pre><code>interface IBCustomer : ICustomer {
      string NickName {get;set;}
  }
  
  class BCustomer : Customer, IBCustomer {
      ....
  }
  </code></pre>
  
  <p>For the purpose of abstraction, I never instantiate concrete types in the base library but use a factory or a DI container, e.g.:</p>
  
  <pre><code>interface ICrmFactory {
      ICustomer CreateCustomer();
  }
  </code></pre>
  
  <p>A and B implement the factory accordingly such that ACustomers or BCustomers are created respectively.</p>
  
  <p>Here's a UML diagram of what I mean (application B is not shown):
  <img src="https://i.stack.imgur.com/3C80D.png" alt="Application design"></p>
  
  <p>For persistence I use NHibernate. Both A and B provide their own mapping (mapping-by-code) to include the extra properties. In addition, A defines IACustomer to be the proxy type of ACustomer and B defines IBCustomer to be the proxy type of BCustomer.</p>
  
  <p>I can now use NHibernate to work with the entities in A and B, e.g. in A</p>
  
  <pre><code>session.QueryOver&lt;ACustomer&gt;()
      .Where(c=&gt;c.ReceivesNewsletter)
      .List()
  </code></pre>
  
  <p>Now let's say I want to do something with customers in the base library (for example in some kind of service object). I've just prototyped a very simple version and so far this seems to work fine:</p>
  
  <pre><code>session.QueryOver&lt;ICustomer&gt;()
     .Where(c =&gt; c.CustomerNumber == "1234ABC")
     .List()
  </code></pre>
  
  <p>That is, I can use the base class of the proxy type of a specific entity in QueryOver, and NHibernate creates the correct query, e.g. in A:</p>
  
  <pre><code>SELECT
      this_.Id as Id0_0_,
      this_.CustomerNumber as Customer2_0_0_,
      this_.ReceivesNewsletter as Receives3_0_0_ 
  FROM
      ACustomer this_ 
  WHERE
      this_.CustomerNumber = @p0;
  @p0 = '1234ABC' [Type: String (4000)] 
  </code></pre>
  
  <h2>My question</h2>
  
  <p>Will those queries always work as expected? Can I always safely use a base type of the proxy interface in my queries in the base library? Will NHibernate always find the "correct" entity type and table to retrieve? Or will there be situations where this affects query efficiency adversely b/c NHibernate needs to do guess work? Any other pitfalls I should be aware of in this scenario?</p>
  
  <p>I could not find information about this in the documentation. This approach would allow me to neatly move common parts of some domains to their own base libraries, but I want to make sure it works.</p>