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
        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);
        }
Esempio n. 5
0
 public ConfigGenerator(PerDtoConfig <TDto, TEntity> config)
 {
     _config = config;
 }