nhibernate - 2 factories / 2 models / 2 databases

22 views
Skip to first unread message

Wayne Douglas

unread,
Oct 15, 2009, 5:49:50 AM10/15/09
to castle-pro...@googlegroups.com
Hi

I'm using the nh facility to configure the following setup:

i have 1 database which stores generic report configuration.
and another which stores the actual report data.

i also have 1 project for interacting with the report configuration
database (entities and mappings etc)
and another for interacting with the report data database (entities
and mappings etc).

i've used the following to create 2 factories:

<code>

<facilities>
<facility id="nhibernate">
<factory id="nhibernate.factory.session1">
...
<assemblies>
<assembly>ReportData.Model</assembly>
</assemblies>
</factory>

<factory id="nhibernate.factory.session2" alias="reporting">
...
<assemblies>
<assembly>Reporting.Model</assembly>
</assemblies>
</factory>
</facility>
</facilities>

<code>

the problem is is that even though nhibernate.factory.session1 and
nhibernate.factory.session2 are looking at different databases and
have different assemblies configured it always seems to create the
tables for both models in both DBs?

how do i tell nhibernate.factory.session1 to look at db1 and model1
and nhibernate.factory.session2 to look at db2 and model2?


--
Cheers,

w://

Valeriu Caraulean

unread,
Oct 15, 2009, 10:48:27 AM10/15/09
to castle-pro...@googlegroups.com
Different connection strings?
I mean, are they different?

Wayne Douglas

unread,
Oct 16, 2009, 4:06:50 PM10/16/09
to castle-pro...@googlegroups.com
so... still not got this working :(

here is the actual config i'm using:

<code>

<castle>
<components>
<component id="GenericRepository" type="Ideal.Web.Repository`1, Ideal.Web"/>
</components>
<facilities>
<facility id="nhibernate"
isWeb="true"
type="Castle.Facilities.NHibernateIntegration.NHibernateFacility,
Castle.Facilities.NHibernateIntegration"
configurationBuilder="Ideal.Rttm.Web.FluentNHibernateConfigurationBuilder,
Ideal.Rttm.Web">

<factory id="nhibernate.factory.session1">
<settings>
<item key="show_sql">true</item>
<item key="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</item>
<item key="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</item>
<item key="connection.connection_string">
Data Source=.;Initial Catalog=Reporting1;User Id=sa;Password=xxx;
</item>
<item key="dialect">
NHibernate.Dialect.MsSql2005Dialect
</item>
<item key="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle
</item>
</settings>
<assemblies>
<assembly>Ideal.Rttm.Model</assembly>
</assemblies>
</factory>

<factory id="nhibernate.factory.session2" alias="reporting">
<settings>
<item key="show_sql">true</item>
<item key="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</item>
<item key="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</item>
<item key="connection.connection_string">
Data Source=.;Initial Catalog=Reporting2;User Id=sa;Password=xxx;
</item>
<item key="dialect">
NHibernate.Dialect.MsSql2005Dialect
</item>
<item key="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle
</item>
</settings>
<assemblies>
<assembly>Ideal.Reporting.Model</assembly>
</assemblies>
</factory>

</facility>
<facility id="atm"
type="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility,
Castle.Facilities.AutomaticTransactionManagement" />
</facilities>
</castle>

</code>

has anyone used the nh facility to do this?
--
Cheers,

w://

Valeriu Caraulean

unread,
Oct 16, 2009, 6:08:52 PM10/16/09
to castle-pro...@googlegroups.com
The config looks ok.
Are you sure your code is correct?

Wayne Douglas

unread,
Oct 17, 2009, 6:53:57 AM10/17/09
to castle-pro...@googlegroups.com
very strange :s

here is my config builder:

<code>

public class FluentNHibernateConfigurationBuilder : IConfigurationBuilder
{
#region IConfigurationBuilder Members

public Configuration GetConfiguration(IConfiguration
facilityConfiguration)
{
var defaultConfigurationBuilder = new DefaultConfigurationBuilder();
Configuration configuration =
defaultConfigurationBuilder.GetConfiguration(facilityConfiguration);

var models = new PersistenceModel();
models.AddMappingsFromAssembly(typeof(Provider).Assembly);
models.AddMappingsFromAssembly(typeof(Report).Assembly);
models.Conventions.Add(typeof(ForeignKeyConventionOverride));
models.Configure(configuration);

if (AppSettings.GetConfigurationBoolean("RebuildDb", false))
{
var export = new SchemaExport(configuration);
var sb = new StringBuilder();
TextWriter output = new StringWriter(sb);
export.Drop(true, true);
export.Execute(true, false, false, null, output);
export.Create(true, true);
}

return configuration;
}

#endregion
}

</code>

any help?

obv the classes Provider and Report come from 2 seperate assemblies
and this config builder lives inside my web project which has
references to both.

w://
--
Cheers,

w://

Valeriu Caraulean

unread,
Oct 18, 2009, 7:22:39 AM10/18/09
to castle-pro...@googlegroups.com
You have specified assemblies related to each session factory in XML
configuration. Why you do that again in code?

Your configuration builder adds two assemblies. Are those assemblies
each for different domain? Then why you're adding them both to
configuration?

Wayne Douglas

unread,
Oct 19, 2009, 5:49:39 AM10/19/09
to castle-pro...@googlegroups.com
hey - cheers for looking at this with me.

not very experienced with nhib - i thought the assembly definition in
the xml would point the factory to one assembly so it would only push
the resulting sql creation scripts to that one db. i thought the one
in the c# was just there to bootstrap it all.

so i need to have 2 separate config builders?

how do i wire one up with one factory configuration and the other with
the other configuration?
--
Cheers,

w://

Valeriu Caraulean

unread,
Oct 19, 2009, 6:16:05 AM10/19/09
to castle-pro...@googlegroups.com
So, you want to use FluentNHibernate to configure factories built via
NHIntegration facility.

What I would try to do:

1. Create a IConfigurationContributor for each of your domains.
2. Check in contributors the name/factory-id to modify only the right
configuration
3. Modify the configuration with FluentNHibernate.

This is just one way to achieve it.

Wayne Douglas

unread,
Oct 19, 2009, 6:40:58 AM10/19/09
to castle-pro...@googlegroups.com
thanks for the poiinter :)

