Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 205163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:31:55+00:00 2026-05-11T17:31:55+00:00

I have a project that is supposed to run on different (at least 2)

  • 0

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 tutorial on nhibernate.info.

Now, my current Architecture looks like this:

MyProject.DAL

Contains NewsItem and INewsItemRepository. NewsItem is a POCO, essentially just an int Id and string Title.

INewsItemRepository is the Interface that includes CRUD Functionality.

MyProject.Plugins.DAL.MSSQL

This class implements the backend. It includes hibernate.cfg.xml, NewsItem.hbm.xml and NewsItemRepository.

The cfg holds the Driver and ConnectionString, the hbm is the SQL-Server specific Mapping, and NewsItemRepository implements the INewsItemRepository.

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.

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?

Edit: 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 <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 <property> Tags in the general DAL Project and then .hbm’s in each Backend-Assembly containing the columns?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:31:56+00:00Added an answer on May 11, 2026 at 5:31 pm

    Well, I haven’t seen your project or data model so you’ll have to interpret this as best suits your situation. However, yes, my reaction is that this is more code than is necessary.

    One of the major benefits of using an ORM like NHibernate is that they separate your code from your persistence architecture. It seems to me that mapping NewsItem (for example) multiple times is unnecessary, for one. Isn’t the data model likely to be the same regardless of backend? Perhaps you could give an example of where they might differ.

    Secondly, I would probably not re-implement NewsItemRepository. Regardless of database backend, the repository is using NHibernate which has a primary purpose of abstracting the database. All CRUD functionality handled by the repository will be the same on MSSQL as SQLite (unless there actually is a difference in data model).

    If your database model is the same on both backends, in theory you should only need to change the connection string to change the database being used. The way I’ve implemented a DAL before is to provide the interfaces as you’ve done and then have a sub namespace for implementations, one (well, the only one at the moment 😉 being NHibernate. Repositories et al. are acquired through dependency injection. The DI kernel handles session factories as well so it’s easy to target a different database for different sections of code.

    Of course all this assumes that your data model is the same on both database engines. If it’s not, my answer might not be very helpful. If that’s the case, knowing what is different might lead me or someone else to a better answer.

    EDIT:

    I see what you mean about the sql-type. However, that’s only a problem if you are generating the schema for your database from NHibernate. When mapping the properties, you don’t need to use <column> and the type you specify on <property> is an NHibernate type. The StringClob type will handle NTEXT fields properly.

    If you are generating your schema from the NHibernate mappings and NHibernate is not smart enough to strip out the sql-type=”NTEXT” for SQLite (I’m hoping it is, but I’ve never used the schema generation tool), I think I would add a build step to cleanup the SQLite schema. Of course, now you’re adding complexity to your process so it’s up to you. However, I’d rather keep my code and mappings as simple and clean as possible and keep the complexity only where it is needed (the nit-picky details of the database engine). It seems to me that keeping two separate mapping files is a recipe for a missing property mapping and users left scratching their heads as to why they can’t put in their DOB when using SQLite 😉

    EDIT2 (regarding DI use with NH):

    This is a bit subjective since I’m sure there are many ways to use DI with NHibernate. I’ve had great success using DI with Fluent NHibernate and Ninject, a DI framework that also uses a “fluent” DSL. Fluent NHibernate (FNH) provides an interface IPersistenceConfigurer that is used to create your database connection string (it also provides a number of Dialects to choose from). Thus, it’s easy to bind that IPersistenceConfigurer service to a provider via the DI framework. You can do the same for ISessionFactory to have a fully D-injected factory. Since Ninject works off attributes my repository constructor might look like:

    [Inject, CRM]
    public MyRepository(ISessionFactory sessionFactory)
    

    Which would inject a factory for the CRM database. Me likey. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project that I'm currently working on but it currently only supports
We have a project that generates a code snippet that can be used on
I have a build project that I run from TeamCity, now it takes the
I have project that I'm working on that is going to require a webserver.
I have a project that I would like to start beta testing soon, it
I have a project that I'm working on and I need to be able
I have a project that I thought was going to be relatively easy, but
I have a project that I am building with Netbeans 6.1 and I am
I have a project that has a makefile with broken dependencies. Is there any
I have a project that has the following line in the additional includes section:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.