Design pattern for mapping database in Data Access Layer [C#]

1.3k Views Asked by At

I work on my Data Access Layer where I use data mapper pattern. My actual structure of code is for example:

public class Person {
    public int Age
    public string FirstName
    public string LastName
    public List<Address> Addresses
    ...
}

public class PersonMapper {
    public Person GetPersonById(int id)
    public List<Person> GetAll()
    public bool UpdatePerson(Person person)
    ...
}

I have so many classes which are corresponding for database table with same name.

My questions are:

  1. Is my approach right? When I mapped all tables, I will use it in the domain layer.

  2. In Mapper classes I use methods which are working only with tables which are same name as these classes. (Person class -> Persons db table, Order class -> Orders db table, etc.) But what is the best way to map advanced selects from database, which will be contains joins to more tables. For example I want select Person with all his Orders. Should I create domain model for Person which will be contains property List<Orders>and use PersonMapper and next OrderMapper?

1

There are 1 best solutions below

0
ddarellis On BEST ANSWER

See the DAO Pattern.

In general: For every table in your db you have an entity and a Dao class for the entity also a database class which communicates with the database.

For example Person, PersonDao, Database.

The person is the entity. PersonDao uses Database class to query database only for person table. Database is a CRUD class for the database.