找到加载具有不同图形的对象的正确模式
我试图找出处理加载具有不同图形(相关实体)的对象的最佳方法,具体取决于它们所使用的上下文。
例如,这是我的域对象的示例:
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
)?
我说得有道理吗? 我离基地太远了吗? 请帮忙。
I'm trying to figure out the best way to handle loading objects with different graphs (related entities) depending on the context their being used.
For example Here's a sample of my domain objects:
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; }
}
What I'm trying to understand is how to load this information differently depending on how I'm using it.
For example, on the front page I have a list of all puzzles. At this point I don't really care about the solutions for the puzzle or for the steps in those solutions (which can get pretty hefty). All I want are the puzzles. I would load them from my controller like this:
public ActionResult Index(/* parameters */)
{
...
var puzzles = _puzzleService.GetPuzzles();
return View(puzzles);
}
Later on for the puzzle view I now care about only the solutions for the current user. I don't want to load the entire graph with all of the solutions and all of the steps.
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.
}
Inside my IPuzzleService, my methods look like this:
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();
}
}
Lazy loading doesn't really work in the real world, because my session is being disposed right after each unit of work. My controllers don't have any concept of the repository and therefore do not manage session state and can't hold on to it until the view is rendered.
I'm trying to figure out what the right pattern to use here is. Do I have different overloads on my service like GetPuzzleWithSolutionsAndVotes
or more view specific like GetPuzzlesForDisplayView
and GetPuzzlesForListView
?
Am I making sense? Am I way off base? Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个类似的情况,我无法使用延迟加载。
如果您只需要一两个案例,那么按照您的建议,最简单的方法是创建单独的 GetPuzleWithXYZ() 方法。
您还可以创建一个具有流畅界面的小型查询对象。
就像...
然后您的 Repository Get() 方法可以实例化表达式生成器并将其传递给调用者
,典型的调用看起来像...
它需要一些工作,但您可以看到基本思想。
I had a similar case where I could not use Lazy loading.
If you only need a one or two cases, then the easiest thing as you suggest, create separate GetPuzleWithXYZ() methods.
You could also create a small query object with a fluent interface.
Something like...
then your Repository Get() method can instantiate the expression builder and pass it to the caller
and typical calls would look like...
it needs a little work, but you can see the basic idea.
我认为您的服务不应该了解视图。 您的要求可能会发生变化,因此不要将他们的名称与视图名称联系起来。
当您调用 GetPuzzles() 时,您应该只加载根谜题,而 GetPuzzleById(int id) 可以立即加载关联。
例如在标准 API 中:
.SetFetchMode("Solutions", NHibernate.FetchMode.Join)
我不明白为什么你不能延迟加载。 如果您使用nHibernate,当您访问该属性时,代理将返回到数据库。 您的控制器应该获得您需要的所有数据。 您不应该将未加载的代理传递给您的视图,然后您的视图必须处理无法加载数据的情况。
所以你应该让控制器加载你需要的特定数据,只为用户加载,我会改变接口,这样:
GetPuzzle(puzzleId,user)
在这个方法中,你可以直接加载一个用户的数据。
I don't think your service should have any knowledge of the Views. Your requirements might change so don't tie their names to view names.
When you call GetPuzzles() you should only load root puzzle, and GetPuzzleById(int id) can eager load the associations.
For example in criteria API:
.SetFetchMode("Solutions", NHibernate.FetchMode.Join)
I don't understand why you cant lazy load. If your using nHibernate, the proxy will go back to the database for you when you access the property. Your controller should get all the data you need. You should not pass a non-loaded proxy to your view then your view has to handle the case where it can't load the data.
So you should have controller load the specific pieces of data you need, to load only for a user, I would change the interface such that:
GetPuzzle(puzzleId,user)
In this method you can just eager load the data for the one user.