/// <summary>
        /// 注册类型。
        /// </summary>
        public static IComponentRegistry Register(this IComponentRegistry registry,
                                                  string name, Type componentType,
                                                  Type[] services,
                                                  ComponentLifeStyle lifeStyle)
        {
            registry.Register(name, componentType, services, lifeStyle,
                                                    Enumerable.Empty<DependencyInfo>());

            return registry;
        }
        public static PartBuilder LifeStyle(this PartBuilder pb, ComponentLifeStyle lifeStyle)
        {
            switch (lifeStyle)
            {
                case ComponentLifeStyle.Singleton:
                    pb = pb.SetCreationPolicy(System.ComponentModel.Composition.CreationPolicy.Shared);
                    break;
                case ComponentLifeStyle.InRequestScope:
                case ComponentLifeStyle.InThreadScope:
                    throw new NotSupportedFeaturesException();
                case ComponentLifeStyle.Transient:
                default:
                    pb = pb.SetCreationPolicy(System.ComponentModel.Composition.CreationPolicy.NonShared);
                    break;
            }

            return pb;
        }
示例#3
0
 public void AddComponent(Type service, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     AddComponent(service, service, key, lifeStyle);
 }
示例#4
0
        public static Autofac.Builder.IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> PerLifeStyle <TLimit, TActivatorData, TRegistrationStyle>(this Autofac.Builder.IRegistrationBuilder <TLimit, TActivatorData, TRegistrationStyle> builder, ComponentLifeStyle lifeStyle)
        {
            switch (lifeStyle)
            {
            case ComponentLifeStyle.LifetimeScope:
                return(HttpContext.Current != null?builder.InstancePerHttpRequest() : builder.InstancePerLifetimeScope());

            case ComponentLifeStyle.Transient:
                return(builder.InstancePerDependency());

            case ComponentLifeStyle.Singleton:
                return(builder.SingleInstance());

            default:
                return(builder.SingleInstance());
            }
        }
示例#5
0
 public DependencyAttribute(ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     LifeStyle = lifeStyle;
 }
示例#6
0
 public void AddComponentWithParameters <TService, TImplementation>(IDictionary <string, string> properties, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     AddComponentWithParameters(typeof(TService), typeof(TImplementation), properties);
 }
示例#7
0
 public void AddComponentInstance(Type service, object instance, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     UpdateContainer(x =>
     {
         var registration = x.RegisterInstance(instance).Keyed(key, service).As(service).PerLifeStyle(lifeStyle);
     });
 }
        /// <summary>
        /// 注册中间件与接口映射关系
        /// </summary>
        /// <param name="implementationType">继承TSservice对象的具体对象</param>
        /// <param name="serviceType">服务类型</param>
        /// <param name="key">key</param>
        /// <param name="lifeStyle">生命周期</param>
        internal IRegisterRule RegisterArrayType(Type implementationType, Type serviceType, string key, ComponentLifeStyle lifeStyle)
        {
            /*开始注册*/
            var rule = new RegisterRule(implementationType, serviceType, key == null ? string.Empty : key, lifeStyle);

            this.Rules.Add(rule);
            rule.IsArray = true;
            if (lifeStyle == ComponentLifeStyle.Transient)
            {
                return(rule);
            }

            return(rule);
        }
示例#9
0
 /// <summary>
 /// Adds the component.
 /// </summary>
 /// <typeparam name="TService">The type of the service.</typeparam>
 /// <typeparam name="TImplementation">The type of the implementation.</typeparam>
 /// <param name="key">The key.</param>
 /// <param name="lifeStyle">The life style.</param>
 public virtual void AddComponent <TService, TImplementation>(string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient)
 {
     AddComponent(typeof(TService), typeof(TImplementation), key, lifeStyle);
 }
 public DependencyAttribute(ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient)
 {
     LifeStyle = lifeStyle;
 }
示例#11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StartupService"/> class.
 /// </summary>
 /// <param name="lifeStyle">生命周期</param>
 public StartupService(ComponentLifeStyle lifeStyle)
 {
     this.lifeStyle = lifeStyle;
 }
示例#12
0
 public void AddComponentLifeStyle(string key, Type classType, ComponentLifeStyle lifeStyle)
 {
     Container.AddComponentLifeStyle(key, classType, lifeStyle);
 }
示例#13
0
        /// <summary>
        /// 注入commandhanler和eventhandler
        /// </summary>
        /// <param name="startup"></param>
        /// <param name="lifeStyle"></param>
        /// <returns></returns>
        public static ApplicationStartup UseInjectingCommandHandlerEventHandler(this ApplicationStartup startup, ComponentLifeStyle lifeStyle)
        {
            if (startup.Items.ContainsKey("UseInjectCommandHandlerEventHandler"))
            {
                return(startup);
            }

            startup.RegisterStartService(new Never.Domains.StartupService(lifeStyle));
            return(startup);
        }
