Skip to content
This repository was archived by the owner on Nov 24, 2026. It is now read-only.

Creating Repository Classes

StrutTower edited this page Nov 18, 2023 · 15 revisions

There are 3 ways to initialize a repository class.

  • Manually writing out the database mapping in the repository Some text
  • Using the property names and data attributes of the domain object Some text
  • Using an EntityMap class to define the mapping Some text

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; }
}

Manual Mapping

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")
    }
}

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")
    }
}

Property Names

public class PersonRepository : DbRepository<Person> {
    public PersonRepository(UnitOfWork uow) : base(uow.DbAdapter) { }
}
//[Table("DatabaseTableName")] // Use the Table attribute 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 or Column 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

Clone this wiki locally