using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TinaX.Const; using CatLib; using CatLib.Container; using Application = CatLib.Application; using System.Reflection; using TinaX.Internal; using TinaX.Services; namespace TinaX.Container { public class ServiceContainer : IServiceContainer { public Application CatApplication { get; private set; } private XCore m_Core; public ServiceContainer(XCore core) { m_Core = core; CatApplication = new XCatApplication(m_Core); App.That = CatApplication; CatApplication.Instance(core); } #region 构建和获取服务 public TService Get(params object[] userParams) => CatApplication.Make(userParams); public object Get(Type type, params object[] userParams) { string service_name = CatApplication.Type2Service(type); return CatApplication.Make(service_name, userParams); } public object Get(string serviceName, params object[] userParams) => CatApplication.Make(serviceName, userParams); public bool TryGet(out TService service, params object[] userParams) { if (CatApplication.IsStatic() || CatApplication.IsAlias() || CatApplication.HasBind() || CatApplication.HasInstance()) { service = CatApplication.Make(userParams); return true; } service = default; return false; } public bool TryGet(Type type, out object service, params object[] userParams) { string service_name = CatApplication.Type2Service(type); if (CatApplication.IsStatic(service_name) || CatApplication.IsAlias(service_name) || CatApplication.HasBind(service_name) || CatApplication.HasInstance(service_name)) { service = CatApplication.Make(service_name, userParams); return true; } service = default; return false; } public bool TryGet(string service_name, out object service, params object[] userParams) { if (CatApplication.IsStatic(service_name) || CatApplication.IsAlias(service_name) || CatApplication.HasBind(service_name) || CatApplication.HasInstance(service_name)) { service = CatApplication.Make(service_name, userParams); return true; } service = default; return false; } public bool TryGetBuildInService(out TBuiltInService service) where TBuiltInService : TinaX.Services.IBuiltInService { if (CatApplication.IsAlias() || CatApplication.HasBind() || CatApplication.HasInstance() || CatApplication.IsStatic()) { service = CatApplication.Make(); return true; } service = default; return false; } public bool TryGetBuildInService(Type type , out object service) { if(!type.IsAssignableFrom(typeof(TinaX.Services.IBuiltInService))) { service = default; return false; } string service_name = CatApplication.Type2Service(type); if (CatApplication.IsStatic(service_name) || CatApplication.IsAlias(service_name) || CatApplication.HasBind(service_name) || CatApplication.HasInstance(service_name)) { service = CatApplication.Make(service_name); return true; } service = default; return false; } public bool TryGetBuildInService(string service_name, out object service) { if (CatApplication.IsStatic(service_name) || CatApplication.IsAlias(service_name) || CatApplication.HasBind(service_name) || CatApplication.HasInstance(service_name)) { service = CatApplication.Make(service_name); return true; } service = default; return false; } #endregion #region 注册绑定服务 /// /// 绑定实例 /// /// /// /// public IBindData Bind() => CatApplication.Bind(); public IBindData Bind(string serviceName, Type type, bool isStatic) => CatApplication.Bind(serviceName, type, isStatic); public IBindData Bind() => CatApplication.Bind(); public bool BindIf(out IBindData bindData) => CatApplication.BindIf(out bindData); public bool BindIf(out IBindData bindData) => CatApplication.BindIf(out bindData); public bool BindIf(string serviceName, Type concreate, bool isStatic, out IBindData bindData) => CatApplication.BindIf(serviceName, concreate, isStatic, out bindData); /// /// 绑定单例 /// /// /// /// public IBindData Singleton() => CatApplication.Singleton(); public IBindData Singleton() => CatApplication.Singleton(); public bool SingletonIf(out IBindData bindData) => CatApplication.SingletonIf(out bindData); public bool SingletonIf(out IBindData bindData) => CatApplication.SingletonIf(out bindData); /// /// 实现内置服务接口 /// /// /// /// /// public IBindData BindBuiltInService() where TBuiltInService : TinaX.Services.IBuiltInService { m_Core.m_TryGetIAppDomain = false; return CatApplication.Singleton() .Alias(); } public IBindData BindBuiltInService() where TBuiltInService : TinaX.Services.IBuiltInService { m_Core.m_TryGetIAppDomain = false; return CatApplication.Singleton(); } /// /// 单例注册已生成的实例 | Singleton registration of generated instances /// /// /// /// public object Instance(object instance) => CatApplication.Instance(instance); /// /// 单例注册已生成的实例 | Singleton registration of generated instances /// /// /// /// public object Instance(string service, object instance) => CatApplication.Instance(service, instance); #endregion #region 解绑 public void Unbind() { CatApplication.Unbind(); } public void Unbind(Type type) { CatApplication.Unbind(CatApplication.Type2Service(type)); } public void Unbind(string serviceName) { CatApplication.Unbind(serviceName); } #endregion #region 注入 public void Inject(object target) { Type obj_type = target.GetType(); foreach (var field in obj_type.GetRuntimeFields()) { var attr = field.GetCustomAttribute(true); if (attr == null) continue; if (this.TryGet(field.FieldType, out var _service)) { field.SetValue(target, _service); continue; } if (attr.Nullable) continue; else throw new ServiceNotFoundException(field.FieldType); //抛异常 } foreach (var property in obj_type.GetRuntimeProperties()) { var attr = property.GetCustomAttribute(true); if (attr == null) continue; if (this.TryGet(property.PropertyType,out var _service)) { property.SetValue(target, _service); continue; } if (attr.Nullable) continue; else throw new ServiceNotFoundException(property.PropertyType); //抛异常 } } #endregion public string Type2ServiceName(Type type) => CatApplication.Type2Service(type); public string Type2ServiceName() => CatApplication.Type2Service(); } }