public EntityUpdateHandler(DecodedDto dtoInfo, DecodedEntityClass entityInfo, IWrappedConfigAndMapper configAndMapper, DbContext context)
 {
     _dtoInfo         = dtoInfo ?? throw new ArgumentNullException(nameof(dtoInfo));
     _entityInfo      = entityInfo ?? throw new ArgumentNullException(nameof(entityInfo));
     _configAndMapper = configAndMapper ?? throw new ArgumentNullException(nameof(configAndMapper));
     _context         = context ?? throw new ArgumentNullException(nameof(context));
 }
 public EntityUpdateHandler(DecodedDto dtoInfo, DecodedEntityClass entityInfo, IWrappedAutoMapperConfig wrapperMapperConfigs, DbContext context)
 {
     _dtoInfo              = dtoInfo ?? throw new ArgumentNullException(nameof(dtoInfo));
     _entityInfo           = entityInfo ?? throw new ArgumentNullException(nameof(entityInfo));
     _wrapperMapperConfigs = wrapperMapperConfigs ?? throw new ArgumentNullException(nameof(wrapperMapperConfigs));
     _context              = context ?? throw new ArgumentNullException(nameof(context));
 }
        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");
        }
        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");
        }
Exemple #7
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");
        }
        public void TestDecodedDto1CheckPropertyTypes()
        {
            //SETUP

            //ATTEMPT
            var decoded = new DecodedDto(typeof(Dto1), _bookEntityInfo, new GenericServicesConfig(), null);

            //VERIFY
            decoded.LinkedEntityInfo.EntityType.ShouldEqual(typeof(Book));
            decoded.PropertyInfos.Single(x => x.PropertyType.HasFlag(DtoPropertyTypes.KeyProperty)).PropertyInfo.Name.ShouldEqual(nameof(Dto1.BookId));
            decoded.PropertyInfos.Single(x => x.PropertyType == DtoPropertyTypes.Normal).PropertyInfo.Name.ShouldEqual(nameof(Dto1.ImageUrl));
            var names = decoded.PropertyInfos.Where(x => x.PropertyType.HasFlag(DtoPropertyTypes.ReadOnly)).Select(x => x.PropertyInfo.Name).ToArray();

            names.ShouldEqual(new string[] { nameof(Dto1.BookId), nameof(Dto1.Title) });
        }
        public void TestDecodedKeyIsStringCheckPropertyTypes()
        {
            //SETUP

            //ATTEMPT
            var decoded = new DecodedDto(typeof(DddCompositeIntStringCreateDto), _EntityInfo, new GenericServicesConfig(), null);

            //VERIFY
            decoded.LinkedEntityInfo.EntityType.ShouldEqual(typeof(DddCompositeIntString));
            decoded.PropertyInfos.Where(x => x.PropertyType.HasFlag(DtoPropertyTypes.KeyProperty))
            .Select(x => x.PropertyInfo.Name).ToArray()
            .ShouldEqual(new[] { nameof(DddCompositeIntStringCreateDto.MyString), nameof(DddCompositeIntStringCreateDto.MyInt) });
            var names = decoded.PropertyInfos.Where(x => x.PropertyType.HasFlag(DtoPropertyTypes.ReadOnly)).Select(x => x.PropertyInfo.Name).ToArray();

            names.ShouldEqual(new [] { nameof(DddCompositeIntStringCreateDto.MyString), nameof(DddCompositeIntStringCreateDto.MyInt) });
        }
        public void TestNormalKeyExtract()
        {
            //SETUP
            var options = SqliteInMemory.CreateOptions <TestDbContext>();

            using (var context = new TestDbContext(options))
            {
                var decodedEntity = new DecodedEntityClass(typeof(NormalEntity), context);
                var decodeDto     = new DecodedDto(typeof(NormalEntityDto), decodedEntity, new GenericServicesConfig(), null);

                //ATTEMPT
                var dto = new NormalEntityDto {
                    Id = 123
                };
                var keys = context.GetKeysFromDtoInCorrectOrder(dto, decodeDto);

                //VERIFY
                ((int)keys[0]).ShouldEqual(123);
            }
        }
        /// <summary>
        /// This extracts the key values from the DTO
        /// We can't be sure that the DecodedDto holds the keys in the correct order, so we use EF Core to provide them
        /// </summary>
        /// <typeparam name="TDto"></typeparam>
        /// <param name="context"></param>
        /// <param name="dto"></param>
        /// <param name="dtoInfo"></param>
        /// <returns></returns>
        public static object[] GetKeysFromDtoInCorrectOrder <TDto>(this DbContext context, TDto dto, DecodedDto dtoInfo)
            where TDto : class
        {
            var keys = new List <object>();

            foreach (var entityKey in context.Model.FindEntityType(dtoInfo.LinkedEntityInfo.EntityType).FindPrimaryKey().Properties)
            {
                var dtoKeyProperty = dtoInfo.PropertyInfos.SingleOrDefault(x => x.PropertyInfo.Name == entityKey.Name);
                if (dtoKeyProperty == null)
                {
                    throw new InvalidOperationException($"The DTO/VM class {typeof(TDto).Name} does not contain the primary key {entityKey.Name}." +
                                                        " You have to include every part of a primary key in the DTO for this service to work.");
                }
                keys.Add(dtoKeyProperty.PropertyInfo.GetValue(dto));
            }

            return(keys.ToArray());
        }