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...

0 Responses to “S#harp Architecture – Testing multiple database mappings in MappingIntegrationTests”