コード例 #1
0
        public void BasicDIFactoryCreate()
        {
            DIFactory factory = new DIFactory();

            factory.CreationMapping.Add(typeof(MyCreatingThing), (DIFactory f) => { return(new MyCreatingThing {
                    Nuts = 88
                }); });

            Assert.IsTrue(factory.Create <MyCreatingThing>().Nuts == 88);

            factory.CreationMapping.Add(typeof(IThingy), (DIFactory f) => { return(new MyThingyImplemented()); });

            Assert.IsTrue(factory.Create <IThingy>().GetAValue() == 123);
            MyAssert.ThrowsException(() => factory.Create <DIFactory>());

            MyCreatingThing specialSetting = new MyCreatingThing {
                Nuts = 678
            };

            factory.SetSettingByType(specialSetting);

            Assert.IsTrue(factory.GetSettingByType <MyCreatingThing>().Nuts == 678);
        }
コード例 #2
0
        /// <summary>
        /// Initialize log services, preferably before you start creating log objects. Optionally, you can
        /// indicate how you want to create loggers.
        /// </summary>
        public static void Initialize(Func <DIFactory, ILogger> creator = null, bool recreateDefaultLogger = true)
        {
            if (creator != null)
            {
                if (!LogCreator.CreationMapping.ContainsKey(typeof(ILogger)))
                {
                    LogCreator.CreationMapping.Add(typeof(ILogger), creator);
                }
                else
                {
                    LogCreator.CreationMapping[typeof(ILogger)] = creator;
                }
            }

            if (recreateDefaultLogger || DefaultLogger == null)
            {
                DefaultLogger = LogCreator.Create <ILogger>();
                DefaultLogger.Initialize("DefaultLogger");
            }
        }