Ejemplo n.º 1
0
        public static ModelScheme <T> Create <T>(Specification <T> specification, ICapacityInfo capacityInfo)
        {
            ThrowHelper.NullArgument(specification, nameof(specification));

            var scopeBuilderContext = new ScopeBuilderContext();

            var rootSpecificationScopeId = scopeBuilderContext.GetOrRegisterSpecificationScope(specification);

            var discoveryContext = new DiscoveryContext(scopeBuilderContext, rootSpecificationScopeId);

            var rootSpecificationScope = (SpecificationScope <T>)scopeBuilderContext.Scopes[rootSpecificationScopeId];

            rootSpecificationScope.Discover(discoveryContext);

            if (capacityInfo is ICapacityInfoHelpersConsumer capacityInfoHelpersConsumer)
            {
                capacityInfoHelpersConsumer.InjectHelpers(CapacityInfoHelpers);
            }

            if (capacityInfo is IFeedableCapacityInfo feedableCapacityInfo && feedableCapacityInfo.ShouldFeed)
            {
                feedableCapacityInfo.Feed(discoveryContext);
            }

            return(new ModelScheme <T>(
                       scopeBuilderContext.Scopes,
                       rootSpecificationScopeId,
                       scopeBuilderContext.Errors,
                       discoveryContext.Errors.ToDictionary(p => p.Key, p => (IReadOnlyList <int>)p.Value),
                       discoveryContext.Paths.ToDictionary(p => p.Key, p => (IReadOnlyDictionary <string, string>)p.Value),
                       capacityInfo,
                       discoveryContext.ReferenceLoopRoots.Count > 0));
        }
Ejemplo n.º 2
0
            public void Should_ThrowException_When_Initialize_With_NullCapacityInfo(object rootSpecificationScope, Dictionary <int, object> specificationScopes, int rootSpecificationScopeId, Dictionary <int, IError> errorRegistry, Dictionary <string, IReadOnlyList <int> > template, Dictionary <string, IReadOnlyDictionary <string, string> > pathMap, ICapacityInfo capacityInfo, bool isReferenceLoopPossible)
            {
                _ = rootSpecificationScope;
                _ = capacityInfo;

                Action action = () => new ModelScheme <TestClass>(specificationScopes, rootSpecificationScopeId, errorRegistry, template, pathMap, null, isReferenceLoopPossible);

                action.Should().ThrowExactly <ArgumentNullException>();
            }
Ejemplo n.º 3
0
        public ModelScheme(IReadOnlyDictionary <int, object> specificationScopes, int rootSpecificationScopeId, IReadOnlyDictionary <int, IError> errorRegistry, IReadOnlyDictionary <string, IReadOnlyList <int> > template, IReadOnlyDictionary <string, IReadOnlyDictionary <string, string> > pathMap, ICapacityInfo capacityInfo, bool isReferenceLoopPossible)
        {
            ThrowHelper.NullArgument(specificationScopes, nameof(specificationScopes));
            ThrowHelper.NullInCollection(specificationScopes.Values, $"{nameof(specificationScopes)}.{nameof(specificationScopes.Values)}");
            _specificationScopes = specificationScopes;

            if (!_specificationScopes.ContainsKey(rootSpecificationScopeId))
            {
                throw new ArgumentException($"{nameof(specificationScopes)} doesn't contain specification scope with id {rootSpecificationScopeId} ({nameof(rootSpecificationScopeId)})");
            }

            if (!(_specificationScopes[rootSpecificationScopeId] is SpecificationScope <T>))
            {
                throw new ArgumentException($"specification scope with id {rootSpecificationScopeId} ({nameof(rootSpecificationScopeId)}) is not of type {typeof(SpecificationScope<T>).FullName}");
            }

            RootSpecificationScope = (SpecificationScope <T>)specificationScopes[rootSpecificationScopeId];

            ThrowHelper.NullArgument(errorRegistry, nameof(errorRegistry));
            ThrowHelper.NullInCollection(errorRegistry.Values, $"{nameof(errorRegistry)}.{nameof(errorRegistry.Values)}");
            ErrorRegistry = errorRegistry;

            ThrowHelper.NullArgument(template, nameof(template));
            ThrowHelper.NullInCollection(template.Values, $"{nameof(template)}.{nameof(template.Values)}");
            Template = template;

            ThrowHelper.NullArgument(pathMap, nameof(pathMap));
            ThrowHelper.NullInCollection(pathMap.Values, $"{nameof(pathMap)}.{nameof(pathMap.Values)}");

            foreach (var item in pathMap.Values)
            {
                foreach (var innerItem in item)
                {
                    if (innerItem.Value == null)
                    {
                        throw new ArgumentNullException($"Collection `{nameof(pathMap)}` contains null in inner dictionary under key `{innerItem.Key}`");
                    }
                }
            }

            _pathMap = pathMap;

            ThrowHelper.NullArgument(capacityInfo, nameof(capacityInfo));
            CapacityInfo             = capacityInfo;
            IsReferenceLoopPossible  = isReferenceLoopPossible;
            RootModelType            = typeof(T);
            RootSpecificationScopeId = rootSpecificationScopeId;
        }
Ejemplo n.º 4
0
            public void Should_Initialize(object rootSpecificationScope, Dictionary <int, object> specificationScopes, int rootSpecificationScopeId, Dictionary <int, IError> errorRegistry, Dictionary <string, IReadOnlyList <int> > template, Dictionary <string, IReadOnlyDictionary <string, string> > pathMap, ICapacityInfo capacityInfo, bool isReferenceLoopPossible)
            {
                var modelScheme = new ModelScheme <TestClass>(specificationScopes, rootSpecificationScopeId, errorRegistry, template, pathMap, capacityInfo, isReferenceLoopPossible);

                modelScheme.RootSpecificationScope.Should().BeSameAs(rootSpecificationScope);

                modelScheme.ErrorRegistry.Should().BeSameAs(errorRegistry);
                modelScheme.Template.Should().BeSameAs(template);
                modelScheme.CapacityInfo.Should().NotBeNull();
                modelScheme.IsReferenceLoopPossible.Should().Be(isReferenceLoopPossible);
                modelScheme.RootSpecificationScopeId.Should().Be(rootSpecificationScopeId);
            }
Ejemplo n.º 5
0
            public void Should_ThrowException_When_Initialize_With_RootSpecificationScopeId_OfSpecificationScopeOfInvalidType(object rootSpecificationScope, Dictionary <int, object> specificationScopes, int rootSpecificationScopeId, Dictionary <int, IError> errorRegistry, Dictionary <string, IReadOnlyList <int> > template, Dictionary <string, IReadOnlyDictionary <string, string> > pathMap, ICapacityInfo capacityInfo, bool isReferenceLoopPossible)
            {
                _ = rootSpecificationScope;

                var invalidRootSpecificationScope = new SpecificationScope <object>();

                specificationScopes[rootSpecificationScopeId] = invalidRootSpecificationScope;

                Action action = () => new ModelScheme <TestClass>(specificationScopes, rootSpecificationScopeId, errorRegistry, template, pathMap, capacityInfo, isReferenceLoopPossible);

                action.Should().ThrowExactly <ArgumentException>();
            }