S#harp Architecture – Testing multiple database mappings in MappingIntegrationTests

Standard
I’m using s#harp for a project that needs to map multiple db’s. Whilst this process of setting this up is well documented on the wiki (http://wiki.sharparchitecture.net/FAQ.ashx) the process of getting mutli db mappings to pass the MappingIntegrationTests isn’t.
For what it’s worth I’ve hacked together a means to test that each Fluent mapping is tested to see if it matches one of the db’s. It aint pretty but it does smoke out dodgy mappings.
——————————————————-
//Add a list that holds the session keys
private List<string> sessionKeys;
[SetUp]
public virtual void SetUp()
{
    string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();
    configuration = NHibernateSession.Init(new SimpleSessionStorage(), mappingAssemblies,
    new AutoPersistenceModelGenerator().Generate(),
    "../../../../app/Humanities.IBusiness.Web/NHibernate.config");
/* ADD EXTRA DB'S TO SESSION CONFIG  */
    var configuration2 = NHibernateSession.AddConfiguration(DataGlobals.ROLES_DB_FACTORY_KEY,
    mappingAssemblies,
    new AutoPersistenceModelGenerator().Generate(),
        "../../../../app/Humanities.IBusiness.Web/NHibernateForRolesDb.config",null,null, null);
/* ADD EACH SESSION KEY TO LIST */
    sessionKeys = new List<string>();
    sessionKeys.Add(DataGlobals.DEFAULT_DB_KEY);
    sessionKeys.Add(DataGlobals.ROLES_DB_FACTORY_KEY);
}

[Test]

public void CanConfirmDatabaseMatchesMappings()
{
//GET ALL META DATA FROM SESSION (NOTE DOESN@T MATTER WHICH SESSION FACTORY IS USED!
    var allClassMetadata = NHibernateSession.GetDefaultSessionFactory().GetAllClassMetadata();
    if (NHibernateSession.IsConfiguredForMultipleDatabases())
    {
    //INTEROGATE EACH CLASS
        foreach (var entry in allClassMetadata)
        {
            bool found = false;
            //ENSURE EACH CLASS MAPS TO A DB
            foreach (string key in sessionKeys)
           {
                ISession session = NHibernateSession.CurrentFor(key);
                try
                {
                    session.CreateCriteria(entry.Value.GetMappedClass(EntityMode.Poco))
                    .SetMaxResults(0).List();
                    found = true;
                }
                catch (Exception ex) { }
            }
        if (found == false)
            throw new MappingException("Mapping not found for " + entry.Key.ToString());
    }
}

else //Do what you'd normally do or do the above...

About these ads

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s