简体   繁体   中英

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

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM