Skip to content

Instantly share code, notes, and snippets.

@ToJans
Created February 16, 2010 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ToJans/305454 to your computer and use it in GitHub Desktop.
Save ToJans/305454 to your computer and use it in GitHub Desktop.
problem with automapping components in fluent nhibernate
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FluentNhibExample.Model.Component
{
public class Address
{
public virtual string Street { get; set; }
public virtual string Nr { get; set; }
public virtual City City { get; set; }
public virtual EmailAddress Email { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcExtensions.Model;
namespace FluentNhibExample.Model
{
public class City : IModelId
{
public virtual int Id { get; set; }
public virtual string Zip { get; set; }
public virtual string Name { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcExtensions.Model;
namespace FluentNhibExample.Model
{
public class Contact : IModelId
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Component.Coordinate Coordinate { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FluentNhibExample.Model.Component
{
class Coordinate
{
public class Coordinate
{
public virtual Address Home { get; set; }
public virtual Address Work { get; set; }
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace FluentNhibExample.Model.Component
{
public class EmailAddress
{
protected Regex MyRegex = new Regex(@"[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}");
private string val;
public virtual string Value
{
get
{
return val;
}
set
{
if (value != null)
{
value = value.ToLowerInvariant();
if (!MyRegex.Match(value).Success)
throw (new ArgumentOutOfRangeException("Invalid email address"));
}
val = value;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcExtensions.Services;
using MvcExtensions.Services.Impl.FluentNHibernate;
// Example which shows a problem with fluentnhibernate component mapping:
// column name should be "Coordinate[Home/Work]AddressEmailValue" instead of just "EmailValue"
// To compile this you need the latest MvcExtensions:
// - source code github: http://github.com/ToJans/MVCExtensions
// - binaries: http://github.com/downloads/ToJans/MVCExtensions/MvcExtensions20100216.zip
namespace FluentNhibExample
{
class Program
{
private class MyDomainDefinition : IDomainDefinition
{
#region IDomainDefinition Members
public System.Reflection.Assembly DomainAssembly
{
get { return this.GetType().Assembly; }
}
public DomainType GetDomainType(Type t)
{
if (t.Namespace == typeof(Model.City).Namespace)
return DomainType.Class;
else if (t.Namespace ==typeof(Model.Component.Address).Namespace)
return DomainType.Component;
else
return DomainType.None;
}
// returning null will not write any mapping files
public string WriteHbmFilesToPath { get { return "."; } }
#endregion
}
static void Main(string[] args)
{
var x = new SqlLiteDatabase("test.db", new MyDomainDefinition());
Console.WriteLine("mappings created in " + System.IO.Directory.GetCurrentDirectory());
Console.ReadLine();
// this will crash, since the components do not map the way they should
x.CreateDB();
Console.WriteLine("SqlLite Database created");
Console.ReadLine();
}
}
}
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true">
<class xmlns="urn:nhibernate-mapping-2.2" name="FluentNhibExample.Model.Contact, FluentNhibExample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`Contact`">
<id name="Id" type="System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
<component name="Coordinate" insert="true" update="true" optimistic-lock="true">
<component name="Home" insert="true" update="true" optimistic-lock="true">
<component name="Email" insert="true" update="true" optimistic-lock="true">
<property name="Value" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="EmailValue" />
</property>
</component>
<property name="Street" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="HomeStreet" />
</property>
<property name="Nr" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="HomeNr" />
</property>
<many-to-one cascade="save-update" class="FluentNhibExample.Model.City, FluentNhibExample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="City">
<column name="City_id" />
</many-to-one>
</component>
<component name="Work" insert="true" update="true" optimistic-lock="true">
<component name="Email" insert="true" update="true" optimistic-lock="true">
<property name="Value" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="EmailValue" />
</property>
</component>
<property name="Street" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="WorkStreet" />
</property>
<property name="Nr" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="WorkNr" />
</property>
<many-to-one cascade="save-update" class="FluentNhibExample.Model.City, FluentNhibExample, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="City">
<column name="City_id" />
</many-to-one>
</component>
</component>
<property name="Name" type="System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Name" />
</property>
</class>
</hibernate-mapping>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment