Image

Mapping Our Domain With FluentMap

Modified: Friday, 12 June 2009 03:13 PM by arobson - Categorized as: Getting Started


Edit

Quick Note On Mapping

Originally, Nvigorate's options for defining mapping were XML or attributes. Because both of these were painful, clunky and easy to mess up, I started looking around for alternatives and learned a little about the Fluent NHibernate project. Those guys are brilliant: the fluent pattern is a definite win in this area.


Important
As a result of this development, XML map support is not going to be actively developed which means some of the things you can do in the Fluent Mapping you cannot represent in XML. While I will probably leave what's there in place, I don't plan to document or "support" it since I can't imagine why you'd rather have a ton of XML over testable, readable, magic-string-free code.

Edit

Address Map

For sanity's sake, this will be the only map where I demonstrate what mapping looks like without the use of the schema template.

Edit

Without


public class AddressMap : FluentMap<Address>
{
public AddressMap()
{
RepositoryName = "demo";
var table = MapTable("HomeAddress");

// note that where a column and property have the same name
// we don't have to use ToColumn

table.Assign(a => a.Id).ToColumn("AddressId").AsKey().ReadOnly();
table.Assign(a => a.Street1).ToColumn("AddressLine1");
table.Assign(a => a.Street2).ToColumn("AddressLine2");
table.Assign(a => a.City);
table.Assign(a => a.State).ToColumn("StateAbbreviation");
table.Assign(a => a.Zip);

MapChildren(a => a.Residents)
.ForeignKey(a => a.Id, c => c.MainAddress.Id);
}
}

Edit

With


public class AddressMap : FluentMap<Address>
{
public AddressMap()
{
// look ma, no strings...
RepositoryName = "demo"; //ok, well, maybe just one string
var table = MapTable<HomeAddressSchema>();

table.Assign(a => a.Id, t => t.AddressId).AsKey().ReadOnly();
table.Assign(a => a.Street1, t => t.AddressLine1);
table.Assign(a => a.Street2, t => t.AddressLine2);
table.Assign(a => a.City);
table.Assign(a => a.State);
table.Assign(a => a.Zip);

MapChildren(a => a.Residents)
.ForeignKey(a => a.Id, c => c.MainAddress.Id);
}
}

Edit

Appointment Map


public class AppointmentMap : FluentMap<Appointment>
{
public AppointmentMap()
{
RepositoryName = "demo";
var table = MapTable<AppointmentSchema>();

table.Assign(a => a.Id, t => t.AppointmentId).AsKey().ReadOnly();
table.Assign(a => a.Time, t => t.ScheduledFor);

MapParent(a => a.Bill)
.ForeignKey(a => a.Id, b => b.Appointment.Id);
}
}


Edit

Bill Map


public class BillMap : FluentMap<Bill>
{
public BillMap()
{
RepositoryName = "demo";
var table = MapTable<BillSchema>();

table.Assign(b => b.Id, t => t.BillId).AsKey().ReadOnly();

MapParent(b => b.Appointment)
.ForeignKey(b => b.Appointment.Id, a => a.Id);

MapChildren(b => b.LineItems)
.ForeignKey(b => b.Id, l => l.Bill.Id);
}
}


Edit

Car Map




Edit

Customer Map


public class CustomerMap : FluentMap<Customer>
{
public CustomerMap()
{
RepositoryName = "demo";
var table = MapTable<CustomerSchema>();

table.Assign(c => c.Id, t => t.CustomerId).AsKey().ReadOnly();
table.Assign(c => c.FirstName);
table.Assign(c => c.LastName);

var contacts = MapTable<CustomerContactInformationSchema>();
contacts.Assign(c => c.Id, t => t.CustomerId).AsKey();
contacts.Assign(c => c.PrimaryEmail, t => t.PrimaryEmail).Nullable();
contacts.Assign(c => c.SecondaryEmail, t => t.SecondaryEmail).Nullable();
contacts.Assign(c => c.PrimaryPhone.Country, t => t.PrimaryCountryCode).ConstructorArgument(0).Nullable();
contacts.Assign(c => c.PrimaryPhone.Area, t => t.PrimaryCountryCode).ConstructorArgument(1).Nullable();
contacts.Assign(c => c.PrimaryPhone.Exchange, t => t.PrimaryAreaCode).ConstructorArgument(2).Nullable();
contacts.Assign(c => c.PrimaryPhone.Number, t => t.PrimaryExchange).ConstructorArgument(3).Nullable();
contacts.Assign(c => c.PrimaryPhone.Extension, t => t.PrimaryExtension).ConstructorArgument(4).Nullable();
contacts.Assign(c => c.SecondaryPhone.Country, t => t.SecondaryCountryCode).ConstructorArgument(0).Nullable();
contacts.Assign(c => c.SecondaryPhone.Area, t => t.SecondaryCountryCode).ConstructorArgument(1).Nullable();
contacts.Assign(c => c.SecondaryPhone.Exchange, t => t.SecondaryAreaCode).ConstructorArgument(2).Nullable();
contacts.Assign(c => c.SecondaryPhone.Number, t => t.SecondaryExchange).ConstructorArgument(3).Nullable();
contacts.Assign(c => c.SecondaryPhone.Extension, t => t.SecondaryExtension).ConstructorArgument(4).Nullable();

contacts.JoinTo(table, JoinType.Left,
contacts.Column(c => c.CustomerId).EqualTo(table.Column(t => t.CustomerId)));

MapParent(c => c.MainAddress)
.ForeignKey(c => c.Id, a => a.Id);

MapToMany<IVehicle, DriversSchema>(c => c.Vehicles)
.SelfToContainer(c => c.Id, d => d.CustomerId)
.RelativeToContainer(v => v.Id, d => d.VehicleId);
}
}


Edit

IVehicle Map




Edit

LineItem Map




Edit

Product Map




Edit

ProductLineItem Map




Edit

Service Map




Edit

ServiceLineItem Map




Edit

Truck Map




Edit

Vehicle Map


ScrewTurn Wiki version 2.0.36. Some of the icons created by FamFamFam.



Sharp Learning Curve Ahead

↑ Grab this Headline Animator