-
Notifications
You must be signed in to change notification settings - Fork 310
Fluent mappings including conventional
Adam Schroder edited this page Jun 7, 2013
·
8 revisions
To a mapping for a class you can inherit from Map<T> where T is the class you are mapping for.
public class UserMapping : Map<User>
{
public UserMapping()
{
PrimaryKey(x => x.UserId);
TableName("Users");
Columns(x =>
{
x.Column(y => y.Name).Ignore();
x.Column(y => y.Age).WithName("a_ge");
});
}
}Mappings can also inherit from Mappings and specify all mappings in once class using the For<> method.
public class OurMappings : Mappings
{
public OurMappings()
{
For<User>().Columns( ....
}
}You only want to create the mappings once and we do this using a Database Factory.
public void Application_Start()
{
MyFactory.Setup();
}public static class MyFactory
{
public static DatabaseFactory DbFactory { get; set; }
public static void Setup()
{
var fluentConfig = FluentMappingConfiguration.Configure(new OurMappings());
//or individual mappings
//var fluentConfig = FluentMappingConfiguration.Configure(new UserMapping(), ....);
DbFactory = DatabaseFactory.Config(x =>
{
x.UsingDatabase(() => new Database(new SqlConnection()));
x.WithFluentConfig(fluentConfig);
x.WithMapper(new Mapper());
});
}
}then you can use it like so in your code.
var database = MyFactory.DbFactory.GetDatabase();if you are using a container then you could have something like
For<IDatabase>().Use(() => MyFactory.DbFactory.GetDatabase());