how do i register more than one IConfigurationBuilder with the
facility config tho?
--
Cheers,

w://

wayne

unread,
Oct 24, 2009, 2:20:13 PM10/24/09
to Castle Project Users
hi

i've tried to google for the contributor and didn't come up with much
- i also grepped the castle svn dir but that didn't do much either.

here's what i tried:

<code file='castle.config'>

<components>
<component id="ReportingIdealConfigurationContributor"
type="Ideal.Reporting.Domain.IdealConfigurationContributor,
Ideal.Reporting.Domain"/>
<component id="RttmIdealConfigurationContributor"
type="Ideal.Rttm.Domain.IdealConfigurationContributor,
Ideal.Rttm.Domain"/>
</components>

<facilities>
<facility id="nhibernatefaciltity"
isWeb="true"

type="Castle.Facilities.NHibernateIntegration.NHibernateFacility,
Castle.Facilities.NHibernateIntegration"

configurationBuilder="Ideal.Rttm.Web.FluentNHibernateConfigurationBuilder,
Ideal.Rttm.Web">
<factory id="sessionFactory1">
<settings>
<item key="show_sql">true</item>
<item
key="connection.provider">NHibernate.Connection.DriverConnectionProvider</
item>
<item
key="connection.driver_class">NHibernate.Driver.SqlClientDriver</item>
<item key="connection.connection_string">Data
Source=.;Initial Catalog=reporting2;Integrated Security=SSPI;</item>
<item key="dialect">NHibernate.Dialect.MsSql2005Dialect</
item>
<item
key="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle</item>
</settings>
<assemblies>
<assembly>Ideal.Reporting.Model</assembly>
</assemblies>
<!--<contributors>
<contributor key="ReportingIdealConfigurationContributor"/>
</contributors>-->
</factory>

<factory id="sessionFactory2" alias="rttm">
<settings>
<item key="show_sql">true</item>
<item
key="connection.provider">NHibernate.Connection.DriverConnectionProvider</
item>
<item
key="connection.driver_class">NHibernate.Driver.SqlClientDriver</item>
<item key="connection.connection_string">Data
Source=.;Initial Catalog=reporting3;Integrated Security=SSPI;</item>
<item key="dialect">NHibernate.Dialect.MsSql2005Dialect</
item>
<item
key="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle</item>
</settings>
<assemblies>
<assembly>Ideal.Rttm.Model</assembly>
</assemblies>

</factory>
<!--<contributors>
<contributor key="RttmIdealConfigurationContributor"/>
</contributors>-->
</facility>

</code>


and an example contributor:

<code>

