Asked by:
Mapping Early Bound entities to POCO entities

Question
-
Hi all,
Whenever you guys need to map early bound entities to POCO entities, you usually use a library like AutoMapper or anything like that? Anyone has a best practice advise regarding to this? Thank you
If I answered your question please mark it as answer Cheers, Henrique Silvestre Souza
Monday, July 10, 2017 9:47 AM
All replies
-
I generally avoid mapping and define whatever additional members I require in a partial class that extends the proxy class. (and for the record I use XrmToolkit's proxy classes).
For Example:
Generated proxy class:
public partial class Account { public guid Id {get; set;} public string Name {get; set;} public Money Revenue {get; set;} }
My extension:
public partial class Account { public string MyProperty {get; set;} public void PrefixName() { Name = $"MyPrefix_{Name}"; } }
I'll generally call the file containing my extensions Account+.cs. But if I'm doing multiple extensions to a class, I might separate them into separate files like Account+Naming.cs and Account+CountRelatedRecords.cs.
Aron
- Edited by Aron F Tuesday, July 11, 2017 9:38 PM
Tuesday, July 11, 2017 9:37 PM -
Thanks for the answer Aron.
I'm using an hexagonal architecture at a external project that will communicate with CRM, so basically I have my domain layer which have their own POCO classes to represent the business, the domain will consumes my wcf service (only one who access CRM, and I'm using early bound entities from CRM) finally, my UI layer only knows my domain layer, so, my UI layer also uses the domain entities (POCO).
So, if I follow your advice, the Account class will be both my data access class (result from CRM queries) and my POCO (business class) right?
The key is, my domain must be decoupling from anything else. If I use partial classes between domain and wcf service I'll break this principle.
Thoughts?
Thanks
If I answered your question please mark it as answer Cheers, Henrique Silvestre Souza
Wednesday, July 12, 2017 10:27 AM -
OK, I understand your desire to maintain the separation of concerns. What I might do is create a new constructor on the POCO that takes an instance of the proxy class and do the mapping in there.
For example:
public class DomainAccount { public Guid Id {get; set;} public string Name {get; set;} public decimal Revenue {get; set;} public DomainAccount(Account acct) { Id = acct.Id; Name = acct.Name; Revenue = acct.Revenue.Value } }
Wednesday, July 12, 2017 11:18 AM -
It's a good approach to the mapping, but to make it work my domain layer will need to reference the SDK layer. (the Account type that you are passing via constructor parameters has inheritance with sdk classes) and will broke the design of the application.
If I answered your question please mark it as answer Cheers, Henrique Silvestre Souza
Wednesday, July 12, 2017 4:11 PM -
Perhaps encapsulating the Account proxy class instance will do the trick. This way you wrap the SDK layer class in the domain layer class.
public class DomainAccount { public Account CrmAccount {get; set;} public DomainAccount(Account acct) { CrmAccount = acct; } }
- Edited by Aron F Wednesday, July 12, 2017 4:26 PM
Wednesday, July 12, 2017 4:24 PM