Skip to content

Commit

Permalink
#3 Initial Core.Web version
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Gerbenis committed Oct 1, 2015
1 parent 91ae313 commit 23e39b8
Show file tree
Hide file tree
Showing 14 changed files with 178 additions and 228 deletions.
7 changes: 7 additions & 0 deletions vNext/BetterModules.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{46ED6C76-1
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "BetterModules.Core.Tests", "test\BetterModules.Core.Tests\BetterModules.Core.Tests.xproj", "{00B11517-DAEB-43AA-B826-B2AA545AE166}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "BetterModules.Sample.Web.Module", "src\BetterModules.Sample.Web.Module\BetterModules.Sample.Web.Module.xproj", "{AD8BA351-BC65-4894-B42F-9AC6618A2998}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -60,6 +62,10 @@ Global
{00B11517-DAEB-43AA-B826-B2AA545AE166}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00B11517-DAEB-43AA-B826-B2AA545AE166}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00B11517-DAEB-43AA-B826-B2AA545AE166}.Release|Any CPU.Build.0 = Release|Any CPU
{AD8BA351-BC65-4894-B42F-9AC6618A2998}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AD8BA351-BC65-4894-B42F-9AC6618A2998}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AD8BA351-BC65-4894-B42F-9AC6618A2998}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AD8BA351-BC65-4894-B42F-9AC6618A2998}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -73,5 +79,6 @@ Global
{39A05B8E-5254-4E78-A03B-2014CDAA29A3} = {1E51A74B-E932-4933-AA00-AAF788D2A2DC}
{431BEF5E-502D-4B21-B13C-6E917EACC532} = {E97ED681-48EA-4EF1-97F2-EE7E1EEC516B}
{00B11517-DAEB-43AA-B826-B2AA545AE166} = {46ED6C76-1BCC-4177-9A67-492424080985}
{AD8BA351-BC65-4894-B42F-9AC6618A2998} = {E97ED681-48EA-4EF1-97F2-EE7E1EEC516B}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
using System;
using System.Linq;
using BetterModules.Core.DataAccess.DataContext.Migrations;
using BetterModules.Core.Modules.Registration;
using BetterModules.Core.Web.Web.EmbeddedResources;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.StaticFiles;
using Microsoft.Framework.DependencyInjection;