public class IdealConfigurationContributor :
IConfigurationContributor
{
public void Process(string name, Configuration config)
{
var models = new PersistenceModel();

models.AddMappingsFromAssembly(typeof(Provider).Assembly);

models.Conventions.Add(typeof
(ForeignKeyConventionOverride));
models.Configure(config);

if (AppSettings.GetConfigurationBoolean("RebuildDb",
false))
{
var export = new SchemaExport(config);
var sb = new StringBuilder();
TextWriter output = new StringWriter(sb);
export.Drop(true, true);
export.Execute(true, false, false, null, output);
export.Create(true, true);
}


}
}

</code>

i did find a conversation discussing the creation of the contributor -
which hinted at registering them and then associating them via the
following:

<contributors>
<contributor key="RttmIdealConfigurationContributor"/>
</contributors>

but contributors isn't an expected node name :(

can anyone shed some light on this for me?

cheers

w://




On 19 Oct, 11:40, Wayne Douglas <wa...@codingvista.com> wrote:
> thanks for the poiinter :)
>
> how do i register more than one IConfigurationBuilder with the
> facility config tho?
>
>
>
>
>
> On Mon, Oct 19, 2009 at 11:16 AM, Valeriu Caraulean <caraul...@gmail.com> wrote:
>
> > So, you want to use FluentNHibernate to configure factories built via
> > NHIntegration facility.
>
> > What I would try to do:
>
> >  1. Create a IConfigurationContributor for each of your domains.
> >  2. Check in contributors the name/factory-id to modify only the right
> > configuration
> >  3. Modify the configuration with FluentNHibernate.
>
> > This is just one way to achieve it.
>
> > On Mon, Oct 19, 2009 at 11:49 AM, Wayne Douglas <wa...@codingvista.com> wrote:
>
> >> hey - cheers for looking at this with me.
>
> >> not very experienced with nhib - i thought the assembly definition in
> >> the xml would point the factory to one assembly so it would only push
> >> the resulting sql creation scripts to that one db. i thought the one
> >> in the c# was just there to bootstrap it all.
>
> >> so i need to have 2 separate config builders?
>
> >> how do i wire one up with one factory configuration and the other with
> >> the other configuration?
>

Valeriu Caraulean

unread,
Oct 25, 2009, 8:06:01 AM10/25/09
to castle-pro...@googlegroups.com
Your contributors should be registered in container under
IConfigurationContributor interface. Then, the NHIntegration Facility
will do container.ResolveAll<IConfigurationContributor>() and will
call Process() on all of them.

As I suggested already, your contributors should be filtered by
factory alias in order to not be applied for your both domains.

For further details please look at the sources of NHIntegration Facility.

Wayne Douglas

unread,
Oct 25, 2009, 8:18:45 AM10/25/09
to castle-pro...@googlegroups.com
hmm - thanks for the help.

ok i have looked at the source and it's not straight forward - i've
also googled the hell out of the internet and that's not helping-
going to try using something else to get my project working. i dont
think the nh facility is what i should be using.

cheers

w://

On Sun, Oct 25, 2009 at 12:06 PM, Valeriu Caraulean <cara...@gmail.com> wrote:
> IConfigurationContributor

--
Cheers,

w://

Wayne Douglas

unread,
Oct 25, 2009, 3:47:26 PM10/25/09
to castle-pro...@googlegroups.com
fyi - i'm removing the fluent nhibernate stuff and getting slightly
closer to having it working.

though atm the following block of code only creates the db for one of
the factories:

<code>

Configuration[] cfgs = _container.ResolveAll<Configuration>();
foreach (Configuration cfg in cfgs)
{
SchemaExport export = new SchemaExport(cfg);
export.Create(true, true);
}

</code>

--
Cheers,

w://

Wayne Douglas

unread,
Oct 25, 2009, 4:37:01 PM10/25/09
to castle-pro...@googlegroups.com
This works absolutely fine without fluent. Bit gutted that it's so
hard (impossible?) to use it with fnh, proper happy I got it working.
Thanks for all the help :)

-----------------------------
e-wayne <at> isit.gd
t-07525 424 882

On 25 Oct 2009, at 12:06 PM, Valeriu Caraulean <cara...@gmail.com>
wrote:

>
>>>>>>> models.AddMappingsFromAssembly(typeof
>>>>>>> (Report).Assembly);
>>>>>>> models.Conventions.Add(typeof
Reply all
Reply to author
Forward
0 new messages