【发布时间】:2011-10-08 13:52:21
【问题描述】:
在我的应用程序中,我有以下代码:
public class Couple
{
public List<Role> Roles { get; set; };
public Couple()
{
//How can I add the role in the constructor?
//Is it correct to refer to the repository?
//If so, this will impact on my EF Code First
}
}
public class Role
{
public string Name { get; set; };
}
public class DataContext : DbContext
{
DbSet<Couple> Couples { get; set; }
DbSet<Role> Roles { get; set; }
}
public interface IRepository
{
void AddCouple(Couple couple);
}
public class Repository : IRepository
{
DataContext db = new DataContext();
public void AddCouple(Couple couple)
{
var role = (from r in db.Roles
where r.Name == "Couple"
select r).SingleOrDefault();
couple.Roles.Add(role); //This is a correct place?
db.Couples.Add(entity);
db.SaveChanges();
}
}
我还有一个带有假存储库的测试项目:
public class FakeRepository : IRepository
{
List<Couple> Couples = new List<Couple>();
ListRole> Roles = new List<Role>();
public void AddCouple(Couple couple)
{
var role = (from r in Roles
where r.Name == "Couple"
select r).SingleOrDefault();
couple.Roles.Add(role);
Couples.Add(entity);
}
}
在AddCouple 方法中的Repository 类中,我添加了角色。但我认为这不是正确的地方。
我的问题是在哪里放置逻辑,查看role 存储库并填充couple 的角色列表。
并且能够继续构建测试。
【问题讨论】:
标签: asp.net-mvc-3 unit-testing architecture repository-pattern