public static void RegisterTypesFromAssemblies(this IUnityContainer container, Assembly[] assemblies)
        {
            container.RegisterTypes(
                AllClasses.FromAssemblies(assemblies),
                WithMappings.FromMatchingInterface,
                WithName.Default,
                (instanceType) => {
                LifetimeManager result = null;
                var ioc = instanceType.GetCustomAttributes(false).OfType <IOCAttribute>().FirstOrDefault();
                if (ioc == null)
                {
                    result = WithLifetime.Hierarchical(instanceType);
                }
                else
                {
                    switch (ioc.LifeCycle)
                    {
                    case IOCLifeCycleType.Singleton: result = WithLifetime.ContainerControlled(instanceType); break;

                    case IOCLifeCycleType.PerThread: result = WithLifetime.PerThread(instanceType); break;

                    case IOCLifeCycleType.PerResolve: result = WithLifetime.PerResolve(instanceType); break;

                    case IOCLifeCycleType.PerRequest: result = WithLifetime.Hierarchical(instanceType); break;

                    default: result = WithLifetime.Hierarchical(instanceType); break;
                    }
                }
                return(result);
            });
        }
示例#2
0
            public override Func <Type, LifetimeManager> GetLifetimeManager()
            {
                var bt = typeof(BaseBiz);

                return(t => {
                    if (t.IsSubclassOf(bt))
                    {
                        //return WithLifetime.Transient(t);
                        return WithLifetime.PerResolve(t);
                    }
                    return WithLifetime.ContainerControlled(t);
                });
            }
        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
        }
示例#4
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}");
        }
示例#5
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)));
        }
示例#6
0
        public static void All(IUnityContainer c)
        {
            if (c == null)
            {
                return;
            }
            if (Container == null)
            {
                Container = c;
            }

            List <Assembly> list = AppDomain.CurrentDomain.GetAssemblies().ToList();

            foreach (Assembly assembly in list)
            {
                IEnumerable <Type> types = from type in GetLoadableTypes(assembly)
                                           where Attribute.IsDefined(type, typeof(ImplementAttribute))
                                           select type;

                foreach (Type to in types)
                {
                    ImplementAttribute custom = to.GetCustomAttributes <ImplementAttribute>().SingleOrDefault();
                    if (custom != null)
                    {
                        Container.RegisterType(custom.FromType, to, WithName.Default(to), WithLifetime.ContainerControlled(to));
                    }
                }
            }
        }