Example #1
0
        public void ResolveMultipelWithScope()
        {
            var builder = new IocBuilder();

            builder.RegisterSingleton <IA, A>();
            builder.Register <IB, B>();
            builder.Register <IC, C>();
            builder.Register <ID, D>();
            builder.Register <ID, D>();
            builder.RegisterPerScope <ID, D>();


            builder.Register <IF, F>();
            var container = builder.Build();

            IEnumerable <ID> d1;

            using (var scope = container.Scope)
            {
                d1 = scope.Resolve <IEnumerable <ID> >();
                var d2 = scope.Resolve <IEnumerable <ID> >();
                //Assert.AreNotEqual(d1.FirstOrDefault().GetHashCode(), d2.LastOrDefault().GetHashCode());
                Assert.AreEqual(d1.LastOrDefault(), d2.LastOrDefault());
            }
            var d3 = container.Resolve <IEnumerable <ID> >();

            Assert.AreNotEqual(d1.FirstOrDefault(), d3.LastOrDefault());
            Assert.AreNotEqual(d1.LastOrDefault(), d3.LastOrDefault());
        }
Example #2
0
        public void ResolveMultipleWithSingletonAndMatch()
        {
            var builder = new IocBuilder();

            builder.RegisterSingleton <IA, A>().Match <A>();
            builder.Register <IB, B>();
            builder.Register <IC, C>();
            builder.Register <ID, D>();
            builder.Register <ID, D>();
            builder.RegisterSingleton <ID, D>().Match <D>();
            builder.Register <IF, F>();
            var container = builder.Build();

            var f1 = container.Resolve <IF>();
            var f2 = container.Resolve <IF>();
            var a  = container.Resolve <A>();
            var d  = container.Resolve <D>();

            Assert.AreNotEqual(f1, f2);
            Assert.AreNotEqual(f1.Ds.FirstOrDefault().B, f2.Ds.FirstOrDefault().B);

            Assert.AreEqual(f1.Ds.FirstOrDefault().A, f2.Ds.FirstOrDefault().A);
            CollectionAssert.AreEqual(f1.Ds.Select(c => c.A).ToArray(), f2.Ds.Select(c => c.A).ToArray());
            Assert.AreEqual(f1.Ds.FirstOrDefault().A, a);
            Assert.AreEqual(f1.Ds.LastOrDefault(), d);
        }
Example #3
0
        public void MissingRegistrationThrowsExceptionOnContainerBuild()
        {
            var builder = new IocBuilder();

            builder.Register <IA, A>();
            builder.Register <IC, C>();
            builder.Register <ID, D>();
            var container = builder.Build();
        }
Example #4
0
        private static void RegisterDependencies(IocBuilder builder)
        {
            builder.Register <IRouteHandler>().Concrete <RouteHandler>();

            builder.Register <IConfigManager>().Concrete <ConfigManager>().AsSingleton();
            builder.Register <IConfigLoader>().Concrete <ConfigLoader>();

            builder.Register <IFileSystemProvider>().Concrete <FileSystemProvider>();
            builder.Register <IJsonReader>().Concrete <JsonReader>();
        }
Example #5
0
        public void ResolveMultipleOfSameType()
        {
            var builder = new IocBuilder();

            builder.Register <IA, A>();
            builder.Register <IA, A>();
            var container = builder.Build();

            var a = container.Resolve <IEnumerable <IA> >().ToArray();

            Assert.AreEqual(2, a.Count());
            Assert.AreNotEqual(a[0], a[1]);
        }
Example #6
0
        public void AdvancedResolve()
        {
            var builder = new IocBuilder();

            builder.Register <IA, A>();
            builder.Register <IB, B>();
            builder.Register <IC, C>();
            var container = builder.Build();

            var a = container.Resolve <IC>();
            var b = container.Resolve <IC>();

            Assert.AreNotEqual(a, b);
        }
Example #7
0
        public void ResolveLambda()
        {
            var builder = new IocBuilder();

            builder.Register <IA, A>();
            builder.Register <IC>(c => new C(c.Resolve <IA>(), c.Resolve <IB>()));
            builder.Register <IB, B>();
            var container = builder.Build();

            var ic = container.Resolve <IC>();
            var id = container.Resolve <IC>();

            Assert.AreNotEqual(ic, id);
        }
