如何更新聚合内的实体
我有一个名为“活动”的聚合,每个聚合都有一个名为“活动”的根实体,该根实体有一个尝试列表(实体)
public class Attempts: IEntity<Attempts>
{
private int id;
public AttempNumber AttemptNumber {get;}
//other fields
}
public class Campaign: IEntity<Campaign> //root
{
private int id;
public IList<Attempt> {get;}
//other fields
}
我正在使用一种方法来添加活动尝试
public virtual void AssignAttempts(Attempts att)
{
Validate.NotNull(att, "attemps are required for assignment");
this.attempts.add(att);
}
当我尝试编辑尝试列表中的特定项目时,就会出现问题。我通过 AttempNumber 获取尝试并将其传递给 editAttempt 方法,但我不知道如何设置尝试而不删除整个列表并再次重新创建它
public virtual void EditAttempts(Attempts att)
{
Validate.NotNull(att, "attemps are required for assignment");
}
任何帮助将不胜感激!
谢谢, 佩德罗·德拉克鲁斯
I have an aggregate named Campaigns every with a root entity named campaign, this root entity has a list of attempts (entity)
public class Attempts: IEntity<Attempts>
{
private int id;
public AttempNumber AttemptNumber {get;}
//other fields
}
public class Campaign: IEntity<Campaign> //root
{
private int id;
public IList<Attempt> {get;}
//other fields
}
Im using a method to add a campaign attempt
public virtual void AssignAttempts(Attempts att)
{
Validate.NotNull(att, "attemps are required for assignment");
this.attempts.add(att);
}
Problem comes when i try to edit a specific item in attempts list. I get Attempt by AttempNumber and pass it to editAttempt method but i dont know how to set the attempt without deleting whole list and recreate it again
public virtual void EditAttempts(Attempts att)
{
Validate.NotNull(att, "attemps are required for assignment");
}
Any help will be appreciated!
Thanks,
Pedro de la Cruz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我认为您的域模型可能存在一些小问题。在我看来,“营销活动”应该是一个聚合根实体,具有“尝试”值对象(或实体)的集合。除非您有包含营销活动集合的营销活动的父概念,否则不存在“营销活动”聚合。此外,不存在“尝试”实体。相反,“尝试”实体或“营销活动”实体上的值的集合。如果“尝试”在“活动”之外具有身份,则它可以是一个实体,否则它是一个值对象。代码可能是这样的:
如果您从营销活动实体检索尝试,然后更改某些属性,则不必将其插入回营销活动实体,因为它已经存在了。如果您使用 NHibernate,代码将如下所示(与其他 ORM 类似):
First, I think there may be a slight problem with your domain model. It seems to me like 'Campaign' should be an aggregate root entity having a collection of 'Attempt' value objects (or entities). There is no 'Campaigns' aggregate unless you have a parent concept to a campaign which would contain a collection of campaigns. Also, there is no 'Attempts' entity. Instead a collection of 'Attempt' entities or values on the 'Campaign' entity. 'Attempt' may be an entity if it has identity outside of a 'Campaign', otherwise it is a value object. The code could be something like this:
If you retrieve an Attempt from the Campaign entity and then change some of the properties, you should not have to insert it back into the campaign entity, it is already there. This is how the code would look if you were using NHibernate (similar for other ORMs):
您不需要 Edit 方法。您的代码可以就地修改 Attempts,如下所示:
当然,如何命名 SaveChanges() 取决于您,这是实体框架命名其通用 Save 方法的方式。
You don't need an Edit method. Your code can modify the Attempts in-place, like so:
Of course how you name the SaveChanges() is up to you, this is the way Entity Framework names its general Save method.