示例#14
0
        /// <summary>
        /// 启动ConcurrentCache支持
        /// </summary>
        /// <param name="startup">程序宿主环境配置服务</param>
        /// <param name="key">IoC容器中的key</param>
        /// <param name="lifeStyle">生命周期</param>
        /// <returns></returns>
        public static ApplicationStartup UseConcurrentCache(this ApplicationStartup startup, string key, ComponentLifeStyle lifeStyle)
        {
            if (startup.ServiceRegister == null)
            {
                return(startup);
            }

            if (startup.Items.ContainsKey("UseConcurrentCache" + key))
            {
                return(startup);
            }

            startup.ServiceRegister.RegisterType(typeof(ConcurrentCounterDictCache), typeof(ICaching), key, lifeStyle);
            startup.Items["UseConcurrentCache" + key] = "t";

            return(startup);
        }
示例#15
0
        /// <summary>
        /// 启用发布订阅模式,生命周期通常声明为单例
        /// </summary>
        /// <typeparam name="TMessageContext">事件上下文</typeparam>
        /// <param name="startup">程序宿主环境配置服务</param>
        /// <param name="lifeStyle">生命周期</param>
        /// <returns></returns>
        public static ApplicationStartup UsePublishSubscribeBus <TMessageContext>(this ApplicationStartup startup, ComponentLifeStyle lifeStyle) where TMessageContext : IMessageContext
        {
            if (startup.ServiceRegister == null)
            {
                return(startup);
            }

            if (startup.Items.ContainsKey("UsePublishSubscribeBus"))
            {
                return(startup);
            }

            /*注册发布事件*/
            startup.ServiceRegister.RegisterType(typeof(MessagePublisher), typeof(IMessagePublisher), string.Empty, ComponentLifeStyle.Singleton);
            startup.ServiceRegister.RegisterType(typeof(TMessageContext), typeof(IMessageContext), string.Empty, ComponentLifeStyle.Transient);
            startup.RegisterStartService(new Never.Messages.StartupService(lifeStyle));
            startup.Items["UsePublishSubscribeBus"] = "t";
            return(startup);
        }
示例#16
0
 /// <summary>
 /// 启用发布订阅模式,生命周期通常声明为单例
 /// </summary>
 /// <param name="startup">程序宿主环境配置服务</param>
 /// <param name="lifeStyle">生命周期</param>
 /// <returns></returns>
 public static ApplicationStartup UsePublishSubscribeBus(this ApplicationStartup startup, ComponentLifeStyle lifeStyle)
 {
     return(UsePublishSubscribeBus <DefaultMessageContext>(startup, lifeStyle));
 }
示例#17
0
 public DependencyAttribute(Type serviceType, ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     LifeStyle = lifeStyle;
     ServiceType = serviceType;
 }
 public DependencyAttribute(Type serviceType, ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient)
 {
     LifeStyle   = lifeStyle;
     ServiceType = serviceType;
 }
示例#19
0
        /// <summary>
        /// 注册中间件与接口映射关系
        /// </summary>
        /// <param name="implementationType">继承TSservice对象的具体对象</param>
        /// <param name="serviceType">服务类型</param>
        /// <param name="key">key</param>
        /// <param name="lifeStyle">生命周期</param>
        public IRegisterRule RegisterType(Type implementationType, Type serviceType, string key, ComponentLifeStyle lifeStyle)
        {
            /*注册规则*/
            Rule(implementationType, serviceType);

            /*开始注册*/
            var rule = new RegisterRule(implementationType, serviceType, key == null ? string.Empty : key, lifeStyle);

            this.Rules.Add(rule);
            if (lifeStyle == ComponentLifeStyle.Transient)
            {
                return(rule);
            }

            return(rule);
        }
        public static IBindingNamedWithOrOnSyntax <T> PerLifeStyle <T>(this IBindingWhenInNamedWithOrOnSyntax <T> binding, ComponentLifeStyle lifeStyle)
        {
            switch (lifeStyle)
            {
            case ComponentLifeStyle.Singleton:
                return(binding.InSingletonScope());

            case ComponentLifeStyle.InThreadScope:
                return(binding.InThreadScope());

            case ComponentLifeStyle.InRequestScope:
                return(binding.InRequestScope());

            case ComponentLifeStyle.Transient:
            default:
                return(binding.InTransientScope());
            }
        }
示例#21
0
 public void AddComponentInstance <TService>(object instance, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     AddComponentInstance(typeof(TService), instance, key, lifeStyle);
 }
示例#22
0
 public void AddComponent <TService>(string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient)
 {
     AddComponent <TService, TService>(key, lifeStyle);
 }
示例#23
0
 public void AddComponentInstance(object instance, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     AddComponentInstance(instance.GetType(), instance, key, lifeStyle);
 }