Example #8
0
        public void AdvancedResolveWithSingletonLambda()
        {
            var builder = new IocBuilder();

            builder.Register <IA, A>();
            builder.RegisterSingleton <IB>(c => new B());
            builder.Register <IC, C>();
            var container = builder.Build();

            var a = container.Resolve <IC>();
            var b = container.Resolve <IC>();

            Assert.AreEqual(a.B, b.B);
        }
Example #9
0
        public void DeepObjectGraphWithWrongOrderAndSingletonResolve()
        {
            var builder = new IocBuilder();

            builder.RegisterSingleton <ID, D>();
            builder.Register <IA, A>();
            builder.Register <IC, C>();
            builder.Register <IB, B>();
            var container = builder.Build();

            var d1 = container.Resolve <ID>();
            var d2 = container.Resolve <ID>();

            Assert.AreEqual(d1, d2);
        }
Example #10
0
        public void DeepObjectGraphWithWrongOrderResolve()
        {
            var builder = new IocBuilder();

            builder.Register <ID, D>();
            builder.Register <IA, A>();
            builder.Register <IC, C>();
            builder.Register <IB, B>();
            var container = builder.Build();

            var d = container.Resolve <ID>();
            var e = container.Resolve <ID>();

            Assert.AreNotEqual(d, e);
        }
Example #11
0
        public override void Register(IocBuilder builder)
        {
            LogManager.Configuration = new XmlLoggingConfiguration("log.config");

            builder
            .Register <IAppender, NLogAppender>()
            .RegisterPerScope <LoggingContext, LoggingContext>();
        }
Example #12
0
        public void RegisterResolveKeyedFunc()
        {
            var builder = new IocBuilder();

            builder.Register(c => new Func <string>(() => "hello")).IdentifiedAs("test");
            var container = builder.Build();

            var func = container.ResolveWithId <Func <string> >("test");

            Assert.AreEqual("hello", func());
        }
Example #13
0
        public void ResolveSelfRegistered()
        {
            var builder = new IocBuilder();

            builder.Register <A, A>();
            var container = builder.Build();

            var a = container.Resolve <A>();
            var b = container.Resolve <A>();

            Assert.AreNotEqual(a, b);
        }
Example #14
0
        public void ResolveKeyed()
        {
            var builder = new IocBuilder();

            builder.Register <G, G>();
            builder.RegisterSingleton <IA, A>().IdentifiedAs("test");
            var container = builder.Build();

            var g = container.Resolve <G>();
            var a = container.ResolveWithId <IA>("test");

            Assert.AreEqual(g.A, a);
        }
Example #15
0
        public void SimpleResolve()
        {
            var builder = new IocBuilder();

            builder.Register <IA, A>();

            var container = builder.Build();

            var a = container.Resolve <IA>();
            var b = container.Resolve <IA>();

            Assert.AreNotEqual(a, b);
        }
Example #16
0
        public void ScopeDisposesResolvedComponents()
        {
            var builder = new IocBuilder();

            builder.Register <E, E>();

            var container = builder.Build();

            E a = null;

            using (var scope = container.Scope)
            {
                a = scope.Resolve <E>();
                Assert.IsFalse(a.IsDisposed);
            }
            Assert.IsTrue(a.IsDisposed);
        }
Example #17
0
        public void ResolveMultipleOfSameTypeAsParameter()
        {
            var builder = new IocBuilder();

            builder.Register <IA, A>();
            builder.Register <IB, B>();
            builder.Register <IC, C>();
            builder.Register <ID, D>();
            builder.Register <ID, D>();
            builder.Register <IF, F>();
            var container = builder.Build();

            var f1 = container.Resolve <IF>();
        }
Example #18
0
 public void ConfigureServices(IocBuilder builder)
 {
     builder.Register <TestServiceTwo>().As <ITestService>();
 }
Example #19
0
 public override void Register(IocBuilder builder)
 {
     builder.Register <GameTimeline, GameTimeline>();
 }