public static IServiceProvider UseStartup(IApplicationStartup applicationStartup, ILogger logger) { logger.LogInformation($@"Using startup: {applicationStartup.GetType().FullName}"); // Configuration. var configurationBuilder = new ConfigurationBuilder(); applicationStartup.ConfigureConfiguration(configurationBuilder); var configuration = configurationBuilder.Build(logger); // Services. var services = new ServiceCollection(); services.AddConfiguration(configuration, logger); applicationStartup.ConfigureServices(services); // Service provider. var serviceProvider = services.BuildServiceProvider(logger); applicationStartup.Configure(serviceProvider); // Tests. if (applicationStartup.RunTests) { applicationStartup.TestServices(serviceProvider); } return(serviceProvider); }
void Startups.ITypeProcessor.Processing(IApplicationStartup application, Type type) { if (type == null || type.IsAbstract || !type.IsClass || type.IsInterface || type.IsGenericTypeDefinition) { return; } if (!ObjectExtension.IsAssignableToType(eventType, type)) { return; } /*分析当前handlerType*/ var handlerAttributes = type.GetCustomAttributes(true); if (handlerAttributes == null && handlerAttributes.Length == 0) { return; } var list = new List <Attribute>(handlerAttributes.Length); foreach (var attr in handlerAttributes) { list.Add((Attribute)attr); } EventBehaviorStorager.Default.Add(type, list); }
/// <summary> /// /// </summary> /// <param name="application"></param> /// <param name="type"></param> public void Processing(IApplicationStartup application, Type type) { if (type == null) { return; } var attributes = type.GetCustomAttributes(typeof(ValidatorAttribute), false); if (attributes != null && attributes.Length > 0) { foreach (ValidatorAttribute attribute in attributes) { if (attribute.ValidatorType.IsAssignableFromType(typeof(IAmValidator)) == true) { all[type] = new Tuple <ValidatorAttribute, Func <IValidator> >(attribute, () => new MyValidator()); return; } if (attribute.ValidatorType.IsAssignableFromType(typeof(IValidator)) == false) { continue; } if (attribute.ValidatorType.IsValueType) { var emit = Never.Reflection.EasyEmitBuilder <Func <IValidator> > .NewDynamicMethod(); var loal = emit.DeclareLocal(attribute.ValidatorType); emit.LoadLocalAddress(loal); emit.InitializeObject(attribute.ValidatorType); emit.LoadLocal(loal); emit.Box(attribute.ValidatorType); emit.CastClass(typeof(IValidator)); emit.Return(); var @delegate = emit.CreateDelegate(); all[type] = new Tuple <ValidatorAttribute, Func <IValidator> >(attribute, @delegate); //all[type] = new Tuple<ValidatorAttribute, Func<IValidator>>(attribute, () => System.Activator.CreateInstance(attribute.ValidatorType) as IValidator); return; } var ctors = attribute.ValidatorType.GetConstructors(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); var ctor = ctors.FirstOrDefault(t => t.GetParameters().Length == 0); if (ctor != null) { var emit = Never.Reflection.EasyEmitBuilder <Func <IValidator> > .NewDynamicMethod(); emit.NewObject(ctor); emit.CastClass(typeof(IValidator)); emit.Return(); var @delegate = emit.CreateDelegate(); all[type] = new Tuple <ValidatorAttribute, Func <IValidator> >(attribute, @delegate); //all[type] = new Tuple<ValidatorAttribute, Func<IValidator>>(attribute, () => System.Activator.CreateInstance(attribute.ValidatorType) as IValidator); return; } throw new Exception($"{((ValidatorAttribute)attributes.FirstOrDefault()).ValidatorType} must has no parameters on ctor"); } } }
/// <summary> /// /// </summary> /// <param name="application"></param> /// <param name="type"></param> public void Processing(IApplicationStartup application, Type type) { if (type == null) { return; } var attributes = type.GetCustomAttributes(typeof(ValidatorAttribute), false); if (attributes != null && attributes.Length > 0) { foreach (ValidatorAttribute attribute in attributes) { var ctors = attribute.ValidatorType.GetConstructors(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance); if (ctors.Any(t => t.GetParameters().Length == 0)) { all[type] = attribute; return; } } throw new Exception($"{((ValidatorAttribute)attributes.FirstOrDefault()).ValidatorType} must has no parameters on ctor"); } return; }
/// <summary> /// Initializes a new instance of the <see cref="StartupContext"/> class. /// </summary> /// <param name="applicationStartup">程序宿主环境配置.</param> public StartupContext(IApplicationStartup applicationStartup) { this.ApplicationStartup = applicationStartup ?? EmptyApplicationStartup.Only; this.Items = (applicationStartup ?? EmptyApplicationStartup.Only).Items; if (this.Items == null) { this.Items = new Dictionary <string, object>(20); } }
void Startups.ITypeProcessor.Processing(IApplicationStartup application, Type type) { if (type == null || type.IsAbstract || !type.IsClass || type.IsInterface || type.IsGenericTypeDefinition) { return; } if (!ObjectExtension.IsAssignableToType(cmdHandlerType, type)) { return; } /*分析当前handlerType*/ var handlerAttributes = type.GetCustomAttributes(true); var list = new List <Attribute>(); foreach (var attr in handlerAttributes) { list.Add((Attribute)attr); } HandlerBehaviorStorager.Default.Add(type, list); /*分析excute方法块*/ var methods = type.GetMethods(bindingFlags); foreach (var method in methods) { if (method.Name != defineEvent) { continue; } var paras = method.GetParameters(); if (paras.Length != 2) { continue; } if (commandContextTye.IsAssignableFrom(paras[0].ParameterType)) { var objects = method.GetCustomAttributes(true); if (objects == null) { return; } var attributes = new List <Attribute>(); foreach (var attr in objects) { attributes.Add((Attribute)attr); } HandlerBehaviorStorager.Default.Add(type, paras[1].ParameterType, attributes); } } }
/// <summary> /// /// </summary> /// <param name="application"></param> /// <param name="type"></param> public void Processing(IApplicationStartup application, Type type) { if (type == null || type.IsAbstract || !type.IsClass || type.IsInterface || type.IsGenericTypeDefinition || type.IsValueType) { return; } if (!ObjectExtension.IsAssignableToType(typeof(IWorkStep), type)) { return; } TemplateWorkStep.AddStep(type); application.ServiceRegister.RegisterType(type, type, string.Empty, IoC.ComponentLifeStyle.Transient); }
void Startups.ITypeProcessor.Processing(IApplicationStartup application, Type type) { if (type == null || type.IsAbstract || !type.IsClass || type.IsInterface || type.IsGenericTypeDefinition) { return; } if (!ObjectExtension.IsAssignableToType(commandType, type)) { return; } /*并行命令处理*/ if (ObjectExtension.IsAssignableToType(serialeCommandType, type)) { Database.Create(type); } /*分析当前handlerType*/ var handlerAttributes = type.GetCustomAttributes(true); if (handlerAttributes == null) { return; } var list = new List <Attribute>(); foreach (var attr in handlerAttributes) { list.Add((Attribute)attr); } CommandBehaviorStorager.Default.Add(type, list); foreach (var attr in handlerAttributes) { var v = attr as ValidatorAttribute; if (v == null) { continue; } application.ServiceRegister.RegisterType(v.ValidatorType, v.ValidatorType, string.Empty, IoC.ComponentLifeStyle.Transient); } }
/// <summary> /// 处理 /// </summary> /// <param name="application">启动宿主对象</param> /// <param name="type">对象类型</param> void Startups.ITypeProcessor.Processing(IApplicationStartup application, Type type) { if (type == null || type.IsAbstract || !type.IsClass || type.IsInterface || type.IsGenericTypeDefinition) { return; } if (!eventType.IsAssignableFrom(type)) { return; } if (type.GetCustomAttributes(attributeType, false).Length > 0) { return; } throw new InvalidException("事件{0}没有配置上EventDomainAttribute特性", type.FullName); }
/// <summary> /// /// </summary> /// <param name="application"></param> /// <param name="type"></param> public void Processing(IApplicationStartup application, Type type) { if (type == null || type.IsAbstract || !type.IsClass || type.IsInterface || type.IsGenericTypeDefinition || type.IsValueType) { return; } if (!ObjectExtension.IsAssignableToType(commandType, type)) { return; } if (exists.Contains(type)) { return; } var attributes = type.GetCustomAttributes(typeof(Never.Attributes.IgnoreAnalyseAttribute), false); if (attributes != null && attributes.Length > 0) { return; } /*构造函数查询*/ if (type.GetConstructor(Type.EmptyTypes) != null) { return; } var ctors = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); foreach (var c in ctors) { if (c.GetParameters().Length == 0) { return; } } throw new ArgumentNullException(string.Format("type command Type {0} must has no-paramater ctor", type.FullName)); }
/// <summary> /// 处理 /// </summary> /// <param name="application">启动宿主对象</param> /// <param name="type">对象类型</param> void Startups.ITypeProcessor.Processing(IApplicationStartup application, Type type) { if (type == null || type.IsAbstract || !type.IsClass || type.IsInterface || type.IsGenericTypeDefinition) { return; } if (!rootType.IsAssignableFrom(type)) { return; } var events = type.GetMethods(bindingFlags); foreach (var e in events) { if (e.Name != defineEvent) { continue; } var paras = e.GetParameters(); if (paras.Length != 1) { continue; } if (!eventType.IsAssignableFrom(paras[0].ParameterType)) { continue; } if (ObjectExtension.IsAssignableToType(typeof(IHandle <>).MakeGenericType(paras[0].ParameterType), type)) { continue; } throw new Exception(string.Format("聚合根{0}找到Handle<{1}>方法,但它没有实现IHandle<{1}>接口", type.FullName, paras[0].ParameterType.Name)); } }
public MainWindow( IWindowManager windowManager, IApplicationStartup applicationDebug, IUpdater updater, MainWindowViewModel vm) { InitializeComponent(); windowManager.RootWindow = this; DataContext = context = vm; context.OnControlChanged = control => { content.Children.Clear(); content.Children.Add(control); }; context.SaveKind = ContentType.Unload; if (!string.IsNullOrEmpty(applicationDebug.StartupFileName)) { context.Open(applicationDebug.StartupFileName); } this.updater = updater; }
/// <summary> /// /// </summary> /// <param name="application"></param> /// <param name="type"></param> public void Processing(IApplicationStartup application, Type type) { if (type == null || type.IsAbstract || !type.IsClass || type.IsInterface || type.IsGenericTypeDefinition || type.IsValueType) { return; } if (!ObjectExtension.IsAssignableToType(commandHandlerType, type)) { return; } if (exists.Contains(type)) { return; } /*构造函数查询*/ var ctorTemp = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); if (ctorTemp == null || ctorTemp.Length == 0) { exists.Add(type); return; } /*获取第一个,再排序找到第一个*/ var ctor = ctorTemp[0]; if (ctorTemp.Length > 1) { /*这里有选择规则,因为注入的时候是可以带参数的,所以如果带参数的个数为0,则用构造函数参数个数去排序,如果 * 带参数的个数不为0,则可以先找到有这些参数的构造,然后再排序(只是这种情况可能在开发过程中由于修改了构造而没有 * 取消注册过正中带参数的行为,这个时候可能会得到不恰当的构造函数,但是一个对象如果存在多个不同参数的构造方法, * 这个应该是不允许的,因此大多数甚者全部都只有一个构造函数)*/ for (var i = 1; i < ctorTemp.Length; i++) { if (ctorTemp[i].GetParameters().Length <= ctor.GetParameters().Length) { ctor = ctorTemp[i]; } } } if (ctor.GetParameters().Length == 0) { exists.Add(type); return; } var ctorParameters = ctor.GetParameters(); foreach (var parameter in ctorParameters) { if (parameter.ParameterType.IsValueType) { continue; } if (application.ServiceRegister.Contain(parameter.ParameterType)) { exists.Add(type); return; } /*说明是不是泛型类型,是泛型参数*/ if (parameter.ParameterType.IsGenericType) { if (application.ServiceRegister.Contain(parameter.ParameterType.GetGenericTypeDefinition())) { exists.Add(type); return; } } throw new ArgumentNullException(string.Format("The type {0} need the paramater type {1} on ctor", type.FullName, parameter.ParameterType.FullName)); } }
public void ConfigureServices(IServiceCollection services) { _startup = services.BuildServiceProvider().GetRequiredService <IApplicationStartup>(); _startup.ConfigureServices(services); }
public Startup(IConfiguration configuration, IApplicationStartup startup) { Configuration = configuration; _startup = startup; }