如何更新聚合内的实体 [英] How update an entity inside Aggregate

查看:95
本文介绍了如何更新聚合内的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Campaigns的聚合,每个聚合都有一个名为Campaign的根实体,该根实体具有尝试(实体)列表。

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使用方法添加广告活动尝试

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);
        }

当我尝试编辑尝试列表中的特定项目时出现问题。我通过AttempNumber获得Attempt并将其传递给editAttempt方法,但是我不知道如何设置尝试而不删除整个列表并再次重新创建它

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");
 }

任何帮助将不胜感激!

谢谢,
佩德罗·德拉克鲁兹(Pedro de la Cruz)

Thanks, Pedro de la Cruz

推荐答案

首先,我认为可能有一个小问题与您的域模型。在我看来,广告系列应该是一个聚合的根实体,其中包含尝试值对象(或实体)的集合。除非您对包含一个广告系列的广告系列有一个父级概念,否则没有广告系列汇总。另外,没有尝试实体。而是收集尝试实体或广告系列实体上的值。如果尝试的身份位于广告系列之外,则可能是一个实体,否则它是一个价值对象。代码可能是这样的:

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:

class Campaign {
    
    public string Id { get; set; }
    
    public ICollection<Attempt> Attempts { get; private set; }
    
    public Attempt GetAttempt(string id) {
        return this.Attempts.FirstOrDefault(x => x.Number == id);
    } 
    
}
    
class Attempt {
    public string Number { get; set; }
    public string Attribute1 { get; set; }
}

如果您从Campaign实体检索了Attempt,然后更改了某些属性,则应该不必将其重新插入广告系列实体,因为它已经存在。如果您使用的是NHibernate(与其他ORM相似),则代码将是这样:

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):

var campaign = this.Session.Get<Campaign>("some-id");
    
var attempt = campaign.GetAttempt("some-attempt-id");
    
attempt.Attribute1 = "some new value";
    
this.Session.Flush(); // will commit changes made to Attempt 

这篇关于如何更新聚合内的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