【发布时间】:2011-02-15 01:27:13
【问题描述】:
我的对象模型中有一个多级依赖链:
一个组织具有以下子关系:
Organization
.CompetitionGroups
.CompetitionGroups.Venues
.CompetitionGroups.Competitions
.Divisions.Games
.Divisions.Games.Participants
.Divisions.Games.Participants.GameSegments
.Divisions.SubDivisions...
.Divisions
.Teams
.Teams.Players
.Teams.Participants
.Teams.Participants.GameSegments
.VenueDates
这只是对象模型的一瞥,但它侧重于关系和列表的复杂性。
考虑到完成一个工作单元的要求,我无法真正得到的是考虑我的存储库接口的最佳方式。
例如,要创建一个游戏,您需要一个场地日期和两个参与者。这是否意味着 GamesController 应该需要一个 IGameRepository、一个 IVenueDateRepository 和一个 IParticipant 存储库?是否应该将它们整合到一个存储库中?
另外,在消费案例中呢?例如,要显示一个单一团队的日程安排,您将需要该团队的所有参与者、该参与者的所有游戏以及参与者的所有 GameSegments。如果将这些因素考虑到单独的存储库中,我看不出您如何进行有效的查询。
这是否意味着您有专门针对不同案例的存储库?例如:
public interface IScheduleRepository {
public ICollection<Game> GetScheduleForTeam(Team team);
// More consumption methods
}
public class ScheduleRepositry : IScheduleRepository {
public ScheduleRepository (ModelContext context) {
// Do stuff with context
}
public ICollection<Game> GetScheduleForTeam(Team team) {
return (
from p in context.Participants
where ((p.Game.VenueDate != null) &&
(p.TeamId == team.Id))
orderby p.Game.VenueDate.StartTime
select p.Game).ToList();
}
// more consumption methods
}
public interface IGameRepository {
public void AddGame(Game game);
// More crud methods
}
// Not showing games repository
public class GamesController : Controller {
public GamesController (IGameRepository gamesRepo,
IVenueDateRepository venueDateRepo,
IParticipantRepository participantRepo) {
// do stuff with repos here
}
[HttpPost]
public ActionResult AddGame(Game game) {
// Skipping validation logic
// this?
VenueDate = venueDateRepo.Add(game.VenueDate);
foreach (Participant p in Game.Participants)
{
participantRepo.Add(p);
}
Game = gamesRepo.AddGame(game);
// or this?
// how would the game repo know to persist
// the children elements? is that tight coupling?
Game = gamesRepo.AddGame(game);
}
// more consumption methods
}
我的问题是,我还不明白基于连接对象模型将存储库分解到何种程度才有意义。我很想在这里得到一些建议。
【问题讨论】:
标签: asp.net design-patterns dependency-injection asp.net-mvc-3