I have a root Web API project and some other side class library projects where other apicontrollers are defined ( each project is a seperated webservice for external communications ).
Each side project has its own controller which has a BuildModel
function where we do the mapping between the external webservice models and the models used internal.
Example of the BuildModel
:
// Get AX Customer
OrderRequest order = ....
var customer = order.Customer;
var axCustomer =
_customerService.GetCustomer(
_mapper.Map<Customer, AxCustComSetup>(customer)
);
// Vendor
var vendor = new AxVendor() { AccountNum = "11111", Name = "VENDOR" };
// Get AX Articles
var articles = _mapper.Map<IEnumerable<OrderLine>, IEnumerable<AxComArticle>>(order.Order.OrderLines);
articles = _articleService.GetArticles(
_mapper.Map(vendor, articles).ToList()
);
// Create EDI Journal and return true if succeeded
var ediJournal = _mapper.Map<AxCustomer, AxComEdiJournal>(axCustomer);
_mapper.Map(vendor, ediJournal);
_mapper.Map(order.Order, ediJournal);
var ediJournalLines = _mapper.Map<IEnumerable<AxComArticle>,
IEnumerable<AxComEdiJournalLine>>(articles);
ediJournal.Lines = ediJournalLines.ToList();
ediJournal.CreateDate = DateTime.Now;
ediJournal.OrderId = string.Empty;
ediJournal.Remark = string.Empty;
return ediJournal;
Basically, we need to make a AxComEdiJournal
object which is then used in operations with our database.
For each side project we almost have the same identical method except for the external model types which is used to do the mapping.
Can we make this more dynamic or generic because now I copy paste this into other projects and change the model types.
For the mapping I use Automapper
.
Aucun commentaire:
Enregistrer un commentaire