工作单元模式 - 管理父子关系
我正在使用 micro-orm (dapper),并尝试为我的存储库提供一个工作单元 (UoW) 实现。我有点困惑如何最好地处理我的 UoW 中的亲子(外键)关系。例如,如果我有以下两个直接映射到数据库表的实体:
public class User
{
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public int ClientDatabaseId { get; set; }
public ClientDatabase ClientDb { get; set; }
}
public class ClientDatabase
{
public int Id { get; set; }
public string DataSource { get; set; }
public string FailoverPartner { get; set; }
public string InitialCatalog { get; set; }
}
其中用户通过外键 User.ClientDatabaseId 与 ClientDatabase 具有父子关系。 User 和 ClientDatabase 上的 Id 属性都是标识列。我的 UoW 接口定义如下:
public interface IUnitOfWork
{
void MarkDirty(object entity);
void MarkNew(object entity);
void MarkDeleted(object entity);
void Commit();
void Rollback();
}
在某个时刻,在同一个 IUnitOfWork 中,我想为 ClientDatabase 和 User 调用 MarkNew(),然后调用 Commit()。现在我想要发生的是首先保存 ClientDatabase (子实体),然后将在 ClientDatabase 上设置的 Id 作为数据库插入的结果,设置为之前 User 上的 ClientDatabaseId 外键属性然后也被插入到数据库中。我只是想知道是否有人以一种很好的通用方式解决了此类问题?
I'm using a micro-orm (dapper) and am trying to come up with a Unit Of Work (UoW) implementation for my repositories to use. I am a little bit stumped how best to deal with parent-child (foreign key) relationships in my UoW. So for example if I have the following two entities which map directly to database tables:
public class User
{
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public int ClientDatabaseId { get; set; }
public ClientDatabase ClientDb { get; set; }
}
public class ClientDatabase
{
public int Id { get; set; }
public string DataSource { get; set; }
public string FailoverPartner { get; set; }
public string InitialCatalog { get; set; }
}
Where a User has parent-child relationship with a ClientDatabase via the foreign key User.ClientDatabaseId. The Id property on both User and ClientDatabase are Identity columns. My UoW interface is defined as follows:
public interface IUnitOfWork
{
void MarkDirty(object entity);
void MarkNew(object entity);
void MarkDeleted(object entity);
void Commit();
void Rollback();
}
At some point, within the same IUnitOfWork I want to call MarkNew() for both a ClientDatabase and a User and then Commit(). Now what I want to happen is for the ClientDatabase to be saved first (child entity) and then for the Id that was set on ClientDatabase, as a result of it's database insertion, to be set as the ClientDatabaseId foreign key property on User before it is then also inserted into the database. I just wondered whether anyone had solved this sort of problem in a nice generic fashion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接使用用户类作为聚合根。因此,在向数据库插入数据之前,代码将检查 ClientDatabase 是否不为空。如果不为空,那么您可以检查 Id 属性以查看它是新的 ClientDatabase 还是现有的 ClientDatabase(以决定是否需要执行插入或更新)。然后,您可以填充 ClientDatabaseId 属性。
Why don't you just use your User Class as an Aggregate Root. So before you do an insert to the database for the code will check if the ClientDatabase is not null. If not null then you could check the Id property to see if its a new ClientDatabase or an existing one (to decide if you need to do an insert or an update). You could then populate the ClientDatabaseId property.