Ejemplo n.º 1
0
        public static IArrangeDependencies Config <TInterface, TImplementation>(Action <IArrangeBuilder <ContainerBuilder> > config = null)
            where TInterface : class
            where TImplementation : class, TInterface
        {
            var arrangeBuilder = new ArrangeBuilder();

            config?.Invoke(arrangeBuilder);

            arrangeBuilder.UseImplementation <TInterface, TImplementation>();

            var containerBuilder = arrangeBuilder.Build();

            containerBuilder.Register((context) =>
            {
                var constructors = typeof(TImplementation).GetConstructors();
                var parameters   = constructors.SelectMany(x => x.GetParameters()).Where(x => x.ParameterType.IsInterface).Distinct();
                var args         = new List <object>();

                foreach (var parameter in parameters)
                {
                    args.Add(context.ResolveOptional(parameter.ParameterType) ?? MockHelper.CreateMock(parameter.ParameterType));
                }

                return(Activator.CreateInstance(typeof(TImplementation), args.ToArray()) as TImplementation);
            }).As <TInterface>().IfNotRegistered(typeof(TInterface));

            var container = containerBuilder.Build();

            return(new ArrangeDependencies(container));
        }
        private static Mock <T> AddMock <T>(ArrangeBuilder arrangeBuilder, Action <Mock <T> > mock) where T : class
        {
            var mockObject = new Mock <T>();

            mock.Invoke(mockObject);

            arrangeBuilder.AddDependency((c) => c.Register <T>((context) => mockObject.Object));

            return(mockObject);
        }
Ejemplo n.º 3
0
        public static IArrangeDependencies Config(Action <IArrangeBuilder <ContainerBuilder> > config = null)
        {
            var arrangeBuilder = new ArrangeBuilder();

            config?.Invoke(arrangeBuilder);

            var containerBuilder = arrangeBuilder.Build();
            var container        = containerBuilder.Build();

            return(new ArrangeDependencies(container));
        }
Ejemplo n.º 4
0
        private static Mock <ILogger <T> > AddLogger <T>(ArrangeBuilder arrangeBuilder)
        {
            arrangeBuilder.UseMock <ILogger <T> >(mock =>
            {
                foreach (var logLevel in Enum.GetValues(typeof(LogLevel)).Cast <LogLevel>())
                {
                    mock.SetupLogLevel(logLevel);
                }
            }, out var logger);

            arrangeBuilder.UseMock <ILoggerFactory>(mock =>
            {
                mock.Setup(x => x.CreateLogger(It.IsAny <string>())).Returns(logger.Object);
            });

            return(logger);
        }
Ejemplo n.º 5
0
        private static IMemoryCache AddMemoryCache(ArrangeBuilder arrangeBuilder)
        {
            if (arrangeBuilder.IsTypeCached <IMemoryCache>(out var result))
            {
                return(result);
            }

            var serviceCollection = new ServiceCollection();

            serviceCollection.AddMemoryCache();
            var cache = serviceCollection.BuildServiceProvider().GetService <IMemoryCache>();

            arrangeBuilder.UseContainerBuilder((c) => c.Register(t => cache).As <IMemoryCache>());
            arrangeBuilder.AddTypeToCache(cache);

            return(cache);
        }
Ejemplo n.º 6
0
        private static void AddDependency <TInterface>(ArrangeBuilder arrangeBuilder, Type implementation)
            where TInterface : class
        {
            arrangeBuilder.AddDependency((containerBuilder) => containerBuilder.Register((context) =>
            {
                var constructors = implementation.GetConstructors();
                var parameters   = constructors.SelectMany(x => x.GetParameters()).Distinct();
                var args         = new List <object>();

                foreach (var parameter in parameters)
                {
                    args.Add(context.ResolveOptional(parameter.ParameterType) ?? MockHelper.CreateMock(parameter.ParameterType));
                }

                return(Activator.CreateInstance(implementation, args.ToArray()) as TInterface);
            }).As <TInterface>());
        }
Ejemplo n.º 7
0
        private static TContext AddDbContext <TContext>(ArrangeBuilder arrangeBuilder)
            where TContext : DbContext
        {
            if (arrangeBuilder.IsTypeCached <TContext>(out var result))
            {
                return(result);
            }

            var root = new InMemoryDatabaseRoot();
            var serviceCollection = new ServiceCollection();

            serviceCollection.AddDbContext <TContext>(config => config.UseInMemoryDatabase(arrangeBuilder.GetHashCode().ToString(), root)
                                                      .ConfigureWarnings(warnings => warnings.Ignore(CoreEventId.ManyServiceProvidersCreatedWarning)));

            arrangeBuilder.UseContainerBuilder((c) => c.Populate(serviceCollection));

            var db = serviceCollection.BuildServiceProvider().GetService <TContext>();

            arrangeBuilder.AddTypeToCache(db);

            return(db);
        }
Ejemplo n.º 8
0
 private static void AddImplementationsOf <TInterface>(ArrangeBuilder arrangeBuilder, params Assembly[] assemblies)
 {
 }
Ejemplo n.º 9
0
 private static void AddDependency(ArrangeBuilder arrangeBuilder, Action <ContainerBuilder> dependency)
 {
     arrangeBuilder.AddDependency(dependency);
 }