我正在創建一個可以調用服務層的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是我的問題,它的聲音就像尋找某人某事的依賴性。
謝謝,所以從用戶角度來看,Personservice應該返回基本的id,密碼,電子郵件但是角色,性別只應該給genderService,兩個服務不應該互相對話? – Jseb
@Jseb:這不是我所宣傳的。我正在推廣的是一個模型,其中每個查詢(例如'GetAllPerson'和'GetPersonByEmail')都包含在它自己的類中。這樣的類可以依賴於它所需要的東西,但在大多數情況下只會使用'DbContext'來查詢數據庫。 – Steven