namespace BetterModules.Core.Web.Extensions
{
public static class BetterModulesApplicationBuilderExtensions
{
public static IApplicationBuilder UseBetterModules(this IApplicationBuilder app, IHostingEnvironment env)
public static IApplicationBuilder UseBetterStaticFiles(this IApplicationBuilder app)
{
RunDatabaseMigrations(app.ApplicationServices);
if (env.IsDevelopment())
return app.UseStaticFiles(new StaticFileOptions
{
app.Use(async (context, next) =>
{
if (context.Request.Query["restart"] == "1")
{
//find a way to restart a server
return;
}
await next.Invoke();
});
}
return app;
}

private static void RunDatabaseMigrations(IServiceProvider provider)
{
var migrationRunner = provider.GetService<IMigrationRunner>();
var modulesRegistration = provider.GetService<IModulesRegistration>();

var descriptors = modulesRegistration.GetModules().Select(x => x.ModuleDescriptor).ToList();
migrationRunner.MigrateStructure(descriptors);
FileProvider = app.ApplicationServices.GetService<IEmbeddedResourceProvider>()
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,56 @@
using BetterModules.Core.Extensions;
using System;
using BetterModules.Core.Extensions;
using BetterModules.Core.Modules.Registration;
using BetterModules.Core.Security;
using BetterModules.Core.Web.Configuration;
using BetterModules.Core.Web.Modules.Registration;
using BetterModules.Core.Web.Mvc;
using BetterModules.Core.Web.Mvc.Extensions;
using BetterModules.Core.Web.Security;
using BetterModules.Core.Web.Web;
using BetterModules.Core.Web.Web.EmbeddedResources;
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.Configuration;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;

namespace BetterModules.Core.Web.Extensions
{
public static class BetterModulesServiceCollectionExtensions
public static class BetterModulesWebServiceCollectionExtensions
{
public static IServiceCollection AddBetterModules(this IServiceCollection services, IConfiguration configuration)
public static IServiceCollection AddBetterModulesWeb(this IServiceCollection services, IConfiguration configuration)
{
services.LoadWebConfiguration(configuration);
services.ConfigureDefaultWebServices();
services.LoadWebAssemblies();

BetterModulesCoreServiceCollectionExtensions.RunDatabaseMigrations(services);

return services;
}

public static IServiceCollection AddBetterModulesWeb(this IServiceCollection services, IConfiguration configuration, Action<ILoggerFactory> configureLoggers)
{
var provider = services.BuildServiceProvider();
var loggerFactory = provider.GetService<ILoggerFactory>();

configureLoggers(loggerFactory);

return services.AddBetterModulesWeb(configuration);
}

private static void ConfigureDefaultWebServices(this IServiceCollection services)
{
services.ConfigureDefaultServices();

services.AddSingleton<IModulesRegistration, DefaultWebModulesRegistration>();
services.AddSingleton<IWebModulesRegistration, DefaultWebModulesRegistration>();

services.AddSingleton<IControllerFactory, DefaultWebControllerFactory>();

services.AddSingleton<IHttpContextAccessor, DefaultHttpContextAccessor>();
services.AddSingleton<IControllerExtensions, DefaultControllerExtensions>();
services.AddSingleton<IPrincipalProvider, DefaultWebPrincipalProvider>();
}

private static void LoadWebConfiguration(this IServiceCollection services, IConfiguration configuration)
{
services.LoadConfiguration(configuration);
services.Configure<DefaultWebConfigurationSection>(configuration);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using BetterModules.Core.Environment.Assemblies;
using BetterModules.Core.Modules.Registration;
using BetterModules.Core.Web.Mvc.Extensions;
using Microsoft.AspNet.Routing;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Logging;

Expand All @@ -12,21 +10,13 @@ namespace BetterModules.Core.Web.Modules.Registration
/// </summary>
public class DefaultWebModulesRegistration : DefaultModulesRegistration, IWebModulesRegistration
{
/// <summary>
/// The controller extensions
/// </summary>
private readonly IControllerExtensions controllerExtensions;

/// <summary>
/// Initializes a new instance of the <see cref="DefaultWebModulesRegistration" /> class.
/// </summary>
/// <param name="assemblyLoader">The assembly loader.</param>
/// <param name="controllerExtensions">The controller extensions.</param>
public DefaultWebModulesRegistration(IAssemblyLoader assemblyLoader, IControllerExtensions controllerExtensions, ILoggerFactory loggerFactory)
: base(assemblyLoader, loggerFactory)
{
this.controllerExtensions = controllerExtensions;
}
public DefaultWebModulesRegistration(IAssemblyLoader assemblyLoader, ILoggerFactory loggerFactory)
: base(assemblyLoader, loggerFactory) { }

/// <summary>
/// Finds the module by area name.
Expand Down Expand Up @@ -68,8 +58,6 @@ protected override void RegisterModuleDescriptor(ModuleRegistrationContext regis
{
var webDescriptor = (WebModuleDescriptor)webContext.ModuleDescriptor;
webDescriptor.RegisterModuleCommands(webContext, services);
webDescriptor.RegisterModuleControllers(webContext, services, controllerExtensions);
webDescriptor.RegisterCustomRoutes(webContext, services);
}

base.RegisterModuleDescriptor(registrationContext, services);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,70 +9,11 @@ namespace BetterModules.Core.Web.Modules.Registration
{
public class WebModuleRegistrationContext : ModuleRegistrationContext
{
public WebModuleRegistrationContext(WebModuleDescriptor moduleDescriptor) : base(moduleDescriptor)
{
Namespaces = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
RouteBuilder = new RouteBuilder();
}

protected ICollection<string> Namespaces { get; private set; }

internal IRouteBuilder RouteBuilder { get; private set; }
public WebModuleRegistrationContext(WebModuleDescriptor moduleDescriptor) : base(moduleDescriptor) {}

public override string GetRegistrationName()
{
return ((WebModuleDescriptor)ModuleDescriptor).AreaName.ToLowerInvariant();
}

public IRouter MapRoute(string name, string url)
{
return MapRoute(name, url, null);
}

public IRouter MapRoute(string name, string url, string[] namespaces)
{
return MapRoute(name, url, null, namespaces);
}


public IRouter MapRoute(string name, string url, object defaults)
{
return MapRoute(name, url, defaults, null);
}


public IRouter MapRoute(string name, string url, object defaults, string[] namespaces)
{
return MapRoute(name, url, defaults, null, namespaces);
}

public IRouter MapRoute(string name, string url, object defaults, object constraints)
{
return MapRoute(name, url, defaults, constraints, null);
}

public IRouter MapRoute(string name, string url, object defaults, object constraints, string[] namespaces)
{
if ((namespaces == null) && (this.Namespaces != null))
{
namespaces = Namespaces.ToArray<string>();
}
RouteBuilder.MapRoute(name, url, defaults, constraints, namespaces);
var router = RouteBuilder.Build();
route.DataTokens["area"] = ((WebModuleDescriptor)ModuleDescriptor).AreaName;
bool flag = (namespaces == null) || (namespaces.Length == 0);
route.DataTokens["UseNamespaceFallback"] = flag;
return route;
}

public void IgnoreRoute(string url)
{
Routes.Ignore(url);
}

public void IgnoreRoute(string url, object constraints)
{
Routes.Ignore(url, constraints);
}
}
}
109 changes: 40 additions & 69 deletions vNext/src/BetterModules.Core.Web/Modules/WebModuleDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using BetterModules.Core.Modules.Registration;
using BetterModules.Core.Web.Modules.Registration;
using BetterModules.Core.Web.Mvc.Extensions;
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.DependencyInjection;

namespace BetterModules.Core.Web.Modules
Expand Down Expand Up @@ -42,70 +41,50 @@ public abstract class WebModuleDescriptor : ModuleDescriptor
/// <value>
/// The name of the module area.
/// </value>
public virtual string AreaName
{
get
{
if (areaName == null)
{
areaName = ("module-" + Name).ToLowerInvariant();
}
public virtual string AreaName => areaName ?? (areaName = ("module-" + Name).ToLowerInvariant());

return areaName;
}
}
// From now on custom routes will be registered using attributes
///// <summary>
///// Registers a routes.
///// </summary>
///// <param name="context">The area registration context.</param>
///// <param name="services"></param>
//public virtual void RegisterCustomRoutes(WebModuleRegistrationContext context, IServiceCollection services)
//{
//}

/// <summary>
/// Registers a routes.
/// </summary>
/// <param name="context">The area registration context.</param>
/// <param name="services"></param>
public virtual void RegisterCustomRoutes(WebModuleRegistrationContext context, IServiceCollection services)
{
}
// Controller registration to DI container ir no longer needed
///// <summary>
///// Registers module controller types.
///// </summary>
///// <param name="registrationContext">The area registration context.</param>
///// <param name="services"></param>
///// <param name="controllerExtensions">The controller extensions.</param>
//public virtual void RegisterModuleControllers(WebModuleRegistrationContext registrationContext, IServiceCollection services, IControllerExtensions controllerExtensions)
//{
// var controllerTypes = controllerExtensions.GetControllerTypes(GetType().Assembly);

/// <summary>
/// Registers module controller types.
/// </summary>
/// <param name="registrationContext">The area registration context.</param>
/// <param name="services"></param>
/// <param name="controllerExtensions">The controller extensions.</param>
public virtual void RegisterModuleControllers(WebModuleRegistrationContext registrationContext, IServiceCollection services, IControllerExtensions controllerExtensions)
{
var controllerTypes = controllerExtensions.GetControllerTypes(GetType().Assembly);

if (controllerTypes != null)
{
var namespaces = new List<string>();
// if (controllerTypes != null)
// {
// var namespaces = new List<string>();

foreach (Type controllerType in controllerTypes)
{
string key = (AreaName + "-" + controllerType.Name).ToUpperInvariant();
if (!namespaces.Contains(controllerType.Namespace))
{
namespaces.Add(controllerType.Namespace);
}
services.AddTransient(controllerType);
// TODO: register controllers with keys and metadata
//containerBuilder
// .RegisterType(controllerType)
// .AsSelf()
// .Keyed<IController>(key)
// .WithMetadata("ControllerType", controllerType)
// .InstancePerDependency()
// .PropertiesAutowired(PropertyWiringOptions.PreserveSetValues);
}

registrationContext.MapRoute(
$"module_{AreaName}_internal_routes",
$"{AreaName}/{{controller}}/{{action}}",
new
{
area = AreaName
},
namespaces.ToArray());
}
}
// foreach (Type controllerType in controllerTypes)
// {
// string key = (AreaName + "-" + controllerType.Name).ToUpperInvariant();
// if (!namespaces.Contains(controllerType.Namespace))
// {
// namespaces.Add(controllerType.Namespace);
// }
// containerBuilder
// .RegisterType(controllerType)
// .AsSelf()
// .Keyed<IController>(key)
// .WithMetadata("ControllerType", controllerType)
// .InstancePerDependency()
// .PropertiesAutowired(PropertyWiringOptions.PreserveSetValues);
// }
// }
//}

/// <summary>
/// Registers the module command types.
Expand Down Expand Up @@ -138,14 +117,6 @@ public virtual void RegisterModuleCommands(WebModuleRegistrationContext registra
services.AddScoped(@interface, type);
}
}
// TODO: register commands
//containerBuilder
// .RegisterAssemblyTypes(assembly)
// .Where(scan => commandTypes.Any(commandType => scan.IsAssignableToGenericType(commandType)))
// .AsImplementedInterfaces()
// .AsSelf()
// .PropertiesAutowired()
// .InstancePerLifetimeScope();
}

/// <summary>
Expand Down
Loading

0 comments on commit 23e39b8

Please sign in to comment.