开发者

Testing multiple db mappings in MappingIntegrationTests

What's the best approach for testing multiple db's in a s#arparch project?

The current SetUp() code in MappingIntegrationTests tries to test each assembly against the first database.

string[] mappingAssemblies = RepositoryTestsHelper.GetMappingAssemblies();

 configuration = NHibernateSession.Init(new SimpleSessionStorage(), mappingAssemblies,
                                   new AutoPersistenceModelGenerator().Generate(),
                               开发者_如何学C    "../../../../app/Humanities.IBusiness.Web/NHibernate.config");

Has anyone managed to correctly test each mapping against the appropriate database schema?


Hey Alec, thanks for the reply. I've hacked a bit of a solution - it aint pretty but it does smoke test dodgy mappings across multiple db's

In the set up I add the following:

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");

            /*NEW CODE */
            var configuration2 = NHibernateSession.AddConfiguration(DataGlobals.ROLES_DB_FACTORY_KEY,
                mappingAssemblies,
                new AutoPersistenceModelGenerator().Generate(),
                "../../../../app/Humanities.IBusiness.Web/NHibernateForRolesDb.config",null,null, null);
            sessionKeys = new List<string>();
            sessionKeys.Add(DataGlobals.DEFAULT_DB_KEY);
            sessionKeys.Add(DataGlobals.ROLES_DB_FACTORY_KEY);

Then in the CanConfirmDatabaseMatchesMappings

foreach (var entry in allClassMetadata)
                {
                    bool found = false;
                    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());
                }

Not sure if it's a full answer but better than nothing :)

Any thoughts?


Al, to be honest I do not know the extent that the users are using Multiple Databases. This is something I believe a few very vocal people lobbied for in the beginning of the projects lifecycle. This is something I was going to ask the community about, looks like the time has come for the question to be asked.

What you might need to do is move the set-up code into individual methods. While I am not crazy with breaking the DRY principal, it would seem in this case it is required.

Alec

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