public static void StartTaskManager() { var taskManager = ActiveEngine.Resolve <ITaskManager>(); var tasks = TypeFinder.ClassesOfType <ITask>(); taskManager?.Start(tasks.ToArray()); }
protected virtual void InitTypeAdapter() { //find mapper configurations provided by other assemblies var mapperConfigurations = TypeFinder.ClassesOfType <IOrderedMapperProfile>(); //create and sort instances of mapper configurations var instances = mapperConfigurations .Select(mapperConfiguration => (IOrderedMapperProfile)Activator.CreateInstance(mapperConfiguration)) .OrderBy(mapperConfiguration => mapperConfiguration.Order); //create AutoMapper configuration var config = new MapperConfiguration(cfg => { foreach (var instance in instances) { cfg.AddProfile(instance.GetType()); } }); //register var typeAdapterFactory = new AutoMappingTypeAdapterFactory(); typeAdapterFactory.Init(config); TypeAdapterFactory = typeAdapterFactory; }
protected virtual void InitServiceProvider(IServiceCollection services) { var containerBuilder = new ContainerBuilder(); //register IAppRuntime containerBuilder.RegisterInstance(this).As <IAppRuntime>().SingleInstance(); //register type finder containerBuilder.RegisterInstance(TypeFinder).As <ITypeFinder>().SingleInstance(); //populate Autofac container builder with the set of registered service descriptors containerBuilder.Populate(services); //find dependency registrars provided by other assemblies var dependencyRegistrars = TypeFinder.ClassesOfType <IDependencyRegistrar>(); //create and sort instances of dependency registrars var instances = dependencyRegistrars .Select(dependencyRegistrar => (IDependencyRegistrar)Activator.CreateInstance(dependencyRegistrar)) .OrderBy(dependencyRegistrar => dependencyRegistrar.Order); //register all provided dependencies foreach (var dependencyRegistrar in instances) { dependencyRegistrar.Register(containerBuilder); } //create service provider ServiceProvider = new DependencyInjection.ServiceProvider(containerBuilder.Build()); }
private void SetupDependencies(bool testMode = false) { //first the self IocContainer.Register <IAppEngine, mobSocialEngine>(Reuse.Singleton); //now the other dependencies by other plugins or system var dependencies = TypeFinder.ClassesOfType <IDependencyRegistrar>(); //create instances for them var dependencyInstances = dependencies.Select(dependency => (IDependencyRegistrar)Activator.CreateInstance(dependency)).ToList(); //reorder according to priority dependencyInstances = dependencyInstances.OrderBy(x => x.Priority).ToList(); foreach (var di in dependencyInstances) { //register individual instances in that order di.RegisterDependencies(IocContainer); } //and it's resolver if (!testMode) { IocContainer.WithMvc(); } }
public string GetSitemapXml() { var xmlDoc = new XmlDocument(); //root node AppendRootNode(xmlDoc); var sitemapProviders = TypeFinder.ClassesOfType <ISitemapProvider>() .Select(x => (ISitemapProvider)Activator.CreateInstance(x)); foreach (var provider in sitemapProviders) { try { var urls = provider.GetUrls(); foreach (var url in urls) { if (!url.IsNullEmptyOrWhiteSpace()) { AppendUrlNode(xmlDoc, url); } } } catch { //continue; } } #if DEBUG return(FormatXml(xmlDoc.OuterXml)); #else return(xmlDoc.OuterXml); #endif }
public void RegisterDependencies(Container container) { //http context container.RegisterInstance <HttpContextBase>(new HttpContextWrapper(HttpContext.Current) as HttpContextBase, new SingletonReuse()); //cache provider container.Register <ICacheProvider, HttpCacheProvider>(reuse: Reuse.Singleton); // settings register for access across app container.Register <IDatabaseSettings>(made: Made.Of(() => new DatabaseSettings()), reuse: Reuse.Singleton); //data provider : TODO: Use settings to determine the support for other providers container.Register <IDatabaseProvider>(made: Made.Of(() => new SqlServerDatabaseProvider()), reuse: Reuse.Singleton); //database context container.Register <IDatabaseContext>(made: Made.Of(() => DatabaseContextManager.GetDatabaseContext()), reuse: Reuse.Singleton); //and respositories container.Register(typeof(IDataRepository <>), typeof(EntityRepository <>)); var asm = AssemblyLoader.LoadBinDirectoryAssemblies(); //services //to register services, we need to get all types from services assembly and register each of them; var serviceAssembly = asm.First(x => x.FullName.Contains("mobSocial.Services")); var serviceTypes = serviceAssembly.GetTypes(). Where(type => type.IsPublic && // get public types !type.IsAbstract && // which are not interfaces nor abstract type.GetInterfaces().Length != 0); // which implementing some interface(s) container.RegisterMany(serviceTypes, reuse: Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Replace); //we need a trasient reporter service rather than singleton container.Register <IVerboseReporterService, VerboseReporterService>(reuse: Reuse.InResolutionScope, ifAlreadyRegistered: IfAlreadyRegistered.Replace); //settings var allSettingTypes = TypeFinder.ClassesOfType <ISettingGroup>(); foreach (var settingType in allSettingTypes) { var type = settingType; container.RegisterDelegate(type, resolver => { var instance = (ISettingGroup)Activator.CreateInstance(type); resolver.Resolve <ISettingService>().LoadSettings(instance); return(instance); }, reuse: Reuse.Singleton); } //and ofcourse the page generator container.Register <IPageGenerator, PageGenerator>(reuse: Reuse.Singleton); //event publishers and consumers container.Register <IEventPublisherService, EventPublisherService>(reuse: Reuse.Singleton); //all consumers which are not interfaces container.RegisterMany(new[] { typeof(IEventConsumer <>) }, serviceTypeCondition: type => !type.IsInterface); }
/// <summary> /// Loads the system plugins compiled into the assembly /// </summary> public static void LoadAssemblySystemPlugins() { var allTypes = TypeFinder.ClassesOfType <IPlugin>(); foreach (var type in allTypes) { var instance = (IPlugin)Activator.CreateInstance(type); Plugins.Add(instance.PluginInfo); } }
public void All_Entities_Versioned_Succeeds() { //first get all the entities var classTypes = TypeFinder.ClassesOfType <FoundationEntity>(); foreach (var c in classTypes) { var entitySetType = typeof(EntitySet <>).MakeGenericType(c); var methodInfo = entitySetType.GetMethod("Count"); methodInfo.Invoke(null, null); } }
public static IList <PluginInfo> GetAvailablePlugins(bool withWidgets = false) { if (_loadedPlugins != null && _loadedPlugins.All(x => x.Widgets == null) && withWidgets) { foreach (var pluginInfo in _loadedPlugins) { var widgetTypes = TypeFinder.ClassesOfType <IWidget>(pluginInfo.Assembly); var widgets = widgetTypes.Where(x => x != null).Select(x => (IWidget)DependencyResolver.ResolveOptional(x)).ToList(); pluginInfo.Widgets = widgets; } } return(_loadedPlugins); }
private void RunStartupTasks() { var startupTasks = TypeFinder.ClassesOfType <IStartupTask>(); var tasks = startupTasks.Select(startupTask => (IStartupTask)Activator.CreateInstance(startupTask)).ToList(); //reorder according to prioiryt tasks = tasks.OrderBy(x => x.Priority).ToList(); foreach (var task in tasks) { task.Run(); } ; }
static InterceptorService() { //init interceptors Interceptors = new ConcurrentDictionary <string, IList <InterceptorAction> >(); var interceptorTasks = TypeFinder.ClassesOfType <IInterceptor>(); var tasks = interceptorTasks.Select(iTask => (IInterceptor)DependencyResolver.Resolve(iTask)).ToList(); var service = DependencyResolver.Resolve <IInterceptorService>(); foreach (var task in tasks) { task.SetupInterceptors(service); } }
private void SetupPictureSizes(bool testMode = false) { PictureSizes = new List <PictureSize>(); if (testMode) { return; } var allPictureSizeRegistrars = TypeFinder.ClassesOfType <IPictureSizeRegistrar>(); var allPictureSizeInstances = allPictureSizeRegistrars.Select(x => (IPictureSizeRegistrar)Activator.CreateInstance(x)); foreach (var sizeInstance in allPictureSizeInstances) { sizeInstance.RegisterPictureSize(PictureSizes); } }
public virtual void ConfigureServices(IServiceCollection services, IConfiguration configuration) { //Init Startups var startups = TypeFinder.ClassesOfType <IStartup>(); var instances = startups .Select(x => (IStartup)Activator.CreateInstance(x)) .OrderBy(x => x.Order); foreach (var item in instances) { item.ConfigureServices(services, configuration); } //Init service provider InitServiceProvider(services); }
public static void RunAllOwinConfigurations(IAppBuilder app) { //first get all the assemblies which implement IOwinStartup var owinStartupTasks = TypeFinder.ClassesOfType <IOwinStartupTask>(); var tasks = owinStartupTasks.Select(startupTask => (IOwinStartupTask)Activator.CreateInstance(startupTask)).ToList(); //reorder according to priority tasks = tasks.OrderBy(x => x.Priority).ToList(); foreach (var task in tasks) { task.Configuration(app); } }
public IList <WidgetInfo> GetAvailableWidgets() { var storeId = ApplicationEngine.CurrentStore.Id; var plugins = GetAvailablePlugins(true).Where(x => x.ActiveStoreIds.Contains(storeId)).ToList(); var widgetInfos = plugins.Where(x => x.Installed && x.ActiveStoreIds.Contains(storeId)).SelectMany(x => { return(x.Widgets.Select(y => new WidgetInfo() { PluginName = x.Name, PluginSystemName = x.SystemName, WidgetSystemName = y.SystemName, WidgetDisplayName = y.DisplayName, WidgetZones = y.WidgetZones, ConfigurationUrl = y.ConfigurationUrl, HasConfiguration = y.HasConfiguration, SkipDragging = y.SkipDragging, WidgetInstance = y })); }).ToList(); //todo:move this to separate file //get the widgets already part of solution var solutionWidgetTypes = TypeFinder.ClassesOfType <IWidget>(restrictToSolutionAssemblies: true); var solutionWidgets = solutionWidgetTypes.Select(x => (IWidget)DependencyResolver.Resolve(x)).ToList(); foreach (var sw in solutionWidgets) { if (widgetInfos.Any(x => x.WidgetSystemName == sw.SystemName)) { continue; } widgetInfos.Add(new WidgetInfo() { PluginName = ApplicationConfig.AppName, PluginSystemName = ApplicationConfig.InbuiltWidgetPluginName, WidgetSystemName = sw.SystemName, WidgetDisplayName = sw.DisplayName, WidgetZones = sw.WidgetZones, ConfigurationUrl = sw.ConfigurationUrl, HasConfiguration = sw.HasConfiguration, SkipDragging = sw.SkipDragging, WidgetInstance = sw }); } return(widgetInfos); }
protected virtual void RunStartupTasks() { //find startup tasks provided by other assemblies var startupTasks = TypeFinder.ClassesOfType <IStartupTask>(); //create and sort instances of startup tasks //we startup this interface even for not installed plugins. //otherwise, DbContext initializers won't run and a plugin installation won't work var instances = startupTasks .Select(startupTask => (IStartupTask)Activator.CreateInstance(startupTask)) .OrderBy(startupTask => startupTask.Order); //execute tasks foreach (var task in instances) { task.Execute(); } }
private void InstallSystemPlugins() { var allPlugins = TypeFinder.ClassesOfType <IPlugin>(); var systemPlugins = allPlugins.Where(x => (x as IPlugin).IsSystemPlugin) .Select(plugin => (IPlugin)Activator.CreateInstance(plugin)) .ToList(); //run the install method foreach (var plugin in systemPlugins) { if (!PluginEngine.IsInstalled(plugin.PluginInfo)) { plugin.Install(); } } }
public void MapAllRoutes(RouteCollection routes) { //we get all the instances of type IRouteMap var routeMaps = TypeFinder.ClassesOfType <IRouteMap>(); foreach (var map in routeMaps) { //create instance of each map var mapInstance = Activator.CreateInstance(map) as IRouteMap; if (mapInstance == null) { throw new mobSocialException(string.Format( "Couldn't create instance of type {0} in namespace {1}", map.Name, map.Namespace)); } //and call the registration mapInstance.MapRoutes(routes); } }
public static void SetupPictureSizes(bool testMode = false) { if (PictureSizes != null && PictureSizes.Count > 0) { return; //already registered } PictureSizes = PictureSizes ?? new List <PictureSize>(); if (testMode) { return; } var allPictureSizeRegistrars = TypeFinder.ClassesOfType <IPictureSizeRegistrar>(); var allPictureSizeInstances = allPictureSizeRegistrars.Select(x => (IPictureSizeRegistrar)Activator.CreateInstance(x)); foreach (var sizeInstance in allPictureSizeInstances) { sizeInstance.RegisterPictureSize(PictureSizes); } }
public void RegisterDependencies(IRegistrator registrar) { // settings register for access across app registrar.Register <IDatabaseSettings, DatabaseSettings>(reuse: Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Keep); //caching registrar.Register <ICacheProvider, MemoryCacheProvider>(reuse: Reuse.Singleton); registrar.Register <ICacheAccountant, CacheAccountant>(reuse: Reuse.Transient); //events registrar.Register <IEventPublisherService, EventPublisherService>(reuse: Reuse.Singleton); //file access registrar.Register <ILocalFileProvider, LocalFileProvider>(reuse: Reuse.Singleton); //localizer registrar.Register <ILocalizer, Localizer>(reuse: Reuse.ScopedOrSingleton); //view engine & friends registrar.Register <IViewAccountant, ViewAccountant>(reuse: Reuse.Singleton); registrar.Register <IAppViewEngine, DefaultAppViewEngine>(reuse: Reuse.Singleton); //media registrar.Register <IImageProcessor, ImageProcessor>(reuse: Reuse.Singleton); registrar.Register <IMediaAccountant, MediaAccountant>(reuse: Reuse.Singleton); //plugin loader registrar.Register <IPluginAccountant, PluginAccountant>(reuse: Reuse.ScopedOrSingleton); //model mapper registrar.Register <IModelMapper, ModelMapper>(reuse: Reuse.Singleton); //routetemplate parser registrar.Register <IRouteTemplateParser, RouteTemplateParser>(reuse: Reuse.Singleton); registrar.Register <IDynamicRouteProvider, DynamicRouteProvider>(reuse: Reuse.ScopedOrSingleton); //themes registrar.Register <IThemeProvider, ThemeProvider>(reuse: Reuse.Singleton); //view compoenent registrar.Register <IViewComponentManager, ViewComponentManager>(reuse: Reuse.ScopedOrSingleton); //search query registrar.Register <ISearchQueryParserService, SearchQueryParserService>(reuse: Reuse.Scoped); //html processor registrar.Register <IHtmlProcessor, HtmlProcessor>(reuse: Reuse.Singleton); //email sender registrar.Register <IEmailSender, EmailSender>(reuse: Reuse.Transient); //bundler registrar.Register <IBundleService, BundleService>(reuse: Reuse.Transient); //minifier registrar.Register <IMinifier, Minifier>(reuse: Reuse.Transient); //interceptor registrar.Register <IInterceptorService, InterceptorService>(reuse: Reuse.Singleton); //conection accountant registrar.Register <IConnectionAccountant, ConnectionAccountant>(Reuse.Transient); var asm = AssemblyLoader.GetAppDomainAssemblies(); var allTypes = asm.Where(x => !x.IsDynamic).SelectMany(x => { try { return(x.GetTypes()); } catch (ReflectionTypeLoadException) { return(new Type[0]); } }) .Where(x => x.IsPublic && !x.IsAbstract).ToList(); //find all the model factories var allModelFactories = allTypes .Where(type => type.GetInterfaces() .Any(x => x.IsAssignableTo(typeof(IModelFactory))));// which implementing some interface(s) //all consumers which are not interfaces registrar.RegisterMany(allModelFactories); //capability providers var allCapabilityProviderTypes = allTypes .Where(type => type.GetInterfaces() .Any(x => x.IsAssignableTo(typeof(ICapabilityProvider))));// which implementing some interface(s) //all providers which are not interfaces registrar.RegisterMany(allCapabilityProviderTypes); //tasks var allTaskTypes = allTypes .Where(type => type.GetInterfaces() .Any(x => x.IsAssignableTo(typeof(ITask))));// which implementing some interface(s) //all providers which are not interfaces registrar.RegisterMany <ITask>(allTaskTypes, type => type.FullName); //interceptors var allInterceptorTypes = allTypes .Where(type => type.GetInterfaces() .Any(x => x.IsAssignableTo(typeof(IInterceptor))));// which implementing some interface(s) //all providers which are not interfaces registrar.RegisterMany <IInterceptor>(allInterceptorTypes, type => type.FullName); //currency providers var allCurrencyProviders = allTypes .Where(type => type.IsPublic && // get public types type.GetInterfaces() .Any(x => x.IsAssignableTo(typeof(ICurrencyRateProvider)))); // which implementing some interface(s) //all providers which are not interfaces registrar.RegisterMany <ICurrencyRateProvider>(allCurrencyProviders, type => type.FullName); //find all the event captures var allEventCaptures = allTypes .Where(type => type.GetInterfaces() .Any(x => x.IsAssignableTo(typeof(IEventCapture))));// which implementing some interface(s) //registrar.RegisterMany<IEventCapture>(); //all consumers which are not interfaces registrar.RegisterMany <IEventCapture>(allEventCaptures, type => type.FullName); //services //to register services, we need to get all types from services assembly and register each of them; var serviceAssembly = asm.First(x => x.FullName.Contains("EvenCart.Services,")); var serviceTypes = serviceAssembly.GetTypes(). Where(type => type.IsPublic && // get public types !type.IsAbstract && // which are not interfaces nor abstract type.GetInterfaces().Length != 0); // which implementing some interface(s) registrar.RegisterMany(serviceTypes, Reuse.Transient); registrar.Register <IFormatterService, FormatterService>(Reuse.Singleton, ifAlreadyRegistered: IfAlreadyRegistered.Replace); //find all event consumer types var allConsumerTypes = allTypes .Where(type => type.GetInterfaces() .Any(x => x.IsAssignableTo(typeof(IFoundationEvent))));// which implementing some interface(s) //all consumers which are not interfaces registrar.RegisterMany(allConsumerTypes); //components //find all event consumer types var allComponents = allTypes .Where(type => type.IsClass && type.IsAssignableTo(typeof(FoundationComponent)));// which implementing some interface(s) registrar.RegisterMany(allComponents, Reuse.Transient); //settings var allSettingTypes = TypeFinder.ClassesOfType <ISettingGroup>(); foreach (var settingType in allSettingTypes) { var type = settingType; registrar.RegisterDelegate(type, resolver => { var storeId = ApplicationEngine.CurrentStore?.Id ?? 0; return(resolver.Resolve <ISettingService>().GetSettings(type, storeId)); }, reuse: Reuse.Transient); } registrar.Register <IAppAuthenticationService, AuthenticationService>(reuse: Reuse.Transient); var allModules = TypeFinder.ClassesOfType <IPlugin>(); foreach (var moduleType in allModules) { var type = moduleType; registrar.Register(type, reuse: Reuse.Singleton); } }
public void RegisterDependencies(IContainer container) { //http context container.RegisterInstance <HttpContextBase>(new HttpContextWrapper(HttpContext.Current) as HttpContextBase, new SingletonReuse()); //cache provider container.Register <ICacheProvider, HttpCacheProvider>(reuse: Reuse.Singleton); // settings register for access across app container.Register <IDatabaseSettings>(made: Made.Of(() => new DatabaseSettings()), reuse: Reuse.Singleton); //data provider : TODO: Use settings to determine the support for other providers container.Register <IDatabaseProvider>(made: Made.Of(() => new SqlServerDatabaseProvider()), reuse: Reuse.Singleton); //database context container.Register <IDatabaseContext>(made: Made.Of(() => DatabaseContextManager.GetDatabaseContext()), reuse: Reuse.InWebRequest); //and respositories container.Register(typeof(IDataRepository <>), typeof(EntityRepository <>), made: Made.Of(FactoryMethod.ConstructorWithResolvableArguments), reuse: Reuse.InResolutionScope); var asm = AssemblyLoader.LoadBinDirectoryAssemblies(); //services //to register services, we need to get all types from services assembly and register each of them; var serviceAssembly = asm.First(x => x.FullName.Contains("mobSocial.Services")); var serviceTypes = serviceAssembly.GetTypes(). Where(type => type.IsPublic && // get public types !type.IsAbstract && // which are not interfaces nor abstract type.GetInterfaces().Length != 0); // which implementing some interface(s) container.RegisterMany(serviceTypes, reuse: Reuse.InResolutionScope); //we need a trasient reporter service rather than singleton container.Register <IVerboseReporterService, VerboseReporterService>(reuse: Reuse.InWebRequest, ifAlreadyRegistered: IfAlreadyRegistered.Replace); //settings var allSettingTypes = TypeFinder.ClassesOfType <ISettingGroup>(); foreach (var settingType in allSettingTypes) { var type = settingType; container.RegisterDelegate(type, resolver => { var instance = (ISettingGroup)Activator.CreateInstance(type); resolver.Resolve <ISettingService>().LoadSettings(instance); return(instance); }, reuse: Reuse.Singleton); } //and ofcourse the page generator container.Register <IPageGenerator, PageGenerator>(reuse: Reuse.Singleton); //event publishers and consumers container.Register <IEventPublisherService, EventPublisherService>(reuse: Reuse.Singleton); //all consumers which are not interfaces //find all event consumer types var allConsumerTypes = asm .Where(x => x.FullName.StartsWith("mobSocial")) .SelectMany(x => { try { return(x.GetTypes()); } catch { return(new Type[0]); } }) .Where(type => type.IsPublic && // get public types type.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IEventConsumer <>)) && !type.IsAbstract); // which implementing some interface(s) //all consumers which are not interfaces container.RegisterMany(allConsumerTypes); //user id provider for SignalR container.Register <IUserIdProvider, SignalRUserIdProvider>(); //register authentication service inwebrequest container.Register <IAuthenticationService, AuthenticationService>(reuse: Reuse.InWebRequest, ifAlreadyRegistered: IfAlreadyRegistered.Replace); //overridable providers container.Register <IRoleNameProvider, RoleNameProvider>(reuse: Reuse.Singleton); }