Am writing a service using asp.netwebapi visual studio 13 i am not getting how to integrate or communicate models with business layer. because if i use 3 tier architecture context will act as models no difference in that so please any body suggest me the communication between the models and business layer
1 Answer
You need to create Models in your WebAPI to separate your BLL from WebAPI. For example, if you have a Person class in your BLL you can have a PersonModel in your WebAPI. It would work like below roughly.
[HttpGet]
Public HttpResponseMessage Get(id)
{
// Validation for id
var person = _dbContext.GetPersonById(id);
// Now populate the Model
var personModel=new PersonModel();
// You can use automapper to replace the following part
personModel.PersonId=person.PersonId;
personModel.Firstname= person.Firstname;
// ....
}
[HttpPost]
Public HttpResponseMessage Post(PersonModel personModel)
{
// Validation here
// ...
var person = new Person();
// You can use automapper to replace the following part
person.PersonId= personModel.PersonId;
person.Firstname=personModel.Firstname;
_dbContext.Save(person);
}
You can use AutoMapper to automatically populate the models without having to write Model <-> BLL class code.
1 Comment
Naveen Kumar
Thanks for suggestion i will get back to u again about this issue