Mixing mapping types

16 views
Skip to first unread message

Steve Mason

unread,
Mar 3, 2009, 12:42:04 PM3/3/09
to S#arp Architecture
Hi,

I'd like to mix a fluent mapping file with the default AutoMapping
functionality, but I'm coming across the following error:

System.ArgumentException: Object of type
'MyProject.Data.NHibernateMaps.FosterFormMapping' cannot be converted
to type 'FluentNHibernate.AutoMap.AutoMap`1
[MyProject.Core.Domain.FosterForm]'.

at System.RuntimeType.CheckValue(Object value, Binder binder,
CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters,
Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature
sig)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at
FluentNHibernate.Utils.InvocationHelper.InvokeGenericMethodWithDynamicTypeArguments<T>
(T target, Expression`1 expression, Object[] methodArguments, Type[]
typeArguments)
at FluentNHibernate.AutoMap.AutoPersistenceModel.MergeMap(Type type,
Object mapping)
at FluentNHibernate.AutoMap.AutoPersistenceModel.CompileMappings()
at FluentNHibernate.AutoMap.AutoPersistenceModel.Configure
(Configuration configuration)
at FluentNHibernate.ConfigurationHelper.AddAutoMappings(Configuration
configuration, AutoPersistenceModel model)
at SharpArch.Data.NHibernate.NHibernateSession.AddMappingAssembliesTo
(Configuration cfg, ICollection`1 mappingAssemblies,
AutoPersistenceModel autoPersistenceModel) in d:\_development
\SharpArchitecture\src\SharpArch\SharpArch.Data\NHibernate
\NHibernateSession.cs: line 128
at SharpArch.Data.NHibernate.NHibernateSession.Init(ISessionStorage
storage, String[] mappingAssemblies, AutoPersistenceModel
autoPersistenceModel, String cfgFile, IDictionary`2 cfgProperties,
String validatorCfgFile) in d:\_development\SharpArchitecture\src
\SharpArch\SharpArch.Data\NHibernate\NHibernateSession.cs: line 66
at SharpArch.Data.NHibernate.NHibernateSession.Init(ISessionStorage
storage, String[] mappingAssemblies, AutoPersistenceModel
autoPersistenceModel, String cfgFile) in d:\_development
\SharpArchitecture\src\SharpArch\SharpArch.Data\NHibernate
\NHibernateSession.cs: line 48
at Tests.Data.NHibernateMaps.MappingIntegrationTests.SetUp() in
MappingIntegrationTests.cs: line 26

All I've done is define a class in my mapping assembly that implements
ClassMap<FosterForm> (which S#arp has nicely picked up for me). If I
remove this class mapping then the AutoMapping kicks in and everything
works, but I want a different format for the Root entity (FosterForm)
and it's quite complex, so I don't want to use
ForTypesThatDeriveFrom<>.

I'm aware this might be a Fluent NHibernate issue, if so let me know
and I'll re-post over there. I'm running the Trunk version of S#arp,
I haven't tried mapping the additional class using hbm's (yet).

Steve Mason

unread,
Mar 3, 2009, 12:44:50 PM3/3/09
to S#arp Architecture

Billy

unread,
Mar 3, 2009, 10:53:53 PM3/3/09
to S#arp Architecture
Hi Steve,

Does the following assist: check out the "Mixed fluent mappings and
auto mappings" section at http://blog.jagregory.com/2009/02/03/fluent-nhibernate-configuring-your-application/

Billy

On Mar 3, 10:44 am, Steve Mason <smaso...@gmail.com> wrote:
> Hi,
>
> I'd like to mix a fluent mapping file with the default AutoMapping
> functionality, but I'm coming across the following error:
>
> System.ArgumentException: Object of type
> 'MyProject.Data.NHibernateMaps.FosterFormMapping' cannot be converted
> to type 'FluentNHibernate.AutoMap.AutoMap`1
> [MyProject.Core.Domain.FosterForm]'.
>
> at System.RuntimeType.CheckValue(Object value, Binder binder,
> CultureInfo culture, BindingFlags invokeAttr)
> at System.Reflection.MethodBase.CheckArguments(Object[] parameters,
> Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature
> sig)
> at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
> invokeAttr, Binder binder, Object[] parameters, CultureInfo culture,
> Boolean skipVisibilityChecks)
> at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
> invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
> at
> FluentNHibernate.Utils.InvocationHelper.InvokeGenericMethodWithDynamicTypeA­rguments<T>

Steve Mason

unread,
Mar 4, 2009, 4:50:59 AM3/4/09
to sharp-architecture
Yes, I've seen that and managed to work around the issue. Though I think that the main problem now is that it just doesn't work "Out of the box" where it appears to be engineered in such a way that you can just drop in your ClassMaps to the Data project and automagically have them picked up and made to override the AutoMap stuff?

