//处理定义在类上的扩展器 internal static void ExtenderInit(IObjectServiceContext ctx) { var attributes = ctx.CoreAttributes; if (!attributes.TryGetValue(typeof(ObjectServiceExtenderAttribute), out var list)) { return; } if (list == null || list.Count <= 0) { return; } var items = ctx.Items; var instance = ctx.ObjectService; var orderList = list.OrderByDescending(x => x.Priority); foreach (ObjectServiceExtenderAttribute attribute in orderList) { var type = attribute?.OwnerType; if (type != null) { var extender = (IObjectExtender <IObjectService>)TypeHelper.CreateObject(type, typeof(IObjectExtender <IObjectService>), false); extender?.Init(instance, items); } } }
//处理CoreAttribute private static void CoreAttributeHandle(IObjectServiceContext ctx) { var attributes = ctx.CoreAttributes; if (!attributes.TryGetValue(typeof(CoreAttributeHandlerAttribute), out var list)) { return; } var items = ctx.Items; var orderList = list.OrderByDescending(x => x.Priority); var intfName = typeof(ICoreAttributeHandler <>).FullName; foreach (var coreAttribute in orderList) { var attribute = (CoreAttributeHandlerAttribute)coreAttribute; var type = attribute?.OwnerType; if (type == null) { continue; } var interfaces = type.GetInterfaces(); if (interfaces == null || interfaces.Length <= 0) { continue; } var instance = TypeHelper.CreateObject(type, null, false); if (instance == null) { continue; } foreach (var intf in interfaces) { if (!intf.IsGenericType) { continue; } if (intf.GetGenericTypeDefinition() != typeof(ICoreAttributeHandler <>)) { continue; } var args = intf.GetGenericArguments(); var attributeType = args[0]; if (!typeof(CoreAttribute).IsAssignableFrom(attributeType)) { continue; } var method = intf.GetMethod("HandleAttributes"); if (method != null) { if (!attributes.TryGetValue(attributeType, out var list1)) { continue; } method.Invoke(instance, new object[] { ctx, list1 }); } } } }
//构建对象容器内容 private static void ObjectAttributesHandle(IObjectServiceContext ctx) { var attributes = ctx.CoreAttributes; if (!attributes.TryGetValue(typeof(ObjectAttribute), out var list)) { return; } foreach (var coreAttribute in list) { var attribute = (ObjectAttribute)coreAttribute; ObjectAttributeHandle(ctx, attribute); } }
//构建类型别名 private static void TypeAliaseAttributesHandle(IObjectServiceContext ctx) { var attributes = ctx.CoreAttributes; if (!attributes.TryGetValue(typeof(TypeAliaseAttribute), out var list)) { return; } var typeAliases = ctx.GlobalObjectNamespace.TypeAliases; foreach (var coreAttribute in list) { var attribute = (TypeAliaseAttribute)coreAttribute; if (attribute.OwnerType != null) { typeAliases.Add(attribute.Name, attribute.OwnerType); } } }
//构建IObjectBuilder对象 private static void ObjectBuilderAttributeHandle(IObjectServiceContext ctx) { var attributes = ctx.CoreAttributes; if (!attributes.TryGetValue(typeof(ObjectBuilderAttribute), out var list)) { return; } foreach (var coreAttribute in list) { var attribute = (ObjectBuilderAttribute)coreAttribute; var type = attribute?.OwnerType; if (type != null) { var builder = (IObjectBuilder)TypeHelper.CreateObject(type, typeof(IObjectBuilder), false, ctx.ObjectService); if (builder != null) { AddSingletonObjectToObjectNamespace(attribute.Name, null, builder, ctx.GlobalObjectNamespace); } } } }
public void HandleAttributes(IObjectServiceContext ctx, IList <CoreAttribute> attributes) { if (attributes == null || attributes.Count <= 0) { return; } var builder = new SettingObjectBuilder(ctx.ObjectService); foreach (SettingObjectAttribute attribute in attributes) { var items = new Hashtable { { typeof(SettingObjectAttribute), attribute } }; var type = attribute.OwnerType; IObjectContainer container = new SingletonObjectContainer(); container.Init(new ObjectDescription(type, items), builder); var objects = ctx.GlobalObjectNamespace; objects.AddObject(type, container); if (type.IsClass) { //查找此类型实现的接口 var interfaces = type.GetInterfaces(); if (interfaces.Length > 0) { foreach (var ifType in interfaces) { if (ifType.IsDefined(typeof(ServiceAttribute), true)) { objects.AddObject(ifType, container); } } } } } }
protected virtual void OnError(IObjectServiceContext ctx, Exception e) { this.Error?.Invoke(ctx, e); }
protected virtual void OnDisposing(IObjectServiceContext ctx) { this.Disposing?.Invoke(ctx); }
protected virtual void OnSetupCompleted(IObjectServiceContext ctx) { this.SetupCompleted?.Invoke(ctx); }
protected virtual void OnPreInit(IObjectServiceContext ctx) { this.PreInit?.Invoke(ctx); }
//把缓存中的日志保存 private void ObjectServiceOnDisposing(IObjectServiceContext ctx) { var logService = ctx.ObjectService.GetObject <LogService>(); logService?.OnTimerInternal(); }
//未处理的异常 private void ObjectServiceOnError(IObjectServiceContext ctx, Exception e) { ExceptionService.Publish(e); }
//处理StarterAttribute private void ObjectServiceOnSetupCompleted(IObjectServiceContext ctx) { StarterAttribute.Init(ctx.Items); }
private static void ObjectAttributeHandle(IObjectServiceContext ctx, ObjectAttribute attribute) { var objectType = attribute.OwnerType; if (objectType == null) { return; } var lifetime = attribute.Lifetime; Type containerType = null; if (!string.IsNullOrEmpty(lifetime)) { containerType = ctx.ObjectService.GetOrCreateType(lifetime); } IObjectContainer container = null; if (containerType != null) { container = (IObjectContainer)TypeHelper.CreateObject(containerType, typeof(IObjectContainer), false); } if (container == null) { container = ObjectContainer.CreateDefault(); } IObjectBuilder objectBuilder = null; var builderName = attribute.Builder; if (!string.IsNullOrEmpty(builderName)) { objectBuilder = ctx.ObjectService.GetObject <IObjectBuilder>(builderName); } if (objectBuilder == null) { objectBuilder = ctx.ObjectService.ObjectBuilder; } var description = ObjectDescription.CreateFromType(objectType); container.Init(description, objectBuilder); var name = attribute.Name; if (string.IsNullOrEmpty(name)) { name = objectType.FullName; } var objects = ctx.GlobalObjectNamespace; objects.AddObject(name, container); objects.AddObject(objectType, container); //查找此类型实现的接口 var interfaces = objectType.GetInterfaces(); if (interfaces.Length > 0) { foreach (var ifType in interfaces) { if (ifType.IsDefined(typeof(ServiceAttribute), true)) { objects.AddObject(ifType, container); } } } }
//处理定义在类上的扩展器 private static void ExtenderAttributesHandle(IObjectServiceContext ctx) { ObjectServiceExtenderAttribute.ExtenderInit(ctx); }