Uczę się Ninject jako mojego pierwszego kontenera DI, myślę, że wszystkie kontenery będą miały podobny pomysł, więc oto moje pytanie.
Mam klasę UserRepository, która ma funkcję:
//constructor
public UserRepository(Kernel kernel) { _kernel = kernel; }
public UserDataModel CreateNewUser(string title, string firstname,
string lastname, int age ...)
{
//I have the kernel registered everything properly
//Getting a new user object from the kernel
var user = _kernel.Get<UserDataModel>();
user.Title = title;
user.Firstname = firstname;
user.Lastname = lastname;
...
return user;
}
Czy to właściwy sposób używania jądra? Czytałem z innych postów, że ujawnianie i przekazywanie jądra to zły, zły pomysł. Jeśli tak, jaki jest właściwy sposób, aby to zrobić?
EDYTOWAĆ
część mojego kodu demonstruje użycie obiektu tempUser
public int UpdateSaUser(Guid msuserid, UserProfile saprofile,
string accesstoken = "")
{
using (var db = new SaModelContainer())
{
var row1 = MakeNewRow(msuserid, saprofile, accesstoken);
var row2 = db.SaUser.SingleOrDefault(
r => r.MsUserId.Equals(msuserid));
if (row2 == null) { //add new
db.sauth_User.Add(row1);
} else { //modify
var entry = db.Entry(row2);
entry.OriginalValues.SetValues(row2);
entry.CurrentValues.SetValues(row1);
}
return db.SaveChanges();
}
}
private SaUser MakeNewRow(Guid msuserid, UserProfile saprofile,
string accesstoken = "")
{
var row = new SaUser {
MsUserId = msuserid,
Provider = (int)saprofile.Provider,
ProfileId = saprofile.ID,
Email = saprofile.Email,
AccessToken = accesstoken,
FirstName = saprofile.FirstName,
LastName = saprofile.LastName,
FullName = saprofile.FullName,
UserName = saprofile.Username,
DisplayName = saprofile.DisplayName,
Country = saprofile.Country,
DoB = StringParser.ParseDate(saprofile.DateOfBirth),
Gender = (int)saprofile.GenderType,
Language = saprofile.Language,
ProfileURL = saprofile.ProfileURL,
ProfilePictureURL = saprofile.ProfilePictureURL
};
return row;
}
Odpowiedzi:
2 dla odpowiedzi № 1Powinieneś wstrzyknąć swoje zależności, znane również jako wstrzyknięcie zależności. Zamiast wstrzykiwać jądro.
Przykład (to jest znane jako iniekcja konstruktora):
private UserDataModel _userDataModel;
[Inject]
public UserRepository(UserDataModel userDataModel)
{
_userDataModel = userDataModel;
}
//Any other method can now use _userDataModel
Prawdopodobnie powinieneś stworzyć interfejs swojego UserDataModel
dzięki czemu można go kpić lub odgiąć w testach jednostkowych. I powiąż ten interfejs z poprawną implementacją za pomocą ninject, patrz przykład. Wtedy atrybuty nie są potrzebne.
Lista UserDataModels nie jest zależnością. TO ZNACZY.Masz jakąś bazę danych lub inną zależność (usługa WCF, usługa sieci Web, Entity Framework, nHibernate, ...), z której pochodzą użytkownicy, jest to Twoja zależność.
public UserRepository(EntityFrameworkContext context)
{
//The entity framework context is the dependency that you don"t want to test.
_context = context;
}
public List GetAllUsers()
{
return _context.Users.ToList();
}