public void GetsLifetimeManagers()
        {
            Assert.IsInstanceOfType(WithLifetime.ContainerControlled(typeof(MockLogger)), typeof(ContainerControlledLifetimeManager));
            Assert.IsInstanceOfType(WithLifetime.ExternallyControlled(typeof(MockLogger)), typeof(ExternallyControlledLifetimeManager));
            Assert.IsInstanceOfType(WithLifetime.Hierarchical(typeof(MockLogger)), typeof(HierarchicalLifetimeManager));
            Assert.IsNull(WithLifetime.None(typeof(MockLogger)));
            Assert.IsInstanceOfType(WithLifetime.PerResolve(typeof(MockLogger)), typeof(PerResolveLifetimeManager));
            Assert.IsInstanceOfType(WithLifetime.Transient(typeof(MockLogger)), typeof(TransientLifetimeManager));
            Assert.IsInstanceOfType(WithLifetime.Custom <CustomLifetimeManager>(typeof(MockLogger)), typeof(CustomLifetimeManager));

#if !NETFX_CORE
            Assert.IsInstanceOfType(WithLifetime.PerThread(typeof(MockLogger)), typeof(PerThreadLifetimeManager));
#endif
        }
示例#2
0
        private void ButtonFifth_Click(object sender, RoutedEventArgs e)
        {
            forthUnity.RegisterInstance(typeof(IProductFactory), new JacketProductFactory(), WithLifetime.ExternallyControlled(typeof(IProductFactory)));
            var f = forthUnity.Resolve <IProductFactory>();

            MessageBox.Show($"1 Product:{f.Create().Name}");

            f = forthUnity.Resolve <IProductFactory>(new PropertyOverride("FactoryName", "China"));
            MessageBox.Show($"2 Product:{f.Create().Name}");


            forthUnity.RegisterType <IProductFactory, JacketProductFactory>(new InjectionProperty("FactoryName", "China"));
            f = forthUnity.Resolve <IProductFactory>(new PropertyOverride("FactoryName", "China"));
            MessageBox.Show($"3 Product:{f.Create().Name}");
        }
示例#3
0
        public MainWindow()
        {
            InitializeComponent();

            firstUnity.RegisterInstance(typeof(IProductFactory), new ShoeProductFactory());


            var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            section.Configure(secondUnity);

            thirdUnity.RegisterTypes(
                AllClasses.FromLoadedAssemblies()
                .Where(x => x.IsPublic &&
                       x.GetInterfaces().Any() &&
                       !x.IsAbstract &&
                       x.IsClass),
                WithMappings.FromAllInterfacesInSameAssembly,
                type => (thirdUnity.Registrations
                         .Select(x => x.RegisteredType)
                         .Any(r => type.GetInterfaces().Contains(r)))?WithName.TypeName(type):WithName.Default(type),
                WithLifetime.ContainerControlled);

            forthUnity.RegisterInstance(typeof(IProductFactory), new JacketProductFactory(), WithLifetime.ExternallyControlled(typeof(IProductFactory)));
        }