如何使用存储库模式预先加载子实体

发布于 2024-10-22 20:30:56 字数 3083 浏览 1 评论 0原文

我有一个名为 Tour 的实体,它可以有许多 Agent。我可以添加代理,但无法删除它们。

// _repo is injected....
var tour = _repo.GetById(tourId);
tour.AddAgent(new Agent(tour.TourId));

当我尝试调用 Tour.RemoveAgent() 方法时,实际上没有删除任何内容。我在 Tour.RemoveAgent() 方法中设置了一个断点,我看到 _agents 属性的计数为 0

tour.RemoveAgent(agentId); // This doesn't work because _agents is empty

当我从存储库检索 Tour 时,是否需要对 EF 执行一些特殊操作来填充 _agents 属性?

更新:问题已解决(感谢 Paul 的回答)

我决定为每个聚合创建一个唯一的存储库,这样就可以轻松地使用 Include() 函数准确定义需要包含的内容。这是一个我从 GenericRepository 类继承的示例(该类也包含在这个问题的底部)。

public class TourRepository : GenericRepository<Tour>
{
    public TourRepository(IDatabaseFactory databaseFactory) : base (databaseFactory)
    {
    }

    public override Tour GetById(Guid id)
    {
        return dataContext.Tours
                .Include(x => x.Agents)
                .Single(x => x.TourId == id);
    }
}

游览类

public partial class Tour
{
  public Guid TourId { get; private set; }
  protected virtual List<Agent> _agents { get; set; }

  public Tour()
  {
    TourId = Guid.NewGuid();
    _agents = new List<Agent>();
  }

  public void AddAgent(Agent agent)
  {
    _agents.Add(agent);
  }

  public void RemoveAgent(Guid agentId)
  {
    _agents.RemoveAll(a => a.AgentId == agentId);
  }
}

代理类

public partial class Agent
{
  public Guid AgentId { get; private set; }
  public Guid TourId { get; private set; }
  public Tour Tour { get; private set; }

  public Agent(Guid tourId)
  {
    TourId = tourId;
    AgentId = Guid.NewGuid();
  }
}

OnModelCreating

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // AGENTS ============================
  modelBuilder.Entity<Agent>()
              .HasKey(x => x.AgentId)
              .Property(p => p.AgentId);

  modelBuilder.Entity<Agent>()
              .HasRequired(p => p.Tour)
              .WithMany(t => t.Agents);

  // TOURS =============================
  modelBuilder.Entity<Tour>()
              .HasKey(x => x.TourId)
              .Property(x => x.TourId);
}

存储库类

public class GenericRepository<T> : IRepository<T> where T : class {
  private MyContext dataContext;
  private readonly IDbSet<T> dbset;  
  
  public GenericRepository(IDatabaseFactory databaseFactory)
  {
    DatabaseFactory = databaseFactory;
    dbset = DataContext.Set<T>();
  }

  protected IDatabaseFactory DatabaseFactory
  {
    get;
    private set;
  }

  protected MyContext DataContext
  {
    get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
  }
  
  // ... stuff removed for brevity ...
  
  public T GetById(Guid id)
  {
    return dbset.Find(id);
  }
}

I have an entity named Tour which can have many Agents. I am able to add agents, but I cannot remove them.

// _repo is injected....
var tour = _repo.GetById(tourId);
tour.AddAgent(new Agent(tour.TourId));

When I attempt to call the Tour.RemoveAgent() method nothing is actually removed. I set a breakpoint inside the Tour.RemoveAgent() method I see that the _agents property has a count of 0.

tour.RemoveAgent(agentId); // This doesn't work because _agents is empty

Do I have to do something special for EF to populate the _agents property when I retrieve the Tour from my repository?

UPDATE: PROBLEM SOLVED (thanks to Paul's answer)

I decided to just create a Repository unique to each aggregate, that way it is easy to define exactly what needs to be included using the Include() function. This is an example where I inherit from the GenericRepository<T> class (which is also included at the bottom of this question).

public class TourRepository : GenericRepository<Tour>
{
    public TourRepository(IDatabaseFactory databaseFactory) : base (databaseFactory)
    {
    }

    public override Tour GetById(Guid id)
    {
        return dataContext.Tours
                .Include(x => x.Agents)
                .Single(x => x.TourId == id);
    }
}

Tour Class

public partial class Tour
{
  public Guid TourId { get; private set; }
  protected virtual List<Agent> _agents { get; set; }

  public Tour()
  {
    TourId = Guid.NewGuid();
    _agents = new List<Agent>();
  }

  public void AddAgent(Agent agent)
  {
    _agents.Add(agent);
  }

  public void RemoveAgent(Guid agentId)
  {
    _agents.RemoveAll(a => a.AgentId == agentId);
  }
}

Agent Class

public partial class Agent
{
  public Guid AgentId { get; private set; }
  public Guid TourId { get; private set; }
  public Tour Tour { get; private set; }

  public Agent(Guid tourId)
  {
    TourId = tourId;
    AgentId = Guid.NewGuid();
  }
}

OnModelCreating

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
  // AGENTS ============================
  modelBuilder.Entity<Agent>()
              .HasKey(x => x.AgentId)
              .Property(p => p.AgentId);

  modelBuilder.Entity<Agent>()
              .HasRequired(p => p.Tour)
              .WithMany(t => t.Agents);

  // TOURS =============================
  modelBuilder.Entity<Tour>()
              .HasKey(x => x.TourId)
              .Property(x => x.TourId);
}

Repository Class

public class GenericRepository<T> : IRepository<T> where T : class {
  private MyContext dataContext;
  private readonly IDbSet<T> dbset;  
  
  public GenericRepository(IDatabaseFactory databaseFactory)
  {
    DatabaseFactory = databaseFactory;
    dbset = DataContext.Set<T>();
  }

  protected IDatabaseFactory DatabaseFactory
  {
    get;
    private set;
  }

  protected MyContext DataContext
  {
    get { return dataContext ?? (dataContext = DatabaseFactory.Get()); }
  }
  
  // ... stuff removed for brevity ...
  
  public T GetById(Guid id)
  {
    return dbset.Find(id);
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫南 2024-10-29 20:30:57

尝试制作 protected virtual List _agents { get;放; 您还可以通过执行以下

public virtual List<Agent> _agents { get; set; }

操作来预先加载:

_databaseContext.Tours.Include(x => x.Agents).Single(x => x.TourId == tourId) 

您可以在此处阅读更多信息: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-lated-entities。 ASPX

Try making protected virtual List _agents { get; set; } public

public virtual List<Agent> _agents { get; set; }

You can also eager load by doing something like this:

_databaseContext.Tours.Include(x => x.Agents).Single(x => x.TourId == tourId) 

you can read more here: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文