Ejemplo n.º 1
0
        public DecodedDto(Type dtoType, DecodedEntityClass entityInfo,
                          IGenericServicesConfig publicConfig, PerDtoConfig perDtoConfig)
        {
            DtoType          = dtoType ?? throw new ArgumentNullException(nameof(dtoType));
            _matcher         = new MethodCtorMatcher(publicConfig.NameMatcher);
            _perDtoConfig    = perDtoConfig; //can be null
            LinkedEntityInfo = entityInfo;
            if (entityInfo.EntityStyle == EntityStyles.DbQuery)
            {
                //If DbQuery then exit immediately as properties etc
                return;
            }

            PropertyInfos = dtoType.GetProperties()
                            .Select(x => new DecodedDtoProperty(x,
                                                                BestPropertyMatch.FindMatch(x, entityInfo.PrimaryKeyProperties).Score >= PropertyMatch.PerfectMatchValue))
                            .ToImmutableList();

            if (!PropertyInfos.Any())
            {
                throw new InvalidOperationException("A DTO using the ILinkToEntity<T> must contain at least one Property!");
            }

            if (entityInfo.CanBeUpdatedViaMethods)
            {
                _matchedSetterMethods = PreloadPossibleMethodCtorMatches(MatchMethodsToProperties(entityInfo),
                                                                         new DecodeName(_perDtoConfig?.UpdateMethod), null);
            }
            if (entityInfo.CanBeCreatedByCtorOrStaticMethod)
            {
                _matchedCtorsAndStaticMethods = PreloadPossibleMethodCtorMatches(MatchCtorsAndStaticMethodsToProperties(entityInfo),
                                                                                 new DecodeName(_perDtoConfig?.UpdateMethod), null);
            }
        }
Ejemplo n.º 2
0
        public DecodedDto(Type dtoType, DecodedEntityClass entityInfo,
                          IGenericServicesConfig publicConfig, PerDtoConfig perDtoConfig)
        {
            DtoType          = dtoType ?? throw new ArgumentNullException(nameof(dtoType));
            _matcher         = new MethodCtorMatcher(publicConfig.NameMatcher);
            _perDtoConfig    = perDtoConfig; //can be null
            LinkedEntityInfo = entityInfo;

            if (_perDtoConfig?.UseSaveChangesWithValidation != null)
            {
                ValidateOnSave = (bool)_perDtoConfig?.UseSaveChangesWithValidation;
            }

            PropertyInfos = dtoType.GetProperties()
                            .Select(x => new DecodedDtoProperty(x,
                                                                BestPropertyMatch.FindMatch(x, entityInfo.PrimaryKeyProperties).Score >= PropertyMatch.PerfectMatchValue))
                            .ToImmutableList();

            if (entityInfo.CanBeUpdatedViaMethods)
            {
                _matchedSetterMethods = PreloadPossibleMethodCtorMatches(MatchMethodsToProperties(entityInfo),
                                                                         new DecodeName(_perDtoConfig?.UpdateMethod), null);
            }
            if (entityInfo.CanBeCreatedByCtorOrStaticMethod)
            {
                _matchedCtorsAndStaticMethods = PreloadPossibleMethodCtorMatches(MatchCtorsAndStaticMethodsToProperties(entityInfo),
                                                                                 new DecodeName(_perDtoConfig?.UpdateMethod), null);
            }
        }
        public static BestMethodCtorMatch FindMatch(ImmutableList <PropertyInfo> propertyInfos, dynamic[] whattoConsider)
        {
            if (whattoConsider.Length == 0)
            {
                return(null);
            }

            BestMethodCtorMatch bestSoFar = null;

            foreach (var method in whattoConsider.Where(x => x.GetParameters().Length > 0).OrderByDescending(x => x.GetParameters().Length))
            {
                var matchedProps = new List <BestPropertyMatch>();
                foreach (var methodParam in method.GetParameters())
                {
                    matchedProps.Add(BestPropertyMatch.FindMatch(methodParam.Name, methodParam.ParameterType, propertyInfos));
                }

                var match = new BestMethodCtorMatch(method, matchedProps);
                if (bestSoFar == null || bestSoFar.Score < match.Score)
                {
                    bestSoFar = match;
                }
            }

            return(bestSoFar);
        }
        public static BestPropertyMatch FindMatch(string name, Type type, IEnumerable<PropertyInfo> propertiesToCheck)
        {
            BestPropertyMatch bestMatch = null;
            foreach (var propertyInfo in propertiesToCheck)
            {
                var match = new BestPropertyMatch(name, type, propertyInfo);
                if (bestMatch == null || bestMatch.Score < match.Score)
                    bestMatch = match;
            }

            return bestMatch;
        }
Ejemplo n.º 5
0
        public DecodedDto(Type dtoType, DecodedEntityClass entityInfo,
                          IGenericServicesConfig publicConfig, PerDtoConfig perDtoConfig)
        {
            DtoType          = dtoType ?? throw new ArgumentNullException(nameof(dtoType));
            _matcher         = new MethodCtorMatcher(publicConfig.NameMatcher);
            _perDtoConfig    = perDtoConfig; //can be null
            LinkedEntityInfo = entityInfo;
            if (entityInfo.EntityStyle == EntityStyles.OwnedType)
            {
                throw new InvalidOperationException($"{DtoType.Name}: You cannot use ILinkToEntity<T> with an EF Core Owned Type.");
            }
            if (entityInfo.EntityStyle == EntityStyles.HasNoKey)
            {
                //If HasNoKey or is OwnedType then exit immediately as properties etc
                return;
            }

            PropertyInfos = dtoType.GetProperties()
                            .Select(x => new DecodedDtoProperty(x,
                                                                BestPropertyMatch.FindMatch(x, entityInfo.PrimaryKeyProperties).Score >= PropertyMatch.PerfectMatchValue))
                            .ToImmutableList();

            if (!PropertyInfos.Any())
            {
                throw new InvalidOperationException($"The {DtoType.Name} class inherits ILinkToEntity<T> but has no properties in it!");
            }

            if (entityInfo.CanBeUpdatedViaMethods)
            {
                _matchedSetterMethods = PreloadPossibleMethodCtorMatches(MatchMethodsToProperties(entityInfo),
                                                                         new DecodeName(_perDtoConfig?.UpdateMethod), null);
            }
            if (entityInfo.CanBeCreatedByCtorOrStaticMethod)
            {
                _matchedCtorsAndStaticMethods = PreloadPossibleMethodCtorMatches(MatchCtorsAndStaticMethodsToProperties(entityInfo),
                                                                                 new DecodeName(_perDtoConfig?.UpdateMethod), null);
            }
        }