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);
            }
        }
Example #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 (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);
            }
        }
Example #3
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);
            }
        }