<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marcin Obel&#187; NHibernate</title>
	<atom:link href="http://marcinobel.com/index.php/tag/nhibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://marcinobel.com</link>
	<description>.NET, ASP.NET MVC, jQuery, Ruby, Ruby on Rails, Test Driven Development, Agile</description>
	<lastBuildDate>Thu, 20 May 2010 14:25:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Fluent NHibernate conventions II &#8211; example for enumerations</title>
		<link>http://marcinobel.com/index.php/fluent-nhibernate-conventions-ii-enumerations/</link>
		<comments>http://marcinobel.com/index.php/fluent-nhibernate-conventions-ii-enumerations/#comments</comments>
		<pubDate>Thu, 20 May 2010 14:25:36 +0000</pubDate>
		<dc:creator>Marcin Obel</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://marcinobel.com/?p=306</guid>
		<description><![CDATA[Most of the time I do not have to touch Fluent NHibernate conventions I have created some time ago. But from time to time I find in mappings some common scenario which is a good candidate to become a convention. This time I found that every time I am mapping a property of enumeration type [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-210" style="margin-right: 10px;" title="Fluent NHibernate" src="http://marcinobel.com/wp-content/uploads/fluent-nhibernate.png" alt="" width="360" height="117" />Most of the time I do not have to touch <strong><a title="Fluent NHibernate" href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a></strong> conventions I have created some time ago. But from time to time I find in mappings some common scenario which is a good candidate to become a convention. This time I found that every time I am mapping a property of enumeration type I have to specify its length manually. </p>
<p>Because I hate redundant work I have defined a <strong>Fluent NHibernate convention</strong> presented below.</p>
<pre class="brush: csharp;">
using System;
using System.Linq;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;

namespace ExampleConventions
{
    public class EnumColumnLengthConvention
        : IPropertyConvention, IPropertyConventionAcceptance
    {
        public void Accept(IAcceptanceCriteria&lt;IPropertyInspector&gt; criteria)
        {
            criteria.Expect(x =&gt; x.Property.PropertyType.IsEnum)
                .Expect(x =&gt; x.Length == 0);
        }

        public void Apply(IPropertyInstance instance)
        {
            var names = Enum.GetNames(instance.Property.PropertyType);
            instance.Length(names.Max(name =&gt; name.Length));
        }
    }
}
</pre>
<p>The convention is really simple. Its whole logic can be found in Apply method and I think it is self explanatory so there is nothing to describe here.</p>
]]></content:encoded>
			<wfw:commentRss>http://marcinobel.com/index.php/fluent-nhibernate-conventions-ii-enumerations/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Fluent NHibernate conventions &#8211; examples</title>
		<link>http://marcinobel.com/index.php/fluent-nhibernate-conventions-examples/</link>
		<comments>http://marcinobel.com/index.php/fluent-nhibernate-conventions-examples/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 22:22:59 +0000</pubDate>
		<dc:creator>Marcin Obel</dc:creator>
				<category><![CDATA[.Net Framework]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://marcinobel.com/?p=195</guid>
		<description><![CDATA[Fluent NHibernate is my favorite extension for NHibernate. I am using it since early betas and I have to say that I love it. One of its underestimated features are conventions. I decided to extract some of them from one of my projects and provide real life examples how they can be used. My conventions [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://marcinobel.com/wp-content/uploads/fluent-nhibernate.png"><img class="alignleft size-full wp-image-210" title="Fluent NHibernate" src="http://marcinobel.com/wp-content/uploads/fluent-nhibernate.png" alt="Fluent NHibernate" width="360" height="117" /></a><strong><a title="Fluent NHibernate" href="http://fluentnhibernate.org/" target="_blank">Fluent NHibernate</a></strong> is my favorite extension for <strong><a title="NHibernate" href="http://nhforge.org/" target="_blank">NHibernate</a></strong>. I am using it since early betas and I have to say that I love it. One of its underestimated features are conventions. I decided to extract some of them from one of my projects and provide real life examples how they can be used. My conventions are listed below but if you need more information visit <strong><a title="Fluent NHibernate Documentation" href="http://wiki.fluentnhibernate.org/Conventions" target="_blank">Fluent NHibernate Wiki</a><span style="font-weight: normal;"> where this feature is described in detail</span></strong>.</p>
<p><strong>ColumnNullabilityConvention</strong> &#8211; says that if nullability for column has not been specified explicitly, should be set to &#8220;NOT NULL&#8221;.</p>
<pre class="brush: csharp;">
public class ColumnNullabilityConvention
    : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria&lt;IPropertyInspector&gt; criteria)
    {
        criteria.Expect(x =&gt; x.Nullable, Is.Not.Set);
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.Not.Nullable();
    }
}
</pre>
<p><strong>CreatedAtPropertyAccessConvention</strong> &#8211; says that every property named <span style="text-decoration: underline;">CreatedAt </span>should be accessed through camel case field hidden behind it.</p>
<pre class="brush: csharp;">
public class CreatedAtPropertyAccessConvention
    : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria&lt;IPropertyInspector&gt; criteria)
    {
        criteria.Expect(x =&gt; x.Property.Name == &quot;CreatedAt&quot;);
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.Access.ReadOnlyPropertyThroughCamelCaseField(
            CamelCasePrefix.Underscore);
    }
}
</pre>
<p><strong>ForeignKeyConstraintNameConvention</strong> &#8211; says that name of every foreign key constraint representing one to many relation should consist of names of entities for which it was specified.</p>
<pre class="brush: csharp;">
public class ForeignKeyConstraintNameConvention
    : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.ForeignKey(&quot;{0}_{1}_FK&quot;.AsFormat(
        instance.Member.Name, instance.EntityType.Name));
    }
}
</pre>
<p><strong>ForeignKeyNameConvention</strong> &#8211; says that name of every column containing foreign key id should consist of name of type which it points to with &#8220;Id&#8221; suffix.</p>
<pre class="brush: csharp;">
public class ForeignKeyNameConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.Column(instance.EntityType.Name + &quot;Id&quot;);
    }
}
</pre>
<p><strong>PrimaryKeyNameConvention </strong>- says that name of every column representing primary key should consist of entity name and &#8220;Id&#8221; suffix.</p>
<pre class="brush: csharp;">
public class PrimaryKeyNameConvention : IIdConvention
{
    public void Apply(IIdentityInstance instance)
    {
        instance.Column(instance.EntityType.Name + &quot;Id&quot;);
    }
}
</pre>
<p><strong>ReferenceConvention</strong> &#8211; says that name of column referenced in many to one convention should consist of entity name and &#8220;Id&#8221; suffix.</p>
<pre class="brush: csharp;">
public class ReferenceConvention : IReferenceConvention
{
    public void Apply(IManyToOneInstance instance)
    {
        instance.Column(instance.Property.PropertyType.Name + &quot;Id&quot;);
    }
}
</pre>
<p><strong>StringColumnLengthConvention</strong> &#8211; says that if length for string column has not been specified, it should be set to 100.</p>
<pre class="brush: csharp;">
public class StringColumnLengthConvention
    : IPropertyConvention, IPropertyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria&lt;IPropertyInspector&gt; criteria)
    {
        criteria.Expect(x =&gt; x.Type == typeof(string))
            .Expect(x =&gt; x.Length == 0);
    }

    public void Apply(IPropertyInstance instance)
    {
        instance.Length(100);
    }
}
</pre>
<p><strong>TableNameConvention</strong> &#8211; says that if name for table has not been specified, it should be created using concatenation of entity name and &#8220;s&#8221; suffix.</p>
<pre class="brush: csharp;">
public class TableNameConvention
    : IClassConvention, IClassConventionAcceptance
{
    public void Accept(IAcceptanceCriteria&lt;IClassInspector&gt; criteria)
    {
        criteria.Expect(x =&gt; x.TableName, Is.Not.Set);
    }

    public void Apply(IClassInstance instance)
    {
        instance.Table(instance.EntityType.Name + &quot;s&quot;);
    }
}
</pre>
<p><strong>At the end the most important thing</strong> which shows that such conventions make sense. With default <strong>Fluent NHibernate</strong> conventions DDL generated by NHibernate for MySQL looked like this:</p>
<pre class="brush: sql;">
alter table `Athlete`  drop foreign key FK9221C9B94070A6F0

