Esempio n. 1
0
        internal static ComponentPartDefinition EnsureIsComponentPart(this HisarAssemblyComponentsLoader lookup, string componentName)
        {
            if (lookup == null)
            {
                throw new ArgumentNullException(nameof(lookup));
            }

            if (string.IsNullOrEmpty(componentName))
            {
                throw new ArgumentNullException(nameof(componentName));
            }

            if (componentName.StartsWith(HisarAssemblyComponentsLoader.ComponentConventionBaseNamespace))
            {
                var token       = componentName.Split('.');
                var componentId = token[2];
                var name        = token.Last();

                lookup.ComponentAssemblyLookup.TryGetValue(componentId, out Assembly componentAssembly);
                if (componentAssembly == null)
                {
                    return(null);
                }

                return(new ComponentPartDefinition
                {
                    ComponentId = componentId,
                    Name = name,
                    Assembly = componentAssembly
                });
            }
            else
            {
                var token = componentName.Split('.');
                if (token.Length == 2)
                {
                    var componentId = token.First();
                    var name        = token.Last();

                    lookup.ComponentAssemblyLookup.TryGetValue(componentId, out Assembly componentAssembly);
                    if (componentAssembly == null)
                    {
                        return(null);
                    }

                    return(new ComponentPartDefinition
                    {
                        ComponentId = componentId,
                        Name = name,
                        Assembly = componentAssembly
                    });
                }
            }

            return(null);
        }
Esempio n. 2
0
        private static HisarAssemblyComponentsLoader CreateAssemblyLoader <TStartup>(IServiceCollection services,
                                                                                     IHostingEnvironment env,
                                                                                     IMvcBuilder builder) where TStartup : class
        {
            var assembly = typeof(TStartup).GetTypeInfo().Assembly;

            if (assembly.EnsureIsHosting())
            {
                var assemblyResolveCallback = assembly.GetTypes().FirstOrDefault(a => typeof(IAssemblyProviderResolveCallback).IsAssignableFrom(a));
                if (assemblyResolveCallback != null)
                {
                    services.AddAssemblyResolver(assemblyResolveCallback);
                }
            }

            var assemblyLoader = new HisarAssemblyComponentsLoader(services.BuildServiceProvider(), env);

            assemblyLoader.LoadComponents(services, builder);
            services.AddSingleton(assemblyLoader);

            return(assemblyLoader);
        }
Esempio n. 3
0
 public PropertyDefinitionFilter(HisarAssemblyComponentsLoader componentsLoader)
 {
     _componentsLoader = componentsLoader;
 }
Esempio n. 4
0
        internal static void AddHisar <TStartup>(this IServiceCollection services,
                                                 IConfiguration configuration,
                                                 IHostingEnvironment env) where TStartup : class
        {
            services.AddNetCoreStackMvc();
            services.AddSingleton <HisarMarkerService>();
            services.AddSingleton <IModelKeyGenerator, DefaultModelKeyGenerator>();

            // Singletons
            services.TryAddSingleton <IMemoryCacheProvider, InMemoryCacheProvider>();
            services.TryAddSingleton <IJsonSerializer, JsonSerializer>();
            services.TryAddSingleton <IDataProtectorProvider, DefaultDataProtectorProvider>();
            services.TryAddSingleton <IComponentTypeResolver, DefaultComponentTypeResolver>();
            services.TryAddSingleton <IAssemblyFileProviderFactory, DefaultAssemblyFileProviderFactory>();
            services.TryAddSingleton <IAssemblyProviderResolveCallback, DefaultIAssemblyProviderCompilationCallback>();

            // Custom view component helper
            services.AddTransient <IViewComponentHelper, HisarDefaultViewComponentHelper>();

            // Per request services
            services.TryAddScoped <IUrlGeneratorHelper, UrlGeneratorHelper>();
            services.TryAddScoped <IMenuItemsRenderer, DefaultMenuItemsRenderer>();
            services.TryAddScoped <IUsernamePasswordValidator, DefaultUsernamePasswordValidator>();
            services.TryAddScoped <IUserRegistration, DefaultUserRegistration>();

            // New instances
            services.TryAddTransient <IHisarExceptionFilter, DefaultHisarExceptionFilter>();
            services.TryAddTransient <IWebCliProxyEventHandler, DefaultWebCliProxyEventHandler>();

            var componentHelper = CreateComponentHelper <TStartup>(services);
            var assembly        = typeof(TStartup).GetTypeInfo().Assembly;

            services.TryAddScoped <IMenuItemsBuilder, DefaultMenuItemsBuilder <TStartup> >();

            bool        isComponent     = componentHelper.IsExternalComponent;
            bool        isCoreComponent = componentHelper.IsCoreComponent;
            IMvcBuilder builder         = null;

            if (isComponent)
            {
                builder = services.AddMvc(options =>
                {
                    options.Filters.Add(new HisarExceptionFilter());
                    if (isCoreComponent)
                    {
                        options.Conventions.Add(new NameSpaceRoutingConvention(componentHelper));
                    }
                });
            }
            else
            {
                services.AddSingleton <IViewComponentSelector, HostingViewComponentSelector>();

                builder = services.AddMvc(options => {
                    options.Filters.Add(new HisarExceptionFilter());
                    options.Conventions.Add(new NameSpaceRoutingConvention(componentHelper));
                });
            }

            builder.ConfigureApplicationPartManager(manager => manager.ApplicationParts.Clear())
            .AddApplicationPart(assembly);

            builder.AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            });

            services.AddSingleton(componentHelper as RunningComponentHelper);

            services.AddMenuBuilders <TStartup>();

            HisarAssemblyComponentsLoader assemblyLoader = null;

            if (isComponent)
            {
                if (isCoreComponent)
                {
                    assemblyLoader = CreateAssemblyLoader <TStartup>(services, env, builder);
                }

                var defaultLayoutFileProvider = new DefaultProxyFileLocator();
                services.TryAddSingleton <IDefaultProxyFileLocator>(_ => defaultLayoutFileProvider);
                builder.AddRazorOptions(options =>
                {
                    options.FileProviders.Add(new InMemoryCliFileProvider(defaultLayoutFileProvider));

                    // component formats
                    options.AreaViewLocationFormats.Add("/Views/{1}/{0}.cshtml");
                    options.ViewLocationFormats.Add("/Views/{1}/{0}.cshtml");
                });
            }
            else
            {
                assemblyLoader = CreateAssemblyLoader <TStartup>(services, env, builder);
                services.AddSingleton <IPropertyDefinitionFilter>(new PropertyDefinitionFilter(assemblyLoader));

                builder.AddRazorOptions(options =>
                {
                    options.FileProviders.Add(new HisarEmbededFileProvider(assemblyLoader.ComponentAssemblyLookup));
                });
            }

            services.AddSingleton(_ => services);
        }
 /// <summary>
 /// Creates a new <see cref="HostingViewComponentSelector"/>.
 /// </summary>
 /// <param name="descriptorProvider">The <see cref="IViewComponentDescriptorCollectionProvider"/>.</param>
 /// <param name="assemblyLoader">The <see cref="HisarAssemblyComponentsLoader"/>.</param>
 public HostingViewComponentSelector(IViewComponentDescriptorCollectionProvider descriptorProvider,
                                     HisarAssemblyComponentsLoader assemblyLoader)
 {
     _descriptorProvider = descriptorProvider;
     _assemblyLoader     = assemblyLoader;
 }