I am learning the repository pattern and entity framework. I have a question regarding how to map my classes with EF 6 Code First (Existing Db). I know there a lot of questions about this (and I've readed maany of them) but still it's hard for me to understand something, which appears to be "logic" but it's getting me crazy (maybe I have readed too many different opinions...)
Problem (Example)
A table Processes
which stores different processes like offer, receipt, order etc.. One of the columns of the table is AddDate
which represents when this process was added to the DB.
Now I want to write a library to access this DB to use it in different apps, since it's the core Db in the company.
In the library I want to expose for example a ProcessRepository
with a method GetByAddDateRange
which returns the processes added after, before or in a given time range.
Question
So, now EF with CodeFirst to an existing DB first generates for me a class for every table with a property for every field. So I have a class Process
(from now ProcessEntity)
with a property AddDate
.
Now in my "domain model", the classes that I want to use in my businnes logic etc, I want to have a class Process
(from now ProcessDomain)
but this should not have an AddDate
property, since it's not important for the "object" or is part of its "properties".
So now how should I do then if I want to get ProcessDomains
from my ProcessRepository
using this AddDate as condition?
-
If I use directly
ProcessEntity
as "domain model" (class generated by EF) and if I understand it right, I have to have a property calledAddDate
since I will need it in my repo when doing sth likeReturn _dbContext.Set(Of Process).Where(Func(s)s.AddDate>addDate)
or not? -
I could (like I've read) have a class
ProcessEntity
andProcessDomain
and map manually between them in my repository. If I understand it right the repo should return the expected type for the layer above, in this case should return my domainModel class.
Do I understand it?
So for me the first option appears just in close to all scenarios useless, since you have to have specific properties in your classes. so it doesn't matter how I try to look at the problem I always see the need to map manually when using an existing db.
Maybe I am missing sth or understanding sth wrong, so please let me know.
Aucun commentaire:
Enregistrer un commentaire