示例#1
0
        public object CreateEntityAndFillFromDto(TDto dto, string methodCtorName)
        {
            if (!_entityInfo.CanBeCreatedByCtorOrStaticMethod && !_entityInfo.CanBeCreatedViaAutoMapper)
            {
                throw new InvalidOperationException($"I cannot create the entity class {_entityInfo.EntityType.Name} because it has no public constructor, or valid static creator methods.");
            }

            //we look for methods to create/update a new entity in the following order
            //1. A public static method that returns IStatusGeneric (chosing the one with the most parameters that the DTO has too)
            //2. A public parameterised constructor (chosing the one with the most parameters that the DTO has too)
            //3. By creating via parameterless ctor and then using AutoMapper to set the properties

            var decodedName = _dtoInfo.GetSpecifiedName(methodCtorName, CrudTypes.Create);

            if (_entityInfo.CanBeCreatedByCtorOrStaticMethod)
            {
                var ctorStaticToRun = _dtoInfo.GetCtorStaticCreatorToRun(decodedName, _entityInfo);
                var runStatus       = BuildCall.RunMethodOrCtorViaLinq(ctorStaticToRun,
                                                                       dto, ctorStaticToRun.PropertiesMatch.MatchedPropertiesInOrder.ToList(), _context);
                CombineStatuses(runStatus);
                return(runStatus.Result);
            }

            if (_entityInfo.HasPublicParameterlessCtor && _entityInfo.CanBeUpdatedViaProperties)
            {
                var entityInstance = Activator.CreateInstance(_entityInfo.EntityType);
                var mapper         = new CreateMapper(_context, _wrapperMapperConfigs, typeof(TDto), _entityInfo);
                mapper.Accessor.MapDtoToEntity(dto, entityInstance);
                return(entityInstance);
            }

            throw new InvalidOperationException(
                      $"There was no way to create the entity class {_entityInfo.EntityType.Name} using {decodedName.ToString() ?? "any approach"}.");
        }
        public void TestFindSetterMethodWithGivenNumParams()
        {
            //SETUP
            var decoded = new DecodedDto(typeof(DddCompositeIntStringCreateDto), _EntityInfo, new GenericServicesConfig(), null);

            //ATTEMPT
            var methodOrCtor = decoded.GetCtorStaticCreatorToRun(new DecodeName(null), _EntityInfo);

            //VERIFY
            methodOrCtor.Name.ShouldEqual("Ctor");
        }