示例#24
0
 public void AddComponent(Type service, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient)
 {
     AddComponent(service, service, key, lifeStyle);
 }
示例#25
0
        public void AddComponentWithParameters(Type service, Type implementation, IDictionary <string, string> properties, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
        {
            UpdateContainer(x =>
            {
                var serviceTypes = new List <Type> {
                    service
                };

                var temp = x.RegisterType(implementation).As(serviceTypes.ToArray()).
                           WithParameters(properties.Select(y => new NamedParameter(y.Key, y.Value)));
                if (!string.IsNullOrEmpty(key))
                {
                    temp.Keyed(key, service);
                }
            });
        }
示例#26
0
 public void AddComponentLifeStyle(string key, Type serviceType, ComponentLifeStyle lifeStyle)
 {
     container.AddComponent(key, serviceType, serviceType);
 }
示例#27
0
 public DependencyAttribute(Type serviceType, ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     LifeStyle   = lifeStyle;
     ServiceType = serviceType;
 }
示例#28
0
 public void AddComponentLifeStyle(string key, Type serviceType, ComponentLifeStyle lifeStyle)
 {
     throw new NotImplementedException();
 }
示例#29
0
 public void AddComponent <TService>(string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     AddComponent <TService, TService>(key, lifeStyle);
 }
示例#30
0
 public virtual void AddComponent(Type service, Type implementation, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     _container.Bind(service).To(implementation).PerLifeStyle(lifeStyle).MapKey(key).ReplaceExisting(service);
 }
示例#31
0
 public void AddComponent <TService, TImplementation>(string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     AddComponent(typeof(TService), typeof(TImplementation), key, lifeStyle);
 }
示例#32
0
 public IContainer RegisterInstance <TService, TImplementation>(TImplementation instance, ComponentLifeStyle lifeStyle = ComponentLifeStyle.LifetimeScope) where TImplementation : class, TService
 {
     return(UpdateContainer(x =>
     {
         var registration = x.RegisterInstance(instance).As(typeof(TService)).PerLifeStyle(lifeStyle);
     }));
 }
示例#33
0
 public DependencyAttribute(ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton)
 {
     LifeStyle = lifeStyle;
 }
 public DependencyAttribute(ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient)
 {
     LifeStyle = lifeStyle;
 }
 public DependencyAttribute(Type serviceType, ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient)
 {
     LifeStyle = lifeStyle;
     ServiceType = serviceType;
 }
示例#36
0
        public static void RegisterConfig(this IContainerManager containerManager, XElement root)
        {
            var compEles = root.Element("components").Elements("component");

            foreach (var ele in compEles)
            {
                var serviceType          = GetTypeByFullName(ele.Attribute("type").Value);
                var interfaceType        = GetTypeByFullName(ele.Attribute("service").Value);
                ComponentLifeStyle scope = ComponentLifeStyle.Transient;
                if (ele.Attribute("scope") != null)
                {
                    switch (ele.Attribute("scope").Value.ToLower())
                    {
                    case "singleton":
                        scope = ComponentLifeStyle.Singleton;
                        break;

                    case "request":
                        scope = ComponentLifeStyle.InRequestScope;
                        break;

                    case "thread":
                        scope = ComponentLifeStyle.InThreadScope;
                        break;

                    case "transiant":
                    default:
                        scope = ComponentLifeStyle.Transient;
                        break;
                    }
                }
                string name = ele.Attribute("name") == null ? null : ele.Attribute("name").Value;

                if (ele.HasElements)
                {
                    var paraEles     = ele.Element("parameters").Elements("parameter");
                    var constructors = serviceType.GetConstructors();
                    foreach (var constructor in constructors)
                    {
                        var           parameters = constructor.GetParameters();
                        List <object> pvals      = new List <object>();
                        try
                        {
                            foreach (var para in parameters)
                            {
                                var    pele = paraEles.FirstOrDefault(e => e.Attribute("name").Value.ToLower() == para.Name.ToLower());
                                object val  = null;
                                if (pele != null)
                                {
                                    val = ConvertTo(para.ParameterType, pele.Attribute("value").Value);
                                }
                                else
                                {
                                    val = containerManager.TryResolve(para.ParameterType);
                                }
                                pvals.Add(val);
                            }

                            object instance = constructor.Invoke(pvals.ToArray());
                            containerManager.AddComponentInstance(interfaceType, instance, name);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }
                else
                {
                    containerManager.AddComponent(interfaceType, serviceType, name, scope);
                }
            }

            var moduleEles = root.Element("modules").Elements("module");

            foreach (var ele in moduleEles)
            {
                var        moduleType = Type.GetType(ele.Attribute("type").Value);
                IIocModule module     = Activator.CreateInstance(moduleType) as IIocModule;
                containerManager.RegisterModule(module);
            }
        }