完善autofac模块

This commit is contained in:
橙子
2023-01-16 20:55:36 +08:00
parent 1314cf1c19
commit 506686b11e
13 changed files with 144 additions and 9 deletions

View File

@@ -7,7 +7,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Autofac.Extensions
namespace Yi.Framework.Core.Autofac.Extensions
{
public static class AutoFacExtensions
{
@@ -18,5 +18,10 @@ namespace Yi.Framework.Autofac.Extensions
return hostBuilder;
}
public static IHostBuilder ConfigureAutoFacContainer(this IHostBuilder hostBuilder, Action<ContainerBuilder> configureDelegate)
{
hostBuilder.UseAutoFacServerProviderFactory();
return hostBuilder.ConfigureContainer(configureDelegate);
}
}
}

View File

@@ -0,0 +1,32 @@
using Autofac;
using Autofac.Core.Registration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core.Autofac.Modules;
namespace Yi.Framework.Core.Autofac.Extensions
{
public static class AutoFacModuleExtensions
{
public static void RegisterYiModule<TModule>(this ContainerBuilder builder, params Assembly[] assemblies) where TModule : YiAutoFacModule, new()
{
new TModule().Load(builder, assemblies);
}
public static void RegisterYiModule(this ContainerBuilder builder, AutoFacModuleEnum autoFacModuleEnum, params Assembly[] assemblies)
{
switch (autoFacModuleEnum)
{
case AutoFacModuleEnum.PropertiesAutowiredModule:
new PropertiesAutowiredModule().Load(builder, assemblies);
break;
}
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Core.Autofac.Modules
{
public enum AutoFacModuleEnum
{
PropertiesAutowiredModule
}
}

View File

@@ -0,0 +1,32 @@
using Autofac;
using Autofac.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Yi.Framework.Core.Attributes;
namespace Yi.Framework.Core.Autofac.Modules
{
internal class PropertiesAutowiredModule : YiAutoFacModule
{
internal override void Load(ContainerBuilder containerBuilder, params Assembly[] assemblies)
{
containerBuilder.RegisterAssemblyTypes(assemblies)
.PropertiesAutowired(new AutowiredPropertySelector());
}
}
public class AutowiredPropertySelector : IPropertySelector
{
public bool InjectProperty(PropertyInfo propertyInfo, object instance)
{
return propertyInfo.CustomAttributes.Any(it => it.AttributeType == typeof(AutowiredAttribute));
}
}
}

View File

@@ -0,0 +1,15 @@
using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Core.Autofac.Modules
{
public abstract class YiAutoFacModule
{
internal abstract void Load(ContainerBuilder containerBuilder,params Assembly[] assemblies);
}
}

View File

@@ -12,7 +12,8 @@
</ItemGroup>
<ItemGroup>
<Folder Include="AutofacModule\" />
<ProjectReference Include="..\Yi.Framework.AspNetCore\Yi.Framework.AspNetCore.csproj" />
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Yi.Framework.Core.Attributes
{
[AttributeUsage(AttributeTargets.Property)]
public class AutowiredAttribute : Attribute
{
}
}

View File

@@ -89,7 +89,8 @@ namespace Yi.Framework.Core.Extensions
if (serviceInterfaces is not null)
{
var serviceType = type;
var firstInter = type.GetInterfaces().LastOrDefault();
var firstInter = type.GetInterfaces().Where(u => u != typeof(ITransientDependency)).LastOrDefault();
if (firstInter is not null)
{
serviceType = firstInter;

View File

@@ -36,6 +36,7 @@ namespace Yi.Framework.Core.Helper
return null;
}
/// <summary>
/// 设置对象属性值
/// </summary>
@@ -48,7 +49,6 @@ namespace Yi.Framework.Core.Helper
try
{
Type Ts = obj.GetType();
//object v = Convert.ChangeType(Value, Ts.GetProperty(FieldName).PropertyType);
Ts.GetProperty(FieldName).SetValue(obj, Value, null);
return true;
}

View File

@@ -9,6 +9,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Yi.Framework.Core.Autofac\Yi.Framework.Core.Autofac.csproj" />
<ProjectReference Include="..\Yi.Framework.Core.AutoMapper\Yi.Framework.Core.AutoMapper.csproj" />
<ProjectReference Include="..\Yi.Framework.Core\Yi.Framework.Core.csproj" />
</ItemGroup>

View File

@@ -1,10 +1,12 @@
using AspNetCore.Microsoft.AspNetCore.Builder;
using Autofac;
using System.Reflection;
using Yi.Framework.Application;
using Yi.Framework.Application.Contracts;
using Yi.Framework.Autofac.Extensions;
using Yi.Framework.Core;
using Yi.Framework.Core.Autofac.Extensions;
using Yi.Framework.Core.Autofac.Modules;
using Yi.Framework.Core.AutoMapper;
using Yi.Framework.Core.Extensions;
using Yi.Framework.Core.Sqlsugar;
@@ -14,7 +16,7 @@ using Yi.Framework.Domain.Shared;
using Yi.Framework.Sqlsugar;
using Yi.Framework.Web;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>еĵط<C4B5>
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseUrls(builder.Configuration.GetValue<string>("StartUrl"));
@@ -32,12 +34,20 @@ builder.UseYiModules(
typeof(YiFrameworkApplicationContractsModule).Assembly,
typeof(YiFrameworkApplicationModule).Assembly,
typeof(YiFrameworkWebModule).Assembly
);
//ʹ<><CAB9>autofac
builder.Host.UseAutoFacServerProviderFactory();
//<2F><><EFBFBD><EFBFBD>autofacģ<63><C4A3>,<2C><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>
builder.Host.ConfigureAutoFacContainer(container =>
{
container.RegisterYiModule(AutoFacModuleEnum.PropertiesAutowiredModule, typeof(YiFrameworkWebModule).Assembly);
});
var app = builder.Build();
var t = app.Services.GetService<Test2Entity>();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,11 @@
using Yi.Framework.Core.Attributes;
using Yi.Framework.Core.DependencyInjection;
namespace Yi.Framework.Web
{
public class Test2Entity: ITransientDependency
{
[Autowired]
public TestEntity testEntity { get; set; }
}
}

View File

@@ -4,5 +4,6 @@ namespace Yi.Framework.Web
{
public class TestEntity: ITransientDependency
{
}
}