I am using WebAPI to expose MSCRM data to client applications as follows:
1) CRM entity mapped to domain model entity using custom mapping logic (CRM entities may not map 1-1 to domain model)
2) Domain model entity being mapped to DTOs using AutoMapper in the WebAPI layer (Domain model objects map 1-1 to DTOs)
3) DTO is sent over the wire when a GET operation is invoked in WebAPI
When a DTO is PUTted/POSTed the reverse process happens
1) A DTO is received as an argument to a PUT/POST method in WebAPI
2) DTO is mapped to domain model object using AutoMapper in the POST/PUT operation
3) The domain model object is mapped to a CRM entity and either inserted/updated to CRM
If the domain model has properties from multiple CRM entities, such as the foreign key of related entities or scalar properties from related entities, what is the recommended way to handle Updates/Inserts in this case? Does it make sense to ignore the foreign key/scalar properties of related entities specified on the DTO and by convention require that a POST/PUT should be made to the related entity directly?
Example:
CRM entity: Incident Attributes: title,product_id
CRM entity: Product Attributes: product_name,product_id,product_legacyid,description
DataModel entity: SalesIncident Attributes: title,productname,productid,productlegacyid
DTO entity: SalesIncidentDTO Attributes: title,productname,productid,productlegacyid
If a SalesIncidentDTO is POSTed/PUTed, AutoMapper maps it just fine to theSalesIncident domain model entity. What is the recommended way to handle inserting/updating the data back to CRM since the fields span across CRM entities? Does it make sense to span the INSERT/UPDATE across multiple CRM entities at all?
-Abhijeet