-
Notifications
You must be signed in to change notification settings - Fork 0
Creating Repository Classes
StrutTower edited this page Jul 4, 2024
·
15 revisions
There are 3 ways to initialize a repository class.
- Manually writing out the database mapping in the repository Manual Mapping
- Using the property names and data attributes of the domain object Property Names
- Using an EntityMap class to define the mapping Entity Maps
The method that is used is based on the values passed to the base class.
For the example I'll use a basic person object:
public class Person {
public int ID { get; set; }
public string DisplayName { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime DateOfBirth { get; set; }
public bool IsActive { get; set; }
}The MapBuilder class can be used to help create the maps with strongly-typed property names
public class PersonRepository : DbRepository<Person> {
public PersonRepository(UnitOfWork uow) : base(uow.DbAdapter, "databaseTableName", GetMaps()) { }
private static List<IMap>() {
return new MapBuilder<Person>()
.AutonumberMap(x => x.ID)
.Map(x => x.DisplayName),
.Map(x => x.CreatedOn),
.Map(x => x.DateOfBirth),
.Map(x => x.IsActive);
// If the C# property doesn't match the database column name then the second parameter can be used
// .Map(x => x.DisplayName, "DatabaseColumnName")
}
}Maps can be created directly but the MapBuilder is the suggested method since the property names are strongly-typed.
public class PersonRepository : DbRepository<Person> {
public PersonRepository(UnitOfWork uow) : base(uow.DbAdapter, "databaseTableName", GetMaps()) { }
private static ICollection<IMap>() {
return new[] {
new AutonumberMap("ID"),
new Map("DisplayName"),
new Map("CreatedOn"),
new Map("DateOfBirth"),
new Map("IsActive")
};
// If the C# property doesn't match the database column name then the second parameter can be used
// new Map("PropertyName", "DatabaseColumnName")
}
}This method using the names of the property on the domain object and attributes to define the database mapping. Check Mapping Attributes to see all available attributes.
public class PersonRepository : DbRepository<Person> {
public PersonRepository(UnitOfWork uow) : base(uow.DbAdapter) { }
}//[TableName("DatabaseTableName")] // Use the TableNameAttribute if the table name does not match the domain object name
public class Person {
[Autonumber]
public int ID { get; set; }
//[ColumnMap("DatabaseColumnName")] // Use the ColumnMap attribute if the column name doesn't match the property name
public string DisplayName { get; set; }
public DateTime CreatedOn { get; set; }
public DateTime DateOfBirth { get; set; }
public bool IsActive { get; set; }
}Entity maps allow you to configure the data mappings in a separate file if you want to keep you domain and repository classes clear of the mapping data.
public class PersonRepository : DbRepository<Person> {
public PersonRepository(UnitOfWork uow) : base(uow.DbAdapter, new PersonEntityMap()) { }
}public class PersonEntityMap : EntityMap<Person> {
public PersonEntityMap() : base("Person") { }
public override IEnumerable<IMap> GetMaps() {
return new[] {
MapProperty(x => x.ID).AsAutonumber().ToSameName(),
MapProperty(x => x.DisplayName).ToSameName(),
MapProperty(x => x.CreatedOn).ToSameName(),
MapProperty(x => x.DateOfBirth).ToSameName(),
MapProperty(x => x.IsActive).ToSameName()
};
}
}