CSharp-Single-EFCore/Bunny.WebApi/Configuration/AddAutofacConfig.cs

32 lines
1.2 KiB
C#
Raw Normal View History

using Autofac;
using Bunny.Common.Context.Database;
using Bunny.Service.IService;
using Bunny.Service.IService.Service;
using Microsoft.AspNetCore.Mvc;
namespace Bunny.WebApi.Configuration;
public static class AddAutofacConfig
{
public static void BuildContainer(ContainerBuilder builder)
{
// 异步方式创建数据库
new EfCoreContext().Database.EnsureCreatedAsync();
// 如果不在在 Controller 层写构造函数可以打开这个,自动完成注入
var controllerBaseType = typeof(ControllerBase);
builder.RegisterAssemblyTypes(typeof(Program).Assembly)
.Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
.PropertiesAutowired(); //支持属性注入
// 注入EfCore上下文对象
builder.RegisterType<EfCoreContext>();
// 注入Service服务
builder.RegisterType<BaseService>().As<IBaseService>();
builder.RegisterType<BlogService>().As<IBlogService>();
builder.RegisterType<RedisOptionService>().As<IRedisOptionService>();
builder.RegisterType<JobService>().As<IJobService>();
builder.RegisterType<MinioService>().As<IMinioService>();
}
}