asp.net webapi 和业务逻辑问题
Issue with asp.net webapi and business logic
正在使用 asp.netwebapi visual studio 编写服务 13 我不知道如何将模型与业务层集成或通信。因为如果我使用 3 层架构上下文将充当模型,那没有区别所以请任何人建议我模型和业务层之间的通信
您需要在 WebAPI 中创建 Models
以将 BLL 与 WebAPI 分开。例如,如果您的 BLL 中有一个 Person
class,您可以在您的 WebAPI 中有一个 PersonModel
。大致如下所示。
[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);
}
您可以使用 AutoMapper 自动填充模型,而无需编写模型 <-> BLL class 代码。
正在使用 asp.netwebapi visual studio 编写服务 13 我不知道如何将模型与业务层集成或通信。因为如果我使用 3 层架构上下文将充当模型,那没有区别所以请任何人建议我模型和业务层之间的通信
您需要在 WebAPI 中创建 Models
以将 BLL 与 WebAPI 分开。例如,如果您的 BLL 中有一个 Person
class,您可以在您的 WebAPI 中有一个 PersonModel
。大致如下所示。
[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);
}
您可以使用 AutoMapper 自动填充模型,而无需编写模型 <-> BLL class 代码。