Browse Source

Resolved #148: Register Controller, PageModel and other MVC components to DI by default.

pull/179/head
Halil İbrahim Kalkan 9 years ago
parent
commit
a65f57da99
  1. 6
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs
  2. 3
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs
  3. 3
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpViewComponent.cs
  4. 45
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs
  5. 3
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/RazorPages/AbpPageModel.cs
  6. 27
      src/Volo.Abp/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs

6
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs

@ -16,6 +16,7 @@ using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ViewComponents; using Microsoft.AspNetCore.Mvc.ViewComponents;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using Volo.Abp.AspNetCore.Mvc.Conventions; using Volo.Abp.AspNetCore.Mvc.Conventions;
using Volo.Abp.AspNetCore.Mvc.DependencyInjection;
using Volo.Abp.AspNetCore.Mvc.VirtualFileSystem; using Volo.Abp.AspNetCore.Mvc.VirtualFileSystem;
using Volo.Abp.Http; using Volo.Abp.Http;
using Volo.Abp.Http.Modeling; using Volo.Abp.Http.Modeling;
@ -26,6 +27,11 @@ namespace Volo.Abp.AspNetCore.Mvc
[DependsOn(typeof(AbpAspNetCoreModule))] [DependsOn(typeof(AbpAspNetCoreModule))]
public class AbpAspNetCoreMvcModule : AbpModule public class AbpAspNetCoreMvcModule : AbpModule
{ {
public override void PreConfigureServices(IServiceCollection services)
{
services.AddConventionalRegistrar(new AbpAspNetCoreMvcConventionalRegistrar());
}
public override void ConfigureServices(IServiceCollection services) public override void ConfigureServices(IServiceCollection services)
{ {
//Configure Razor //Configure Razor

3
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpController.cs

@ -2,14 +2,13 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.ObjectMapping; using Volo.Abp.ObjectMapping;
using Volo.Abp.Uow; using Volo.Abp.Uow;
namespace Volo.Abp.AspNetCore.Mvc namespace Volo.Abp.AspNetCore.Mvc
{ {
public abstract class AbpController : Controller, ITransientDependency public abstract class AbpController : Controller
{ {
public IUnitOfWorkManager UnitOfWorkManager { get; set; } public IUnitOfWorkManager UnitOfWorkManager { get; set; }

3
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpViewComponent.cs

@ -1,9 +1,8 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc namespace Volo.Abp.AspNetCore.Mvc
{ {
public abstract class AbpViewComponent : ViewComponent, ITransientDependency public abstract class AbpViewComponent : ViewComponent
{ {
} }
} }

45
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DependencyInjection/AbpAspNetCoreMvcConventionalDependencyRegistrar.cs

@ -0,0 +1,45 @@
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.DependencyInjection;
namespace Volo.Abp.AspNetCore.Mvc.DependencyInjection
{
public class AbpAspNetCoreMvcConventionalRegistrar : DefaultConventionalRegistrar
{
protected override ServiceLifetime? GetServiceLifetimeFromClassHierarcy(Type type)
{
var lifeTime = base.GetServiceLifetimeFromClassHierarcy(type);
if (lifeTime != null)
{
return lifeTime;
}
if (IsController(type) ||
IsPageModel(type) ||
IsViewComponent(type))
{
return ServiceLifetime.Transient;
}
return null;
}
private static bool IsPageModel(Type type)
{
return typeof(PageModel).IsAssignableFrom(type) || type.IsDefined(typeof(PageModelAttribute), true);
}
private static bool IsController(Type type)
{
return typeof(Controller).IsAssignableFrom(type) || type.IsDefined(typeof(ControllerAttribute), true);
}
private static bool IsViewComponent(Type type)
{
return typeof(ViewComponent).IsAssignableFrom(type) || type.IsDefined(typeof(ViewComponentAttribute), true);
}
}
}

3
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/RazorPages/AbpPageModel.cs

@ -2,14 +2,13 @@
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids; using Volo.Abp.Guids;
using Volo.Abp.ObjectMapping; using Volo.Abp.ObjectMapping;
using Volo.Abp.Uow; using Volo.Abp.Uow;
namespace Volo.Abp.AspNetCore.Mvc.RazorPages namespace Volo.Abp.AspNetCore.Mvc.RazorPages
{ {
public abstract class AbpPageModel : PageModel, ITransientDependency //TODO: Register PageModels by convention, instead of using ITransientDependency public abstract class AbpPageModel : PageModel
{ {
public IUnitOfWorkManager UnitOfWorkManager { get; set; } public IUnitOfWorkManager UnitOfWorkManager { get; set; }

27
src/Volo.Abp/Volo/Abp/DependencyInjection/DefaultConventionalRegistrar.cs

@ -1,24 +1,24 @@
using System; using System;
using System.Reflection; using System.Reflection;
using JetBrains.Annotations;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
namespace Volo.Abp.DependencyInjection namespace Volo.Abp.DependencyInjection
{ {
//TODO: Make DefaultConventionalRegistrar extensible, so we can only define GetLifeTimeOrNull to contribute to the convention. This can be more performant!
public class DefaultConventionalRegistrar : ConventionalRegistrarBase public class DefaultConventionalRegistrar : ConventionalRegistrarBase
{ {
public override void AddType(IServiceCollection services, Type type) public override void AddType(IServiceCollection services, Type type)
{ {
var typeInfo = type.GetTypeInfo(); if (IsConventionalRegistrationDisabled(type))
if (typeInfo.IsDefined(typeof(DisableConventionalRegistrationAttribute), true))
{ {
return; return;
} }
var dependencyAttribute = typeInfo.GetCustomAttribute<DependencyAttribute>(true); var dependencyAttribute = GetDependencyAttributeOrNull(type);
var lifeTime = GetLifeTimeOrNull(type, dependencyAttribute);
var lifeTime = dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromInterfaces(type);
if (lifeTime == null) if (lifeTime == null)
{ {
return; return;
@ -43,7 +43,22 @@ namespace Volo.Abp.DependencyInjection
} }
} }
protected virtual ServiceLifetime? GetServiceLifetimeFromInterfaces(Type type) protected virtual bool IsConventionalRegistrationDisabled(Type type)
{
return type.IsDefined(typeof(DisableConventionalRegistrationAttribute), true);
}
protected virtual DependencyAttribute GetDependencyAttributeOrNull(Type type)
{
return type.GetCustomAttribute<DependencyAttribute>(true);
}
protected virtual ServiceLifetime? GetLifeTimeOrNull(Type type, [CanBeNull] DependencyAttribute dependencyAttribute)
{
return dependencyAttribute?.Lifetime ?? GetServiceLifetimeFromClassHierarcy(type);
}
protected virtual ServiceLifetime? GetServiceLifetimeFromClassHierarcy(Type type)
{ {
if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type)) if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type))
{ {

Loading…
Cancel
Save