0%

autofac入门学习

Auto FAC 7.0 , Web API Core 3.0 搭建三层项目框架 学习笔记

这是一个搭建三层项目基础架构的笔记。搭建这个框架的原因有下面三点:

为了向学习 比较新的 asp.net core框架,配合 EF core auto fac 依赖注入 搭建一个最基础的三层项目架构

为了搭建一个自己的小的脚手架项目,方便以后开发使用

为了能够增加对项目的理解,同时也是巩固以前学习和用过的一些知识点,来做一个实践

为什么要用依赖注入框架

如何来搭建

官方资料

部署教程

asp.net 三层项目如何搭建

asp.net web api 框架如何引入 auto FAC 框架

  • .Net core 3.1 AutoFac配置及各种注册方式

  • .NET Core Autofac 4.0的配置和使用示例代码

  • 官方的依赖注入

  • autofac .net-core 依赖注入

    • 依赖注入核心代码
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
    .ConfigureLogging((context, loggingBuilder) =>
    {
    loggingBuilder.AddFilter("System", LogLevel.Information);
    loggingBuilder.AddFilter("Microsoft", LogLevel.Information);
    var path = context.HostingEnvironment.ContentRootPath;
    loggingBuilder.AddLog4Net($"{path}/Configuration/log4net.config");//配置文件
    })
    .ConfigureWebHostDefaults(webBuilder =>
    {
    webBuilder.UseStartup<Startup>();
    // 此处配置使用 autoFac服务
    }).UseServiceProviderFactory(new AutofacServiceProviderFactory());


    public void ConfigureContainer(ContainerBuilder builder)
    {
    Assembly service = Assembly.Load("TinyErp.Service");
    Assembly data = Assembly.Load("TinyErp.Data");
    builder.RegisterAssemblyTypes(service, data)
    .Where(t => (t.FullName.EndsWith("Service") || t.Name.EndsWith("Repository") ) && !t.IsAbstract) //类名以service结尾,且类型不能是抽象的 
    .InstancePerLifetimeScope() //生命周期,,
    .AsImplementedInterfaces()
    .PropertiesAutowired(); //属性注入
    builder.RegisterGeneric(typeof(GenericRepository<>)).As(typeof(IGenericRepository<>)).AsImplementedInterfaces().InstancePerLifetimeScope();
    builder.RegisterType(typeof(TinyErpDbContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
    builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerLifetimeScope();

    //如果需要在controller中使用属性注入
    var controllerBaseType = typeof(ControllerBase);
    builder.RegisterAssemblyTypes(typeof(Program).Assembly)
    .Where(t => controllerBaseType.IsAssignableFrom(t) && t != controllerBaseType)
    .PropertiesAutowired();

    }


    [SwaggerResponse(201, "成功返回类型", Type = typeof(List<StudentDto>))]
    public List<StudentDto> GetStudentList(int page = 1,int limit = 10, string keyword = "")
    {

    _logger.LogInformation("LogInformation:" + DateTime.Now.ToString());
    _logger.LogWarning("LogWarning:" + DateTime.Now.ToString());
    _logger.LogError("LogError:" + DateTime.Now.ToString());
    _logger.LogDebug("LogDebug:" + DateTime.Now.ToString());
    return PooledRedisClientHelper.Get<List<StudentDto>>("TINYERP" + REDIS_KEYS.ADMIN_USERS, 10, () =>
    {
    _logger.LogError("LogError:" + DateTime.Now.ToString());
    // 通过依赖注入的 _studentService 来查询获取数据
    var res = _studentService.GetUserList();

    _logger.LogDebug("LogDebug:" + DateTime.Now.ToString(), res);
    return res;
    });

    }

asp.net 如何引入 EF core

  • 使用mvc的EF-core 微软官方教程

  • MVC + EFCore 项目实战 - 数仓管理系统2- 搭建基本框架配置EFCore

  • .Net Core2.2 + EF Core + DI,三层框架项目搭建教程 https://www.cnblogs.com/han1982/p/11058788.html

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    // 初始化创建数据库 
    private static void CreateDbIfNotExists(IHost host)
    {
    using (var scope = host.Services.CreateScope())
    {
    var services = scope.ServiceProvider;
    try
    {
    var context = services.GetRequiredService<TinyErpDbContext>();
    DbInitializer.Initialize(context);
    }
    catch (Exception ex)
    {
    var logger = services.GetRequiredService<ILogger<Program>>();
    logger.LogError(ex, "An error occurred creating the DB.");
    }
    }
    }

    // 创建DBContext
    public class TinyErpDbContext:DbContext
    {
    public TinyErpDbContext(DbContextOptions<TinyErpDbContext> options):base(options)
    {
    }

    public DbSet<Course> Courses { get; set; }
    public DbSet<Enrollment> Enrollments { get; set; }
    public DbSet<Student> Students { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
    modelBuilder.Entity<Course>().ToTable("Course");
    modelBuilder.Entity<Enrollment>().ToTable("Enrollment");
    modelBuilder.Entity<Student>().ToTable("Student");
    }
    }


ASP.NET 常用开发框架有哪些

  • abp ABP 使用人数还是蛮多的,整合了很多流行的技术组件和框架。而且也是专门设计了架构,不同我觉得最后自己也能对用到的组件有一个了解,能够独立搭建,不然直接使用框架,遇到问题,有时候也会没有头绪,因为很多概念,知识点都没有遇到过。

  • asp.net web开发框架

  • furion-开发框架

  • smart-store

遇到的坑

1
2
3
4
5
6
7
8
9
10
11
// 这一步 
public class StudentService : EntityService<Student>, IStudentService
{
public IUnitOfWork unitOfWork;
public IGenericRepository<Student> studentRepository;
public StudentService(IUnitOfWork unitOfWork, IGenericRepository<Student> studentRepository) : base(unitOfWork, studentRepository)
{
this.unitOfWork = unitOfWork;
this.studentRepository = studentRepository;
}
}

效果展示

三层项目架构目录结构

通过控制器中通过构造函数注入对象

调用注入对象的接口,成功获取数据