drop table if exists Countries

drop table if exists `Athlete`

create table Countries (
     Id INTEGER NOT NULL AUTO_INCREMENT,
     Name VARCHAR(255),
     primary key (Id)
)

create table `Athlete` (
     Id INTEGER NOT NULL AUTO_INCREMENT,
     DisplayName VARCHAR(255),
     Email VARCHAR(255),
     Password VARCHAR(255),
     CreatedAt DATETIME,
     IsActive TINYINT(1),
     Country_id INTEGER,
     primary key (Id)
)

alter table `Athlete`
	add index (Country_id),
	add constraint FK9221C9B94070A6F0
	foreign key (Country_id)
	references Countries (Id)
</pre>
<p>When I have applied conventions mentioned above to my fluent mappings DDL looks like this:</p>
<pre class="brush: sql;">
alter table Athletes  drop foreign key Athletes_Country_FK

drop table if exists Athletes

drop table if exists Countries

create table Athletes (
     AthleteId INTEGER NOT NULL AUTO_INCREMENT,
     DisplayName VARCHAR(100) not null,
     Email VARCHAR(100) not null,
     Password VARCHAR(100) not null,
     CreatedAt DATETIME not null,
     IsActive TINYINT(1) not null,
     CountryId INTEGER,
     primary key (AthleteId)
)

create table Countries (
     CountryId INTEGER NOT NULL AUTO_INCREMENT,
     Name VARCHAR(100) not null,
     primary key (CountryId)
)

alter table Athletes
	add index (CountryId),
	add constraint Athletes_Country_FK
	foreign key (CountryId)
	references Countries (CountryId)
</pre>
<p>For me it looks much better. Additionally I have made it once and that is it. Now I have to care only about domain model and business logic without thinking about not so important stuff lake names of database objects.</p>
]]></content:encoded>
			<wfw:commentRss>http://marcinobel.com/index.php/fluent-nhibernate-conventions-examples/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