Here is my workaround for anyone interested, it uses Fluently.Configure from the article Billy mentioned:

            var cfg = new Configuration().Configure();
            var sessionFactory = Fluently.Configure(cfg)
                .Database(...) // Optional
                .Mappings(m =>
                            {
                                m.HbmMappings.AddFromAssemblyOf<FosterFormMapping>();
                                m.FluentMappings.AddFromAssemblyOf<FosterFormMapping>();
                                m.AutoMappings.Add(new AutoPersistenceModelGenerator().Generate());
                            }).BuildSessionFactory();

            NHibernateSession.Storage = new SimpleSessionStorage();
            NHibernateSession.SessionFactory = sessionFactory;

Thanks,
Steve

2009/3/4 Billy <wmcca...@gmail.com>

Billy

unread,
Mar 4, 2009, 9:22:55 AM3/4/09
to S#arp Architecture
Thank you Steve! I will add your direction to the documentation.

Billy


On Mar 4, 2:50 am, Steve Mason <smaso...@gmail.com> wrote:
> Yes, I've seen that and managed to work around the issue. Though I think
> that the main problem now is that it just doesn't work "Out of the box"
> where it appears to be engineered in such a way that you can just drop in
> your ClassMaps to the Data project and automagically have them picked up and
> made to override the AutoMap stuff?
>
> Here is my workaround for anyone interested, it uses Fluently.Configure from
> the article Billy mentioned:
>
>             var cfg = new Configuration().Configure();
>             var sessionFactory = Fluently.Configure(cfg)
>                 .Database(...) // Optional
>                 .Mappings(m =>
>                             {
>
> m.HbmMappings.AddFromAssemblyOf<FosterFormMapping>();
>
> m.FluentMappings.AddFromAssemblyOf<FosterFormMapping>();
>                                 m.AutoMappings.Add(new
> AutoPersistenceModelGenerator().Generate());
>                             }).BuildSessionFactory();
>
>             NHibernateSession.Storage = new SimpleSessionStorage();
>             NHibernateSession.SessionFactory = sessionFactory;
>
> Thanks,
> Steve
>
> 2009/3/4 Billy <wmccaffe...@gmail.com>
>
>
>
> > Hi Steve,
>
> > Does the following assist:  check out the "Mixed fluent mappings and
> > auto mappings" section at
> >http://blog.jagregory.com/2009/02/03/fluent-nhibernate-configuring-yo...

Steve Mason

unread,
Mar 5, 2009, 8:46:23 AM3/5/09
to sharp-arc...@googlegroups.com
Hi Billy,

Attached is a patch that changes the base NHibernateSession.Init method to use the Fluently.Configure method directly, as this appears to be the recommended way of configuring Fluent now. This has the effect of fixing my previous issue as well.

I'm using it now and it works, though I must admit that I haven't tried a "traditional" automapped application with it yet.

Steve

2009/3/4 Billy <wmcca...@gmail.com>
UseFluentlyToConfigureSession.patch

Billy

unread,
Mar 9, 2009, 1:45:07 AM3/9/09
to S#arp Architecture
Great patch Steve...thank you for submitting it! This will be
included in the next check in that I do and in the RC 2 release. I
hope to have that wrapped up by Monday evening.

Billy McCafferty


On Mar 5, 7:46 am, Steve Mason <smaso...@gmail.com> wrote:
> Hi Billy,
>
> Attached is a patch that changes the base NHibernateSession.Init method to
> use the Fluently.Configure method directly, as this appears to be the
> recommended way of configuring Fluent now. This has the effect of fixing my
> previous issue as well.
>
> I'm using it now and it works, though I must admit that I haven't tried a
> "traditional" automapped application with it yet.
>
> Steve
>
> 2009/3/4 Billy <wmccaffe...@gmail.com>
>  UseFluentlyToConfigureSession.patch
> 5KViewDownload

Berryl Hesh

unread,
Mar 11, 2009, 11:15:19 PM3/11/09
to S#arp Architecture
Hi Guys / Steve

Does anyone have a set of unit tests for NHibernateSession?

Thx
Berryl

Billy

unread,
Mar 15, 2009, 6:33:13 PM3/15/09
to S#arp Architecture
Hi Berryl,

Specific unit tests do not exist for this class within
SharpArch.Tests. This class does undergo manual testing and has a
number of indirect unit tests via the tests within the Northwind
sample code. You're most welcome to provide a patch to include some
that directly unit tests this class if you have implementation ideas
for them.

Thanks!
Billy
> > > > > > > I'd like to mix a fluentmappingfile with the default AutoMapping
> > > > > > > All I've done is define a class in mymappingassembly that
> > > > implements
> > > > > > > ClassMap<FosterForm> (which S#arp has nicely picked up for me).  If I
> > > > > > > remove this classmappingthen the AutoMapping kicks in and
> > > > everything
> > > > > > > works, but I want a different format for the Root entity (FosterForm)
> > > > > > > and it's quite complex, so I don't want to use
> > > > > > > ForTypesThatDeriveFrom<>.
>
> > > > > > > I'm aware this might be a Fluent NHibernate issue, if so let me know
> > > > > > > and I'll re-post over there.  I'm running the Trunk version of S#arp,
> > > > > > > I haven't triedmappingthe additional class using hbm's (yet).
>
> > >  UseFluentlyToConfigureSession.patch
> > > 5KViewDownload
Reply all
Reply to author
Forward
0 new messages