コード例 #1
0
        public ContainerAdapter BuildNewAdapter(Container container)
        {
            container.Options.DefaultScopedLifestyle = new LifetimeScopeLifestyle();

            var currentAssembly = new[] {typeof(Helpers.CompositionRoot).Assembly};

            var concreteTypes = currentAssembly
                .GetImplementations(
                    ImplementationConvention.NameEndsWith,
                    new[] {"Repository"}
                ).ToList();

            var containerAdapter = new ContainerAdapter(container);

            containerAdapter.RegisterAbstractor(
                settings =>
                {
                    settings.ApplicationAssemblies = currentAssembly;
                    settings.ApplicationTypes = concreteTypes;
                });

            container.Register<ILogger, FakeLogger>(Lifestyle.Singleton);

            return containerAdapter;
        }
コード例 #2
0
        public static Container GetContainer()
        {
            // Double-checked locking pattern

            // ReSharper disable once InvertIf
            if (_container == null)
            {
                lock (Lock)
                {
                    if (_container != null) return _container;

                    _container = new Container();

                    _container.Options.DefaultScopedLifestyle = new LifetimeScopeLifestyle();

                    var currentAssembly = new[] {typeof (CompositionRoot).Assembly};

                    var concreteTypes = currentAssembly
                        .GetImplementations(
                            ImplementationConvention.NameEndsWith,
                            new[] {"Repository"}
                        ).ToList();

                    var containerAdapter = new ContainerAdapter(_container);

                    containerAdapter.RegisterAbstractor(
                        cs =>
                        {
                            cs.ApplicationAssemblies = currentAssembly;
                            cs.ApplicationTypes = concreteTypes;
                        });

                    containerAdapter.RegisterSingleton<IUnitOfWork, FakeUnitOfWork>();
                    containerAdapter.RegisterSingleton<IStopwatch, FakeStopwatch>();
                    containerAdapter.RegisterScoped<ILogger, FakeLogger>();

                    _container.Verify();
                }
            }

            return _container;
        }