Fluent NHibernate Wiki
Im>JamesGregory
No edit summary
Im>JamesGregory
No edit summary
Line 28: Line 28:
   
 
=== ForeignKeyConvention ===
 
=== ForeignKeyConvention ===
  +
  +
The <code>ForeignKeyConvention</code> is an amalgamation of several other conventions to provide an easy way to specify the naming scheme for all foreign-keys in your domain. This is particularly useful because not all the foreign-keys are accessible in the same way, depending on where they are; this convention negates need to know about the underlying structure.
  +
  +
The only consideration that needs to be made is that for many-to-one relationships you're setting the name of the key on the current entity, while all others you're setting the key on the ''other'' entity. This manifests itself in many-to-one's receiving a <code>PropertyInfo</code> instance to build their key from, and everything else a <code>Type</code> instance.
  +
  +
<blockquote>Suggestions are welcome for this signature, as it's not exactly ideal.</blockquote>
  +
  +
<source lang="csharp">
  +
public class CustomForeignKeyConvention
  +
: ForeignKeyConvention
  +
{
  +
protected override string GetKeyName(PropertyInfo property, Type type)
  +
{
  +
if (property == null)
  +
return type.Name + "ID"; // many-to-many, one-to-many, join
  +
  +
return property.Name + "ID; // many-to-one
  +
}
  +
}
  +
</source>
  +
 
=== ManyToManyTableNameConvention ===
 
=== ManyToManyTableNameConvention ===
 
=== UserTypeConvention ===
 
=== UserTypeConvention ===

Revision as of 09:02, 13 August 2009

If you don't know what conventions are, you should read the conventions page first. Here you can find details of each of the individual conventions that you're able to create.

Your conventions are made by either deriving from a particular class supplied by Fluent NHibernate (ForeignKeyConvention for example), or by implementing one of the many interfaces we provide.

Base-classes

We provide several classes that you can derive from to help you with common conventions, naming of foreign keys, many-to-many table names; these are particularly useful when the convention may have some logic behind it that you aren't really interested in implementing.

AttributePropertyConvention<T>

The AttributePropertyConvention<T> is a special convention that only gets applied to properties that have a particular attribute on them, that attribute being defined by the generic argument T.

To use this convention, subclass it and supply the attribute as the generic argument, then implement the abstract Apply method.

public class ThereBeDragonsAttribute : Attribute
{}

public class ThereBeDragonsConvention
  : AttributePropertyConvention<ThereBeDragonsAttribute>
{
  protected override void Apply(ThereBeDragonsAttribute attribute, IPropertyInstance instance)
  {
    // do something to the instance
  }
}

ForeignKeyConvention

The ForeignKeyConvention is an amalgamation of several other conventions to provide an easy way to specify the naming scheme for all foreign-keys in your domain. This is particularly useful because not all the foreign-keys are accessible in the same way, depending on where they are; this convention negates need to know about the underlying structure.

The only consideration that needs to be made is that for many-to-one relationships you're setting the name of the key on the current entity, while all others you're setting the key on the other entity. This manifests itself in many-to-one's receiving a PropertyInfo instance to build their key from, and everything else a Type instance.

Suggestions are welcome for this signature, as it's not exactly ideal.

public class CustomForeignKeyConvention
  : ForeignKeyConvention
{
  protected override string GetKeyName(PropertyInfo property, Type type)
  {
    if (property == null)
      return type.Name + "ID";  // many-to-many, one-to-many, join
    
    return property.Name + "ID; // many-to-one
  }
}

ManyToManyTableNameConvention

UserTypeConvention

Interfaces

There are many interfaces available to implement for your conventions, you can find them all in the FluentNHibernate.Conventions namespace; below are the most common ones that you're likely to need, most of the other ones are pretty esoteric. You can see the full list in the Conventions folder on github.

IClassConvention

Applied individually to each class mapping. Implement this interface to customise things like table names, default-insert/update parameters, caching, locking etc... Anything that would be set just on the class.

IIdConvention

Applied individually to each Identity in your mappings. Implement this interface if you need to customise only identities.

IPropertyConvention

Applied individually to each Property in your mappings. Implement this interface if you need to customise only properties, good for customising column names and access strategies.

IVersionConvention

Applied individually to each Version in your mappings. Implement this interface if you need to customise only versions.

IJoinConvention

Applied individually to each Join in your mappings. Implement this interface if you need to customise only join mappings. It's important to know that joins can contain other IMappingParts, so any implementors of IMappingPartConvention will also get called for any children of a join.

IComponentConvention

Applied individually to each Component in your mappings. Implement this interface if you need to customise only component mappings. It's important to know that components can contain other IMappingParts, so any implementors of IMappingPartConvention will also get called for any children of a component.

IDynamicComponentConvention

Applied individually to each Dynamic Component in your mappings. Implement this interface if you need to customise only dynamic component mappings. It's important to know that dynamic components can contain other IMappingParts, so any implementors of IMappingPartConvention will also get called for any children of a dynamic component.

IHasOneConvention

Applied individually to each one-to-one relationship in your mappings. Implement this interface if you need to customise all HasOne relationships.

IHasManyConvention

Applied individually to each one-to-many relationship in your mappings. Implement this interface if you need to customise all HasMany relationships.

IHasManyToManyConvention

Applied individually to each many-to-many relationship in your mappings. Implement this interface if you need to customise all HasManyToMany relationships.

IReferenceConvention

Applied individually to each many-to-one relationship in your mappings. Implement this interface if you need to customise all Reference relationships.