c# - Repository with Multiple Entities Creating a DTO -
doing c# code review here, , noticed developer doing join 5 additional tables, , inline iqueryable projection dto. tables being joined in repository user, appointment, communicationpreference, userproduct, , product. didn't design system, , i'm simplifying somewhat, let's go now. using entity framework , web api.
this little long apologize, curious hear others' thoughts on matter.
so these tables relate in each user has list of upcoming service appointments service products. userproduct table shows relationship between product , user, , product table shows kinds of products. communicationpreference deals how want contacted things when service due, if coming service product, how want contacted (phone email text etc).
so developer joins these tables in repository itself, , creates dto called "userserviceoverviewdto" returned controller , front end , has information display in grid customer representative (who user is, upcoming appointments, products, etc. wrote above).
i understand why did - instead of making 5 calls 5 different repositories, did in one, , instead of having convert each 1 dto, did in 1 shot. understand repository shouldn't return dto, either. , ideally repository should return logically aggregated , representative of 1 entity solves problem.
as thinking this, stumbled upon in what type return when querying multiple entities in repository layer? doesn't me enough , wanted ask more advice on do.
looking @ ddd perspective, i'm wondering new "thing" needs introduced. maybe need create new context/repository new thing. if i'm trying common grouping , give name "x" mean want create new database table represent that? not, because have tables store information.
does mean instead want create new class called "x" without corresponding database table? how different dto? in fact, suggests making class, i'm trying make repository more pure instantiating x instead of xdto , casting x dto in service layer anyway (i haven't gained other shifting stuff around , creating anemic entity).
so in summary, then, question is:
if have multiple entities i'm grouping convenience on front end, better to:
- have service layer make 5 distinct database calls, , assemble dto in layer return controller? isn't bad? 5 calls make code seem more "pure"? isn't better joins in 1 shot , need database?
- create new class in c# represents marriage of these 5 tables, , implement mapping between class , corresponding dto. class not persisted said above.
- have repository perform joins create new dto inline projection (seems wrong b/c repositories should return entity not dto)
really looking forward feedback here! thanks!
take @ cqrs, it's exact discussion of topic. basically, should create new object represent aggregate, know doing introduces significant complexity project. thing have created cannot updated/persisted, can queried. updating these other entities associated must done via separate domain models.
Comments
Post a Comment