Naveen Kumar
Naveen Kumar

Reputation: 141

Issue with asp.net webapi and business logic

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

Upvotes: 0

Views: 496

Answers (1)

su8898
su8898

Reputation: 1713

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.

Upvotes: 1

Related Questions