在我的一个类中,我想添加一个方法'GetUsefulInfo‘。
为了实现这个方法,我需要对数据模型中的其他实体运行查询。
问题是,我应该如何从这个方法中获得数据上下文?我需要将它作为参数传递给该方法吗?感觉应该有一种方法来实现这一点,而不必要求这个类的消费者这样做?
谢谢,-克里斯
发布于 2012-05-08 09:04:33
这看起来很适合UserRepository。
通常,存储库使用某种依赖项注入来保存对上下文的引用,或者只是对DataContext的私有引用。
在那里,您可以查询任何存储库以收集所需的信息。
示例代码:
public class UserRepository : IUserRepository
{
private readonly EntityFrameworkDataContext database =
new EntityFrameworkDataContext();
public User GetUserFullInfo()
{
try
{
// Your DataContext queries to return the User and all his info...
}
catch
{
// Whaterver...
}
}
}https://stackoverflow.com/questions/10491094
复制相似问题