Пример #1
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);
        }
Пример #2
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());
        }
Пример #3
0
        public CedrusGame(IocBuilder builder)
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            builder.LogRegistrations();
            builder.RegisterModules(Assembly.GetExecutingAssembly(), type => type.FullName.EndsWith("Module"));

            builder.RegisterSingleton(c => graphics.GraphicsDevice);
            builder.RegisterSingleton(c => new GameSettings(Window));
            builder.RegisterSingleton(c => Content);
            EnumerateAssemblies(assembly => builder.RegisterSingletonSubclassesOf(assembly, typeof(CommonAssetBuilder)));

            container = builder.Build();

            gameTimeline = container.Resolve <GameTimeline>();
            Appender     = container.Resolve <IAppender>()[GetType()];
        }
Пример #4
0
 public override void Register(IocBuilder builder)
 {
     builder.RegisterSingleton(c =>
                               RenderingUtilities.CreateRenderTarget(c.Resolve <GraphicsDevice>(), 10, 10))
     .IdentifiedAs("background")
     .Register(c =>
               new Func <Color>(() => Color.White))
     .IdentifiedAs("backgroundColor")
     .RegisterSingleton <BackgroundLayerRenderer, BackgroundLayerRenderer>();
 }
Пример #5
0
        public void ResolveSingletonLambda()
        {
            var builder = new IocBuilder();

            builder.RegisterSingleton <IA>(c => new A());
            var container = builder.Build();

            var a1 = container.Resolve <IA>();
            var a2 = container.Resolve <IA>();

            Assert.AreEqual(a1, a2);
        }
Пример #6
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);
        }
Пример #7
0
        public void ResolveEnumerableSingletonWithMatch()
        {
            var builder = new IocBuilder();

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

            var a3 = container.Resolve <IEnumerable <A> >();
            var a1 = container.Resolve <IA>();
            var a2 = container.Resolve <IA>();

            Assert.AreEqual(a1, a2);
            Assert.AreEqual(a2, a3.FirstOrDefault());
        }
Пример #8
0
        public void ResolveSingletonWithMatch()
        {
            var builder = new IocBuilder();

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

            var a1 = container.Resolve <IA>();
            var a2 = container.Resolve <IA>();
            var a3 = container.Resolve <A>();

            Assert.AreEqual(a1, a2);
            Assert.AreEqual(a2, a3);
        }
Пример #9
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);
        }
Пример #10
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);
        }