【发布时间】:2016-11-29 15:05:03
【问题描述】:
我正在创建一个将调用服务层的 Web API,并且我正在尝试学习依赖注入(我希望使用 ninject),但我不确定如何创建对服务层的依赖。
这就是 web api 所调用的。
这里的问题是,当调用 IPersonService 时,该人将定义性别、名称、角色和种族。我正在使用构造函数注入,但不确定是应该调用 GenderService 还是应该调用业务层(在本例中由 Core 定义)。
我应该像上图还是下图那样调用服务
这就是我的个人服务的样子
namespace Service.Services
{
public class PersonService : IPersonService
{
private IPersonCore personCore = null;
private INameService nameService = null;
private IRoleService roleService = null;
private IGenderService genderService = null;
private IEthnicityService ethnicityService = null;
private IPrefixService prefixService = null;
private Person currUser;
public PersonService(IPersonCore _personcore, INameService _namecore, IRoleService _roleservice, IGenderService _genderservice, IEthnicityService _ethnicityservice, IPrefixService _prefixservice )
{
this.personCore = _personcore;
this.nameService = _namecore;
this.roleService = _roleservice;
this.genderService = _genderservice;
this.ethnicityService = _ethnicityservice;
this.prefixService = _prefixservice;
}
public IEnumerable<Person> GetAllPerson()
{
if (isAuthorized())
{
return this.personCore.GetPersons();
}
return null;
}
public Person GetPersonByID(int id)
{
if (isAuthorized())
{
return this.personCore.GetPersonByID(id);
}
return null;
}
public Person GetPersonByEmail(string email)
{
if (isAuthorized())
{
return this.personCore.GetPersonByEmail(email);
}
return null;
}
public IEnumerable<Person> GetPersonByName(string first, string last, string middle)
{
if(isAuthorized())
{
Name newname = this.nameService.CreateName(first, last, middle);
return this.personCore.GetPersonByName(newname);
}
return null;
}
public IEnumerable<Person> GetPersonWithRoles(IEnumerable<Roles> r)
{
}
public IEnumerable<Person> GetPersonWithDOB(DateTime d)
{
if (isAuthorized())
{
return this.personCore.GetPersonWithDOB(d);
}
return null;
}
public Person SetPersonRole(int id, Roles r)
{
}
public Person SetGender(int id, Gender g)
{
}
public Person SetEthnicity(int id, Ethnicity e)
{
}
public Person SetPrefix(int id, Prefix p)
{
}
public Person CreatePerson(Person p)
{
if (isAuthorized())
{
return personCore.AddPerson(p);
}
return null;
}
public Person UpdatePerson(Person p)
{
if (isAuthorized())
{
return personCore.UpdatePerson(p);
}
return null;
}
public Person ActivatePerson(int id)
{
if (isAuthorized())
{
return personCore.ActivatePerson(id);
}
return null;
}
public Person DeactivatePerson(int id)
{
if (isAuthorized())
{
return personCore.DeactivatePerson(id);
}
return null;
}
public bool DeletePerson(int id)
{
if (isAuthorized())
{
return personCore.DeletePerson(id);
}
return false;
}
protected bool isAuthorized()
{
//Probably move to common
return true;
}
}
}
当我从 Web API 调用它时,我的问题是,它听起来像是很多依赖来查找有关某个人的信息。
【问题讨论】:
标签: c# ninject ioc-container