/// <summary>
        /// This look for methods to update a new entity in the following order
        /// 1. DDD-styled entity: A public access method that fits the DTO
        /// 2. Standard styled entity: using AutoMapper to update the entity</summary>
        /// <param name="dto"></param>
        /// <param name="methodName"></param>
        /// <param name="entity"></param>
        /// <param name="mapper"></param>
        /// <returns></returns>
        private IStatusGeneric RunMethodViaLinq(TDto dto, string methodName, dynamic entity, CreateMapper mapper)
        {
            var decodedName = _dtoInfo.GetSpecifiedName(methodName, CrudTypes.Update);

            if (_entityInfo.CanBeUpdatedViaMethods && decodedName.NameType != DecodedNameTypes.AutoMapper)
            {
                //1. DDD-styled entity: A public access method that fits the DTO

                //This gets one method to run. If it can't be found, or there are too many matches it throws an exception
                var methodToUse = _dtoInfo.GetMethodToRun(decodedName, _entityInfo);

                //This runs the method via LINQ
                return(BuildCall.RunMethodViaLinq(methodToUse.Method,
                                                  dto, entity, methodToUse.PropertiesMatch.MatchedPropertiesInOrder.ToList(), _context));
            }

            if (_entityInfo.CanBeUpdatedViaProperties)
            {
                //2. Standard styled entity: using AutoMapper to update the entity
                mapper.Accessor.MapDtoToEntity(dto, entity);
                return(new StatusGenericHandler());
            }

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

            //ATTEMPT
            var ex = Assert.Throws <InvalidOperationException>(() => decoded.GetMethodToRun(new DecodeName("UpdatePublishedOn(2)"), _bookEntityInfo));

            //VERIFY
            ex.Message.ShouldStartWith("Could not find a method of name UpdatePublishedOn(2)");
        }
        public void TestFindSetterMethodWithGivenNumParams()
        {
            //SETUP
            var decoded = new DecodedDto(typeof(Dtos.ChangePubDateDto), _bookEntityInfo, new GenericServicesConfig(), null);

            //ATTEMPT
            var method = decoded.GetMethodToRun(new DecodeName("UpdatePublishedOn(1)"), _bookEntityInfo);

            //VERIFY
            method.Method.Name.ShouldEqual("UpdatePublishedOn");
        }
        public void TestCheckExtraParamsOk()
        {
            //SETUP
            var decoded = new DecodedDto(typeof(ExtraParamsDto), _bookEntityInfo, new GenericServicesConfig(), null);

            //ATTEMPT
            var method = decoded.GetMethodToRun(new DecodeName(null), _bookEntityInfo);

            //VERIFY
            method.Method.Name.ShouldEqual("UpdatePublishedOn");
        }
示例#5
0
        public void TestGetDefaultSetterMethod()
        {
            //SETUP
            var decoded = new DecodedDto(typeof(Tests.Dtos.ChangePubDateDto), _bookEntityInfo, new GenericServicesConfig(), null);

            //ATTEMPT
            var method = decoded.GetMethodToRun(new DecodeName(null), _bookEntityInfo);

            //VERIFY
            method.Method.Name.ShouldEqual("UpdatePublishedOn");
        }
示例#6
0
        public IStatusGeneric ReadEntityAndUpdateViaDto(TDto dto, string methodName, params Expression <Func <TDto, object> >[] includes)
        {
            //first we need to load it
            var keys   = _context.GetKeysFromDtoInCorrectOrder(dto, _dtoInfo);
            var mapper = new CreateMapper(_context, _configAndMapper, typeof(TDto), _entityInfo);

            var entity = mapper.Accessor.ReturnExistingEntity(keys, includes);

            if (entity == null)
            {
                return(new StatusGenericHandler()
                       .AddError(
                           $"Sorry, I could not find the {_entityInfo.EntityType.GetNameForClass()} you were trying to update."));
            }

            //we look for methods to update a new entity in the following order
            //1. DDD-styled entity: A public access method that fits the DTO
            //2. Standard styled entity: using AutoMapper to update the entity

            var decodedName = _dtoInfo.GetSpecifiedName(methodName, CrudTypes.Update);

            if (_entityInfo.CanBeUpdatedViaMethods && decodedName.NameType != DecodedNameTypes.AutoMapper)
            {
                //1. DDD-styled entity: A public access method that fits the DTO

                //This gets one method to run. If it can't be found, or there are too many matches it throws an exception
                var methodToUse = _dtoInfo.GetMethodToRun(decodedName, _entityInfo);

                //This runs the method via LINQ
                return(BuildCall.RunMethodViaLinq(methodToUse.Method,
                                                  dto, entity, methodToUse.PropertiesMatch.MatchedPropertiesInOrder.ToList(), _context, _createNewDBContext));
            }

            if (_entityInfo.CanBeUpdatedViaProperties)
            {
                //2. Standard styled entity: using AutoMapper to update the entity
                mapper.Accessor.MapDtoToEntity(dto, entity);
                return(new StatusGenericHandler());
            }

            throw new InvalidOperationException(
                      $"There was no way to update the entity class {_entityInfo.EntityType.Name} using {decodedName.ToString() ?? "any approach"}.");
        }