Esempio n. 1
0
        public void Register(RestRegistryEntry entry)
        {
            if (!entry.Type.IsRestModel())
            {
                throw new ArgumentException("Entry type must be a valid subtype of RestModel");
            }

            _entryDictionary[entry.Type] = entry;
        }
Esempio n. 2
0
        public void CheckIfExceptionThrownOnInvalidTypeRegister()
        {
            RestRegistry      registry = new RestRegistry();
            RestRegistryEntry entry    = new RestRegistryEntry(typeof(string), RestAllowedOperations.All);

            Assert.Throws <ArgumentException>(() =>
            {
                registry.Register(entry);
            });
        }
Esempio n. 3
0
        public void CheckIfTypeIsRegisteredWithValidAllowedOperations()
        {
            RestRegistry          registry = new RestRegistry();
            Type                  testType = typeof(ClassForRestRegistryTest);
            RestAllowedOperations testTypeAllowedOperations = RestAllowedOperations.All;
            RestRegistryEntry     testTypeEntry             = new RestRegistryEntry(testType, testTypeAllowedOperations);

            registry.Register(testTypeEntry);
            RestRegistryEntry registeredEntry = registry.Entries
                                                .Single(e => e.Type == testType);

            Assert.Equal(testTypeAllowedOperations, registeredEntry.AllowedOperations);
        }
Esempio n. 4
0
        public void CheckIfTypeIsRegistered()
        {
            RestRegistry          registry = new RestRegistry();
            Type                  testType = typeof(ClassForRestRegistryTest);
            RestAllowedOperations testTypeAllowedOperations = RestAllowedOperations.All;
            RestRegistryEntry     testTypeEntry             = new RestRegistryEntry(testType, testTypeAllowedOperations);

            registry.Register(testTypeEntry);
            RestRegistryEntry registeredEntry = registry.Entries
                                                .SingleOrDefault(e => e.Type == testType);

            Assert.NotNull(registeredEntry);
        }
        public RestOptionsBuilder WithModelOptions <TModel>(Action <RestModelOptionsBuilder> buildAction) where TModel : RestModel
        {
            Type modelType = typeof(TModel);
            RestRegistryEntry modelTypeEntry = _registry.Entries
                                               .SingleOrDefault(e => e.Type == modelType);

            if (modelTypeEntry == null)
            {
                throw new InvalidOperationException("Provided model type is not registered in REST registry");
            }

            RestModelOptionsBuilder options = new RestModelOptionsBuilder(modelTypeEntry.AllowedOperations);

            buildAction?.Invoke(options);
            RestRegistryEntry newModelTypeEntry = new RestRegistryEntry(modelType, options.AllowedOperations);

            _registry.Register(newModelTypeEntry);
            return(this);
        }
        private static RestOptionsBuilder RegisterRestModelTypes(this IServiceCollection services, IEnumerable <Type> modelTypes)
        {
            IRestRegistry restRegistry        = new RestRegistry();
            Type          iDataRepositoryType = typeof(IDataRepository <>);
            Type          dataRepositoryType  = typeof(DataRepository <>);

            foreach (Type modelType in modelTypes)
            {
                RestAllowedOperations modelAllowedOperations = modelType.GetRestAllowedOperations();
                RestRegistryEntry     modelTypeEntry         = new RestRegistryEntry(modelType, modelAllowedOperations);
                restRegistry.Register(modelTypeEntry);
                services.AddScoped(
                    iDataRepositoryType.MakeGenericType(modelType),
                    dataRepositoryType.MakeGenericType(modelType)
                    );
            }

            services.AddSingleton(typeof(IRestRegistry), restRegistry);
            services.AddSingleton <IRestRouter, RestRouter>();
            services.AddRouting();
            RestOptionsBuilder options = new RestOptionsBuilder(restRegistry);

            return(options);
        }