public static CasePlanInstanceAggregate ToDomain(this CasePlanInstanceModel record)
 {
     return(new CasePlanInstanceAggregate
     {
         AggregateId = record.Id,
         Version = record.Version,
         CasePlanId = record.CasePlanId,
         CaseOwner = record.CaseOwner?.RoleId,
         Name = record.Name,
         State = record.CaseState == null ? (CaseStates?)null : (CaseStates)record.CaseState,
         ExecutionContext = string.IsNullOrWhiteSpace(record.ExecutionContext) ? null : JsonConvert.DeserializeObject <CasePlanInstanceExecutionContext>(record.ExecutionContext),
         Roles = record.Roles.Select(_ => ToCasePlanInstanceRoleDomain(_)).ToList(),
         Files = new ConcurrentBag <CasePlanInstanceFileItem>(record.Files.Select(_ => ToDomain(_)).ToList()),
         WorkerTasks = new ConcurrentBag <CasePlanInstanceWorkerTask>(record.WorkerTasks.Select(_ => ToDomain(_)).ToArray()),
         Children = new ConcurrentBag <BaseCaseEltInstance>(record.Children.Where(_ => _.ParentId == null).Select(c => c.ToElementInstance()).ToList()),
         CreateDateTime = record.CreateDateTime,
         UpdateDateTime = record.UpdateDateTime
     });
 }
        public static CasePlanInstanceModel ToModel(this CasePlanInstanceAggregate record)
        {
            var result = new CasePlanInstanceModel
            {
                Id               = record.AggregateId,
                Version          = record.Version,
                CasePlanId       = record.CasePlanId,
                Name             = record.Name,
                CaseState        = record.State == null ? null : (int?)record.State,
                ExecutionContext = record.ExecutionContext == null ? null : JsonConvert.SerializeObject(record.ExecutionContext),
                Roles            = record.Roles.Select(_ => ToModel(_, record.AggregateId, record.CaseOwner)).ToList(),
                Files            = record.Files.Select(_ => ToModel(_, record.AggregateId)).ToList(),
                WorkerTasks      = record.WorkerTasks.Select(_ => ToModel(_, record.AggregateId)).ToList(),
                Children         = record.Children == null ? new List <CasePlanElementInstanceModel>() : record.Children.Select(_ => _.ToModel(record.AggregateId)).ToList(),
                CreateDateTime   = record.CreateDateTime,
                UpdateDateTime   = record.UpdateDateTime
            };

            return(result);
        }