Пример #1
0
        private static void RegisterRepositories()
        {
            var types = typeof(TContext).Assembly.GetTypes();

            var repoApis = from type in types
                           let genericApi = type.GetInterfaces().FirstOrDefault(i => i.GetGenericArguments().Length == 1 && typeof(IRepository <>) == i.GetGenericTypeDefinition())
                                            where type.IsInterface && genericApi != null
                                            select new { RepoApi = type, GenericApi = genericApi };

            foreach (var apiPair in repoApis)
            {
                Type repoProxy;
                // Find implementations for the RepoAPI, which will also fit the generic API because of the inheritance
                var implementations = types.Where(t => t.IsClass && apiPair.RepoApi.IsAssignableFrom(t)).ToList();
                if (implementations.Count == 0)
                {
                    repoProxy = ProxyBuilder.Build(apiPair.RepoApi);
                }
                else
                {
                    var selectedImpl = implementations.First();
                    repoProxy = ProxyBuilder.Build(apiPair.RepoApi, selectedImpl);
                }

                var constructorDelegate = ReflectionTool.ConstructorDelegate <Repository>(repoProxy);
                // Register constructor for both interfaces
                Repositories[apiPair.RepoApi] = Repositories[apiPair.GenericApi] = constructorDelegate;
            }
        }
Пример #2
0
        public void EmptyRepositoryInterface()
        {
            // Act
            Type proxyType = null;

            Assert.DoesNotThrow(delegate
            {
                proxyType = _proxyBuilder.Build(typeof(IEmptyRepository));
            });

            // Assert
            var baseType = proxyType.BaseType;

            Assert.IsNotNull(baseType);

            var genericBaseType = baseType.GetGenericTypeDefinition();

            Assert.AreEqual(typeof(Repository <>), genericBaseType);
        }