Overrides versus Conventions

58 views
Skip to first unread message

ag

unread,
Jun 11, 2012, 12:11:38 PM6/11/12
to Fluent NHibernate
Is there some good documentation about the relation between overrides
and conventions ? Especially in the sequence that they are called.
I looked around and I found documentation about each one seperately
not about how they interact.

On debugging I discovered to my surprise that my overrides are called
first and then the conventions are called.
There is probably a reason for this, so if someone can explain this or
point to some documentation link, I would appreciate it. I'm probably
missing some important concept.

The reason I would expect it to be first conventions and then
overrides, Is that I thought conventions are extenstions/overrides to
the default automapping and for the cases in which the convention
would yield incorrect results, you could still use an override.



Kind regards,
Alex

Carl Bussema

unread,
Jun 11, 2012, 2:44:55 PM6/11/12
to fluent-n...@googlegroups.com
Your overrides should take effect if you are EXPLICIT about them. If you give an override that says Map(x => x.Stuff).ReadOnly(); it's still going to use your ColumnNameConvention. But if you say 
Map(x => x.Stuff).Column("AwesomeStuff").ReadOnly(); then you have overriden your convention.


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


ag

unread,
Jun 12, 2012, 5:38:46 AM6/12/12
to fluent-n...@googlegroups.com
Hi Carl,
as a result of your reply I did some more investigation.
And indeed what you say is correct for the Column name, and most properties.
When looking in the source code I found some layered system for storing attributes.
From which I conclude that indeed changed properties in overrides should win from changes done by conventions.
But I think I might have found a bug for the property GeneratedBy.
Can this be confirmed by someone ? Or how do I proceed with this
 
 
When I do a :
GeneratedBy.Sequence("somesequencename")
in a override and in a convention IIdConvention 
then I receive an exception:
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
Additional information: An item with the same key has already been added.
 
The reason is that the convention (being called after the override !!!) is accessing the Generator mapping of the highest layer at that moment (since overrides are higher and has been called it will get an already defined sequence mapping) and tries to add the same parameter name to a map.
I suppose that when GeneratedBy is called from a convention that we need to get a Generator instance with mapping of the Convention layer and not just the highest at that moment.
Here is some source code I suspect to be the cause :
 
 

namespace FluentNHibernate.Conventions.Instances

{
    
public class IdentityInstance : IdentityInspectorIIdentityInstance

...

 

    public IGeneratorInstance GeneratedBy

        {
            
get
            {
                mapping.Set(x => x.Generator, 
Layer.Conventions, new GeneratorMapping());
                 
                
return new GeneratorInstance(mapping.Generator, mapping.Type.GetUnderlyingSystemType());
            }
        }

 
The line : 
  
return new GeneratorInstance(mapping.Generator, mapping.Type.GetUnderlyingSystemType());
 
calls mapping.Generator which retrieves the generator mapping with highest level.
For explanation see the following snippets:
 
 
 

   public GeneratorMapping Generator
        {
            
get { return attributes.GetOrDefault<GeneratorMapping>("Generator"); }
        }

 

 

  public static class AttributeStoreExtensions
    {
        
public static T GetOrDefault<T>(this AttributeStore store, string attribute)
        {
            
return (T)(store.Get(attribute) ?? default(T));
        }
    }

 

 

    [Serializable]
    
public class AttributeStore
    {
       ...
 
        
public object Get(string property)
        {
            
var values = layeredValues[property];
 
            
if (!values.Any())
                
return null;
 
            
var topLayer = values.Max(x => x.Key);
 
            
return values[topLayer];
        }

 
 
 
 
 
 
 
Op maandag 11 juni 2012 20:44:55 UTC+2 schreef Carl B het volgende:
Your overrides should take effect if you are EXPLICIT about them. If you give an override that says Map(x => x.Stuff).ReadOnly(); it's still going to use your ColumnNameConvention. But if you say 
Map(x => x.Stuff).Column("AwesomeStuff").ReadOnly(); then you have overriden your convention.
 
 
On Mon, Jun 11, 2012 at 12:11 PM, ag <alexg...@gmail.com> wrote:
 
Is there some good documentation about the relation between overrides
and conventions ? Especially in the sequence that they are called.
I looked around and I found documentation about each one seperately
not about how they interact.
 
On debugging I discovered to my surprise that my overrides are called
first and then the conventions are called.
There is probably a reason for this, so if someone can explain this or
point to some documentation link, I would appreciate it. I'm probably
missing some important concept.
 
The reason I would expect it to be first conventions and then
overrides, Is that I thought conventions are extenstions/overrides to
the default automapping and for the cases in which the convention
would yield incorrect results, you could still use an override.
 
 
 
Kind regards,
Alex
 
--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-nhibernate@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibernate+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages