Ignore a property when mapping by code. NH3.2

841 views
Skip to first unread message

Michael DELVA

unread,
Apr 20, 2011, 5:37:51 PM4/20/11
to nhu...@googlegroups.com
Hello all,

I couldn't find any method to make the convention mapper ignore some properties for my entities.

Is it possible?

Thanks in advance

Michael

Fabio Maulo

unread,
Apr 20, 2011, 5:44:38 PM4/20/11
to nhu...@googlegroups.com
yes it is.
Send here your example and the prop you want ignore.
Please send the real impl. of that prop in your domain and the reason because you want ignore it.
I have to check why my patterns have not discovered it... if it is only a matter of read your mind there is no problem, but if it can be discovered I would do it by default. 

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
Fabio Maulo

Michael DELVA

unread,
Apr 20, 2011, 7:40:21 PM4/20/11
to nhu...@googlegroups.com
Fabio,

Disclaimer: I don't know if what I show below is the most correct way, so please don't hurt ;)

I have this entity:

public class Action : BaseEntity
{
    private IStatistic statistic;
    private int statisticId;
        
    public int StatisticId
    {
        get { return statisticId; }
        set
        {
            statisticId = value;

            statistic = StatisticsFactory.Get(value);
        }
    }
       
    public IStatistic Statistic
    {
        get { return statistic; }
        set
        {
            statistic = value;

            statisticId = value.Id;
        }
    }
}

I'd like to map the Statistic field to the database, and not the IStatistic object.

How could I do that?

Thanks in advance

Fabio Maulo

unread,
Apr 21, 2011, 7:56:23 AM4/21/11
to nhu...@googlegroups.com
With ModelMapper is enough avoiding the map of that property.
With ConventionModelMapper (or when you inject the SimpleModelInspector to the ModelMapper) you can use this:
public void IsPersistentProperty(Func<MemberInfo, bool, bool> match)

There are other options but is too large...

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
Fabio Maulo

Michael DELVA

unread,
Apr 21, 2011, 8:39:27 AM4/21/11
to nhu...@googlegroups.com
Thanks for your answer.

Using the same domain as my previous post, I added this to the ConventionModelMapper:

mapper.IsPersistentProperty((mi,declared) =>
{
if (mi.DeclaringType == typeof(Action) && mi.Name == "Statistic")
return false;

return true;
});

But I still get this exception (with the StackTrace) when I want to export the schema:

NHibernate.MappingException: Could not determine type for: TestsMappingsNH.Entities.IStatistic, TestsMappingsNH, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: 

NHibernate.Mapping.Column(<Statistic>k__BackingField)
at NHibernate.Mapping.SimpleValue.get_Type() in SimpleValue.cs: line 239
at NHibernate.Mapping.Column.GetSqlTypeCode(IMapping mapping) in Column.cs: line 392
at NHibernate.Mapping.Column.GetDialectTypeName(Dialect dialect, IMapping mapping) in Column.cs: line 219
at NHibernate.Mapping.Column.GetSqlType(Dialect dialect, IMapping mapping) in Column.cs: line 208
at NHibernate.Mapping.Table.SqlCreateString(Dialect dialect, IMapping p, String defaultCatalog, String defaultSchema) in Table.cs: line 372
at NHibernate.Cfg.Configuration.GenerateSchemaCreationScript(Dialect dialect) in Configuration.cs: line 895
at NHibernate.Tool.hbm2ddl.SchemaExport.Initialize() in SchemaExport.cs: line 67
at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop) in SchemaExport.cs: line 296
at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean export, Boolean justDrop) in SchemaExport.cs: line 290
at NHibernate.Tool.hbm2ddl.SchemaExport.Create(Boolean script, Boolean export) in SchemaExport.cs: line 105
at TestsMappingsNH.Tests.FactMethodName() in Program.cs: line 53 

Fabio Maulo

unread,
Apr 21, 2011, 8:47:06 AM4/21/11
to nhu...@googlegroups.com
Two pointd:
1)
mapper.IsPersistentProperty((mi,declared) =>
{
if (mi.DeclaringType == typeof(Action) && mi.Name.Tolower() == "statistic")
return false;

return true;
});
2) what you sent here is not what you are trying to map because in your real case  you have an autoproperty.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
Fabio Maulo

Michael DELVA

