我想找出处理加载对象与不同的图形(相关实体)取决于他们正在使用的上下文的最佳方式。寻找正确的模式加载不同图形的对象
例如这里是我的域对象的样本:
public class Puzzle
{
public Id{ get; private set; }
public string TopicUrl { get; set; }
public string EndTopic { get; set; }
public IEnumerable<Solution> Solutions { get; set; }
public IEnumerable<Vote> Votes { get; set; }
public int SolutionCount { get; set; }
public User User { get; set; }
}
public class Solution
{
public int Id { get; private set; }
public IEnumerable<Step> Steps { get; set; }
public int UserId { get; set; }
}
public class Step
{
public Id { get; set; }
public string Url { get; set; }
}
public class Vote
{
public id Id { get; set; }
public int UserId { get; set; }
public int VoteType { get; set; }
}
我试图了解是如何不同,这取决于我如何使用它加载此信息。
例如,在头版我有一个所有难题的列表。在这一点上,我并不真正关心这些难题的解决方案,也不关心这些解决方案中的步骤(可能会非常沉重)。我想要的只是谜题。我会加载他们从我的控制器是这样的:
public ActionResult Index(/* parameters */)
{
...
var puzzles = _puzzleService.GetPuzzles();
return View(puzzles);
}
后来的拼图视图我现在只关心当前用户的解决方案。我不想使用所有解决方案和所有步骤加载整个图表。
public ActionResult Display(int puzzleId)
{
var puzzle = _accountService.GetPuzzleById(puzzleId);
//I want to be able to access my solutions, steps, and votes. just for the current user.
}
里面我IPuzzleService,我的方法是这样的:
public IEnumerable<Puzzle> GetPuzzles()
{
using(_repository.OpenSession())
{
_repository.All<Puzzle>().ToList();
}
}
public Puzzle GetPuzzleById(int puzzleId)
{
using(_repository.OpenSession())
{
_repository.All<Puzzle>().Where(x => x.Id == puzzleId).SingleOrDefault();
}
}
延迟加载并没有真正在现实世界中工作,因为我的会话的每个工作单元之后布置。我的控制器没有任何存储库的概念,因此不会管理会话状态,只有在呈现视图之前才能保留它。
我想弄清楚这里使用的正确模式是什么。我对我的服务有不同的过载,如GetPuzzleWithSolutionsAndVotes
或更多查看,具体如GetPuzzlesForDisplayView
和GetPuzzlesForListView
?
我有道理吗?我离开基地吗?请帮忙。
我的头脑有点模糊,现在我希望这对你有意义并提供一些价值 – JoshBerke 2009-06-22 15:26:22