title: NHibernate and Multiple Database backends - Architecture
body: <p>I have a project that is supposed to run on different (at least 2) databse backends, so users can choose (currently between MSSQL and SQLite). I've just started learning NHibernate, following the <a href="http://nhibernate.info/doc/tutorials/first-nh-app/your-first-nhibernate-based-application" rel="nofollow noreferrer">tutorial on nhibernate.info</a>.</p>
  
  <p>Now, my current Architecture looks like this:</p>
  
  <h3>MyProject.DAL</h3>
  
  <p>Contains <strong>NewsItem</strong> and <strong>INewsItemRepository</strong>. NewsItem is a POCO, essentially just an int Id and string Title.</p>
  
  <p>INewsItemRepository is the Interface that includes CRUD Functionality.</p>
  
  <h3>MyProject.Plugins.DAL.MSSQL</h3>
  
  <p>This class implements the backend. It includes <strong>hibernate.cfg.xml</strong>, <strong>NewsItem.hbm.xml</strong> and <strong>NewsItemRepository</strong>.</p>
  
  <p>The cfg holds the Driver and ConnectionString, the hbm is the SQL-Server specific Mapping, and NewsItemRepository implements the INewsItemRepository.</p>
  
  <p>If I want to add the MyProject.Plugins.DAL.SQLite, I will have to duplicate all this, which is logical. However, I wonder if that is the correct approach? I feel like I'm unnecessarily duplicating things. I mean: I need to make sure the mapping is Database-specific, and that I update all my Plugins if the database changes. But this architecture still seems somehow... bloated.</p>
  
  <p>Am I doing the right thing here? Or are there common patterns about what goes into  the general business assembly and what needs to be kept separate for each database?</p>
  
  <p><strong>Edit:</strong> One of the challenges here is the handling of sql-types. For example, strings are mapped as nvarchar(255) by default, and if I need ntext in MSSQL, i'll have to use the &lt;column sql-type="NTEXT" syntax, do I? I guess that specifying sql-type is database-dependent, so I'll need my mapping separated per database? Or is there a built-in mechanism to have mappings "inherit" each other? (I.e. have one .hbm.xml with all the &lt;property&gt; Tags in the general DAL Project and then .hbm's in each Backend-Assembly containing the columns?</p>