unread,
Apr 21, 2011, 9:04:32 AM4/21/11
to nhu...@googlegroups.com
Two pointd:
1)
mapper.IsPersistentProperty((mi,declared) =>
{
if (mi.DeclaringType == typeof(Action) && mi.Name.Tolower() == "statistic")
return false;

return true;
});
2) what you sent here is not what you are trying to map because in your real case  you have an autoproperty.

1) I had to add a check for PropertyInfo, otherwise the schema was invalid (it was trying to set backing fields as columns)

mapper.IsPersistentProperty((mi,declared) =>
{
if (mi.DeclaringType == typeof(Action) && mi is PropertyInfo && mi.Name.ToLower() == "statistic")
return false;

return true;
}); 

2) I have these entities:

public class Action : BaseEntity
{
    private IStatistic statistic;
    private int statisticId;
        
    public int StatisticId
    {
        get { return statisticId; }
        set
        {
            statisticId = value;

            statistic = StatisticsFactory.Get(value);
        }
    }
       
    public IStatistic Statistic
    {
        get { return statistic; }
        set
        {
            statistic = value;

            statisticId = value.Id;
        }
    }
}

And I always get this error, despite the IsPersistentProperty method returning false for the ignored property...

Could not determine type for: TestsMappingsNH.Entities.IStatistic, TestsMappingsNH, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(statistic)

   at NHibernate.Mapping.SimpleValue.get_Type() in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Mapping\SimpleValue.cs:line 239
   at NHibernate.Mapping.Column.GetSqlTypeCode(IMapping mapping) in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Mapping\Column.cs:line 392
   at NHibernate.Mapping.Column.GetDialectTypeName(Dialect dialect, IMapping mapping) in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Mapping\Column.cs:line 219
   at NHibernate.Mapping.Column.GetSqlType(Dialect dialect, IMapping mapping) in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Mapping\Column.cs:line 208
   at NHibernate.Mapping.Table.SqlCreateString(Dialect dialect, IMapping p, String defaultCatalog, String defaultSchema) in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Mapping\Table.cs:line 372
   at NHibernate.Cfg.Configuration.GenerateSchemaCreationScript(Dialect dialect) in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Cfg\Configuration.cs:line 895
   at NHibernate.Tool.hbm2ddl.SchemaExport.Initialize() in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaExport.cs:line 67
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop) in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaExport.cs:line 296
   at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean export, Boolean justDrop) in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaExport.cs:line 286
   at NHibernate.Tool.hbm2ddl.SchemaExport.Create(Boolean script, Boolean export) in D:\__PROG STUFF__\VS\2010\Projects\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaExport.cs:line 105
   at TestsMappingsNH.Tests.FactMethodName() in D:\__PROG STUFF__\VS\2010\Projects\Tests\New folder\TestsMappingsNH\Program.cs:line 53

Fabio Maulo

unread,
Apr 21, 2011, 9:12:59 AM4/21/11
to nhu...@googlegroups.com
remove the check on PropertyInfo. Fields are candidates to be persisted.
The ICandidatePersistentMembersProvider, injected to the ModelMapper, is the responsible to determine candidate members to be persisted.
Given a class the DefaultCandidatePersistentMembersProvider will return properties and fields.
The default impl. of IsPersistentProperty is
 (mi, declared) => declared || ((mi is PropertyInfo) && MatchNoReadOnlyPropertyPattern(mi));

I can't check your class right now (I'm in the middle of something); I'll check it later today or tomorrow.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
Fabio Maulo

Fabio Maulo

unread,
Apr 21, 2011, 9:20:38 AM4/21/11
to nhu...@googlegroups.com
ah!! Michael,
Thanks to let me see that there are some place to improve ;).

In ConfORM the answer is easy but in NH I have simplified it and perhaps not everything was so simplified LOL!!
In ConfORM is:
orm.ExcludeProperty<Action>(x=> x.Statistic);
--
Fabio Maulo

Michael DELVA

unread,
Apr 21, 2011, 9:23:44 AM4/21/11
to nhu...@googlegroups.com
I'm glad to have found some more work for you to do :)

So I have to use the ModelMapper while you release a new version with the fix?

PS: What about the something you were into the middle? ;)

Fabio Maulo

unread,
Apr 21, 2011, 9:30:51 AM4/21/11
to nhu...@googlegroups.com
LOL!! 
It is only about code, are not two legs.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
Fabio Maulo

Reply all
Reply to author
Forward
0 new messages