Esempio 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);
            }
        }
        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);
            }
        }
Esempio n. 3
0
        private List <MethodCtorMatch> MatchMethodsToProperties(DecodedEntityClass entityInfo)
        {
            var nonReadOnlyPropertyInfo = PropertyInfos.Where(y => y.PropertyType != DtoPropertyTypes.ReadOnly)
                                          .Select(x => x.PropertyInfo).ToList();

            _allPossibleSetterMatches = _matcher.GradeAllMethods(entityInfo.PublicSetterMethods,
                                                                 nonReadOnlyPropertyInfo, HowTheyWereAskedFor.DefaultMatchToProperties).ToList();
            return(_allPossibleSetterMatches.Where(x => x.PropertiesMatch.Score >= PropertyMatch.PerfectMatchValue).ToList());
        }
Esempio n. 4
0
        public MethodCtorMatch GetCtorStaticCreatorToRun(DecodeName nameInfo, DecodedEntityClass entityInfo)
        {
            if (nameInfo.NameType == DecodedNameTypes.NoNameGiven)
            {
                var result = GetDefaultCtorOrMethod(_matchedCtorsAndStaticMethods, true);
                if (result != null)
                {
                    return(result);
                }

                throw new InvalidOperationException($"The entity class {entityInfo.EntityType.Name} did find an ctor/static method that matches the {DtoType.Name}." +
                                                    $" It only links ctor/static methods where ALL the method's parameters can be fulfilled by the DTO/VM non-read-only properties." +
                                                    " The possible matches are \n" + string.Join("\n", _allPossibleCtorsAndStaticMatches));
            }

            return(FindMethodCtorByName(nameInfo, _matchedCtorsAndStaticMethods, "ctor/static methods"));
        }
Esempio 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);
            }
        }
        public static IStatusGeneric <DecodedDto> GetOrCreateDtoInfo(this Type classType, DecodedEntityClass entityInfo,
                                                                     IGenericServicesConfig publicConfig, PerDtoConfig perDtoConfig)
        {
            var status = new StatusGenericHandler <DecodedDto>();

            if (classType.IsPublic || classType.IsNestedPublic)
            {
                return(status.SetResult(DecodedDtoCache.GetOrAdd(classType, type => new DecodedDto(classType, entityInfo, publicConfig, perDtoConfig))));
            }

            status.AddError($"Sorry, but the DTO/ViewModel class '{classType.Name}' must be public for GenericServices to work.");
            return(status);
        }