Skip to content

Commit

Permalink
#3 Changes to WebModulesRegistration
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukas Gerbenis committed Sep 17, 2015
1 parent 41b8d50 commit 4194707
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ protected override void RegisterModuleDescriptor(ModuleRegistrationContext regis
if (webContext != null)
{
var webDescriptor = (WebModuleDescriptor)webContext.ModuleDescriptor;
webDescriptor.RegisterModuleCommands(webContext, containerBuilder);
webDescriptor.RegisterModuleControllers(webContext, containerBuilder, controllerExtensions);
webDescriptor.RegisterCustomRoutes(webContext, containerBuilder);
webDescriptor.RegisterModuleCommands(webContext, services);
webDescriptor.RegisterModuleControllers(webContext, services, controllerExtensions);
webDescriptor.RegisterCustomRoutes(webContext, services);
}

base.RegisterModuleDescriptor(registrationContext, services);
Expand Down
79 changes: 48 additions & 31 deletions vNext/src/BetterModules.Core.Web/Modules/WebModuleDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using Autofac;
using BetterModules.Core.Extensions;
using BetterModules.Core.Modules;
using BetterModules.Core.Modules.Registration;
using BetterModules.Core.Web.Modules.Registration;
using BetterModules.Core.Web.Mvc.Commands;
using BetterModules.Core.Web.Mvc.Extensions;
using Microsoft.AspNet.Mvc;
using Microsoft.Framework.DependencyInjection;

namespace BetterModules.Core.Web.Modules
{
Expand Down Expand Up @@ -59,18 +59,18 @@ public virtual string AreaName
/// Registers a routes.
/// </summary>
/// <param name="context">The area registration context.</param>
/// <param name="containerBuilder">The container builder.</param>
public virtual void RegisterCustomRoutes(WebModuleRegistrationContext context, ContainerBuilder containerBuilder)
/// <param name="services"></param>
public virtual void RegisterCustomRoutes(WebModuleRegistrationContext context, IServiceCollection services)
{
}

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

Expand All @@ -85,33 +85,34 @@ public virtual void RegisterModuleControllers(WebModuleRegistrationContext regis
{
namespaces.Add(controllerType.Namespace);
}

containerBuilder
.RegisterType(controllerType)
.AsSelf()
.Keyed<IController>(key)
.WithMetadata("ControllerType", controllerType)
.InstancePerDependency()
.PropertiesAutowired(PropertyWiringOptions.PreserveSetValues);
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(
string.Format("module_{0}_internal_routes", AreaName),
string.Format("{0}/{{controller}}/{{action}}", AreaName),
new
{
area = AreaName
},
namespaces.ToArray());
$"module_{AreaName}_internal_routes",
$"{AreaName}/{{controller}}/{{action}}",
new
{
area = AreaName
},
namespaces.ToArray());
}
}

/// <summary>
/// Registers the module command types.
/// </summary>
/// <param name="registrationContext">The area registration context.</param>
/// <param name="containerBuilder">The container builder.</param>
public virtual void RegisterModuleCommands(WebModuleRegistrationContext registrationContext, ContainerBuilder containerBuilder)
/// <param name="services"></param>
public virtual void RegisterModuleCommands(WebModuleRegistrationContext registrationContext, IServiceCollection services)
{
Assembly assembly = GetType().Assembly;

Expand All @@ -122,14 +123,30 @@ public virtual void RegisterModuleCommands(WebModuleRegistrationContext registra
typeof(ICommandOut<>),
typeof(ICommand<,>)
};

containerBuilder
.RegisterAssemblyTypes(assembly)
.Where(scan => commandTypes.Any(commandType => scan.IsAssignableToGenericType(commandType)))
.AsImplementedInterfaces()
.AsSelf()
.PropertiesAutowired()
.InstancePerLifetimeScope();
var types = assembly
.GetExportedTypes()
.Where(type => commandTypes.Any(type.IsAssignableToGenericType))
.ToList();
foreach (var type in types)
{
services.AddScoped(type);
var interfaces = type
.GetInterfaces()
.Where(x => x.IsPublic && x != typeof (IDisposable))
.ToList();
foreach (var @interface in interfaces)
{
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

0 comments on commit 4194707

Please sign in to comment.