我在这个主题上阅读了很多内容,但我仍然不明白如何在自己的上下文中使用不同的服务。我知道,如果我使用类似"工作单位",这种方法将解决我的问题,但我正在寻找另一种解决方案。任何帮助将不胜感激!
public class SportCategoryService : ISportCategoryService
{
private IDeletableEntityRepository<SportCategory> sportCategoriesDb;
private IDeletableEntityRepository<User> usersDb;
public SportCategoryService(IDeletableEntityRepository<SportCategory> sportCategoriesDb, IDeletableEntityRepository<User> usersDb)
{
this.sportCategoriesDb = sportCategoriesDb;
this.usersDb = usersDb;
}
public void AddCategoriesForUser(string[] categories, string userId)
{
var user = usersDb.All().FirstOrDefault(x => x.Id == userId);
foreach (var name in categories)
{
var currentCategory = this.sportCategoriesDb.All().FirstOrDefault(x => x.Name == name);
if (currentCategory != null)
{
currentCategory.Users.Add(user);
}
else
{
sportCategoriesDb.Add(new SportCategory() { Name = name, Users = new List<User>() { user } });
}
}
sportCategoriesDb.SaveChanges();
}
}