/// <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; }
public void AddComponent(Type service, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton) { AddComponent(service, service, key, lifeStyle); }
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()); } }
public DependencyAttribute(ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton) { LifeStyle = lifeStyle; }
public void AddComponentWithParameters <TService, TImplementation>(IDictionary <string, string> properties, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton) { AddComponentWithParameters(typeof(TService), typeof(TImplementation), properties); }
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); }
/// <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; }
/// <summary> /// Initializes a new instance of the <see cref="StartupService"/> class. /// </summary> /// <param name="lifeStyle">生命周期</param> public StartupService(ComponentLifeStyle lifeStyle) { this.lifeStyle = lifeStyle; }
public void AddComponentLifeStyle(string key, Type classType, ComponentLifeStyle lifeStyle) { Container.AddComponentLifeStyle(key, classType, lifeStyle); }
/// <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); }
/// <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); }
/// <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); }
/// <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)); }
public DependencyAttribute(Type serviceType, ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton) { LifeStyle = lifeStyle; ServiceType = serviceType; }
public DependencyAttribute(Type serviceType, ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient) { LifeStyle = lifeStyle; ServiceType = serviceType; }
/// <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()); } }
public void AddComponentInstance <TService>(object instance, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton) { AddComponentInstance(typeof(TService), instance, key, lifeStyle); }
public void AddComponent <TService>(string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient) { AddComponent <TService, TService>(key, lifeStyle); }
public void AddComponentInstance(object instance, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton) { AddComponentInstance(instance.GetType(), instance, key, lifeStyle); }
public void AddComponent(Type service, string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Transient) { AddComponent(service, service, key, lifeStyle); }
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); } }); }
public void AddComponentLifeStyle(string key, Type serviceType, ComponentLifeStyle lifeStyle) { container.AddComponent(key, serviceType, serviceType); }
public void AddComponentLifeStyle(string key, Type serviceType, ComponentLifeStyle lifeStyle) { throw new NotImplementedException(); }
public void AddComponent <TService>(string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton) { AddComponent <TService, TService>(key, lifeStyle); }
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); }
public void AddComponent <TService, TImplementation>(string key = "", ComponentLifeStyle lifeStyle = ComponentLifeStyle.Singleton) { AddComponent(typeof(TService), typeof(TImplementation), key, lifeStyle); }
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); })); }
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); } }