Inheritance: System.Entity
 internal CreateContactPhone(IPrincipal principal, AgreementContact contact)
 {
     if (principal == null)
     {
         throw new ArgumentNullException("principal");
     }
     Principal = principal;
     Contact   = contact;
     ContactId = contact.Id;
 }
        internal static string ToJsonAudit(this AgreementContact entity)
        {
            var state = JsonConvert.SerializeObject(new
            {
                entity.Id,
                entity.Guid,
                entity.AgreementId,
                entity.PersonId,
                entity.ParticipantAffiliationId,
                entity.Type,
                entity.Title,
            });

            return(state);
        }
        public void Handle(CreateContact command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            // are we attaching to existing person, or creating a new one?
            var person = command.PersonId.HasValue
                ? _entities.Get <Person>().Single(x => x.RevisionId == command.PersonId.Value) : null;

            if (person == null)
            {
                // create person entity
                var createPersonCommand = new CreatePerson
                {
                    Salutation = command.Salutation,
                    FirstName  = command.FirstName,
                    MiddleName = command.MiddleName,
                    LastName   = command.LastName,
                    Suffix     = command.Suffix,
                    NoCommit   = true,
                };
                _createPerson.Handle(createPersonCommand);
                person = createPersonCommand.CreatedPerson;

                // attach email address if provided
                if (!string.IsNullOrWhiteSpace(command.EmailAddress))
                {
                    _createEmailAddress.Handle(new CreateEmailAddress(command.EmailAddress, person)
                    {
                        IsDefault = true,
                        NoCommit  = true,
                    });
                }
            }

            var entity = new AgreementContact
            {
                AgreementId = command.AgreementId,
                Person      = person,
                Type        = command.Type,
                Title       = command.JobTitle,
            };

            _entities.Create(entity);

            if (command.Phones != null)
            {
                foreach (var phone in command.Phones)
                {
                    _createPhone.Handle(new CreateContactPhone(command.Principal, entity)
                    {
                        NoCommit = true,
                        Type     = phone.Type,
                        Value    = phone.Value,
                    });
                }
            }

            // log audit
            var audit = new CommandEvent
            {
                RaisedBy = command.Principal.Identity.Name,
                Name     = command.GetType().FullName,
                Value    = JsonConvert.SerializeObject(new
                {
                    command.AgreementId,
                    command.Type,
                    command.PersonId,
                    command.Salutation,
                    command.FirstName,
                    command.MiddleName,
                    command.LastName,
                    command.Suffix,
                    command.EmailAddress,
                    command.JobTitle,
                }),
                NewState = entity.ToJsonAudit(),
            };

            _entities.Create(audit);

            _unitOfWork.SaveChanges();
            command.CreatedContactId = entity.Id;
        }