当前位置: 首页 > news >正文

Resume全栈项目(.NET)


文章目录

  • 项目地址
  • 一、数据库准备/日志安装
    • 1.1 创建实体层
      • 1. Entities
      • 2. Enums 存放枚举
    • 1.2 创建数据库层
      • 1. 安装Persistance层需要的库
      • 2. 创建ResumeDbContext
      • 3. 添加数据库配置/注册DBContext
      • 4. 执行Add-Migration
      • 5. 修改字段类型
      • 6. Enum支持Json
    • 1.3 安装Serilog
      • 1. Api层安装所需要的包
      • 2. 在appsetting里配置
      • 3. 注册Serilog在Program里
      • 4.EfCore开启日志
      • 5. 在Hanlder里使用
  • 二、Application层
    • 2.1 安装MediaR和AutoMapper
      • 1. 安装所需要的包和引用
      • 2. 添加ApplicationRegistration
      • 3. 在Program里注册


项目地址

  • 教程作者:
  • 教程地址:
https://www.CSDN.com/watch?v=AiwzQMupPsU
  • 代码仓库地址作者:
https://github.com/mohammad-taheri1/Youtube-Resume-Management-dotnet-react-ts
  • 代码仓库自己:
https://github.com/CXTV/Resume/tree/main/backend

一、数据库准备/日志安装

在这里插入图片描述

1.1 创建实体层

1. Entities

  1. BaseEntity.cs
namespace ResumeManagement.Domain.Entities
{
    public abstract class BaseEntity
    {
        public Guid ID { get; set; }   
        public DateTime CreatedAt { get; set; } = DateTime.Now;
        public DateTime UpdateAt { get; set; } = DateTime.Now;
        public bool isActive { get; set; } = true;  
    }
}
  1. Company.cs : 里面有Job的Collection
namespace ResumeManagement.Domain.Entities
{
    public class Company:BaseEntity
    {
        public string Name { get; set; }
        public CompanySize Size { get; set; }
        //relations 一对多
        public ICollection<Job> Jobs { get; set; }
    }
}
  1. Job.cs:每个职位有对应的公司id和名称,一个职位有多个候选人
namespace ResumeManagement.Domain.Entities
{
    public class Job: BaseEntity
    {
        public string Title { get; set; }
        public JobLevel Level { get; set; }
        //relations 多对一
        public Guid CompanyID { get; set; }
        public Company Company { get; set; }
        //relations 一对多一个职位可以有多个候选人
        public ICollection<Candidate> Candidates { get; set; }
    }
}
  1. Candidate.cs: 每个候选人,有投递的工作ID和工作名称
namespace ResumeManagement.Domain.Entities
{
    public class Candidate: BaseEntity
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public string CoverLetter { get; set; }
        public string ResumeUrl { get; set; }
        //relations 多对一
        public Guid JobID { get; set; }
        public Job Job { get; set; }
    }
}

2. Enums 存放枚举

  1. 公司规模的枚举
namespace ResumeManagement.Domain.Enums
{
    public enum CompanySize
    {
        Small,
        Medium,
        Large
    }
}

2.工作等级的

1.2 创建数据库层

在这里插入图片描述

1. 安装Persistance层需要的库

在这里插入图片描述

2. 创建ResumeDbContext

  • 这里定义表的关系
    在这里插入图片描述

3. 添加数据库配置/注册DBContext

  1. API层数据库连接appsettings.json
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "ConnectionStrings": {
    "ResumeDBConnectionStrings": "Server=.;Database=ResumeDB;Trusted_Connection=True;MultipleActiveResultSets=true;TrustServerCertificate=True;"
  },
  "AllowedHosts": "*"
}
  1. 在Persistance层创建PersistanceRegistration.cs文件

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace ResumenManagement.Persistance
{
    public static class PersistanceRegistration
    {
        public static void  AddPersistanceRegistration(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddDbContext<ResumeDbContext>(options =>
        options.UseSqlServer(configuration.GetConnectionString("ResumeDBConnectionStrings")));
        }
    }
}

  1. 在Api层里注册

在这里插入图片描述

4. 执行Add-Migration

  • 注意:如果从新添加了文件夹,一定要build项目之后,重启项目,才会执行migration成功
    在这里插入图片描述
  • 执行成功后,在数据里,就可以看到我们的表

在这里插入图片描述

5. 修改字段类型

  • 在我们创建完成表之后,返现Size是int类型,Job里的Level也是int类型,这是因为枚举类型造成的原因,需要在DbContext里修改类型
  • 在这里插入图片描述

6. Enum支持Json

  • Program.cs里添加
builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});

1.3 安装Serilog

1. Api层安装所需要的包

在这里插入图片描述

2. 在appsetting里配置

  • 直接配置在setting之后,就不需要在中间件里配置
  "Serilog": {
    "MinimumLevel": {
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.EntityFrameworkCore": "Information"
      }
    },
    "WriteTo": [
      {
        "Name": "Console",
        "Args": {
          "outputTemplate": "[{Timestamp:dd-MM HH:mm:ss} {Level:u3}] |{SourceContext}| {NewLine}{Message:lj}{NewLine}{Exception}"
        }
      },
      {
        "Name": "File",
        "Args": {
          "path": "Logs/Resturant-API-.log",
          "rollingInterval": "Day",
          "rollOnFileSizeLimit": true,
          "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact"
        }
      }
    ]
  },
  "AllowedHosts": "*"

3. 注册Serilog在Program里

  • 注册服务以及中间件
    在这里插入图片描述

4.EfCore开启日志

  • 在Persistence层里,efCore服务注册的地方PersistanceRegistration.cs
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ResumenManagement.Application.Contracts.Persistance;
using ResumenManagement.Persistance.Repositories;

namespace ResumenManagement.Persistance
{
    public static class PersistanceRegistration
    {
        public static void  AddPersistanceRegistration(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddDbContext<ResumeDbContext>(options =>
                options.UseSqlServer(configuration.GetConnectionString("ResumeDBConnectionStrings"))
                .EnableSensitiveDataLogging() // 启用敏感数据记录
                );

            services.AddScoped(typeof(IAsyncRepository<>), typeof(BaseRepository<>));
            services.AddScoped<ICompanyRepository, CompanyRepository>();
        }
    }
}

5. 在Hanlder里使用

  1. 需要先在Application层安装一个包才可以使用
    在这里插入图片描述
  2. 注册服务以及使用

在这里插入图片描述

二、Application层

2.1 安装MediaR和AutoMapper

1. 安装所需要的包和引用

在这里插入图片描述

2. 添加ApplicationRegistration

  • 在Application层添加ApplicationRegistration.cs
using Microsoft.Extensions.DependencyInjection;

namespace ResumenManagement.Application
{
    public static class ApplicationRegistration
    {
        public static void AddApplicationRegistration(this IServiceCollection services)
        {
            //1.获取应用程序程序集
            var applicationAssembly = typeof(ApplicationRegistration).Assembly;

            //2.注册所有MediatR处理程序
            services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(applicationAssembly));
            //3.注册所有AutoMapper配置
            services.AddAutoMapper(applicationAssembly);              
        }
    }
}

3. 在Program里注册

  • Program.cs里添加ApplicationRegistration

在这里插入图片描述

在这里插入图片描述

相关文章:

  • Servlet、HttpServletRequest、HttpServletResponse、静态与动态网页、jsp、重定向与转发
  • 旋转编码器
  • 医学交互作用分析步骤和目的(R语言)
  • @Validated 使用介绍
  • Android开源库——Glide
  • Java主流开发框架之请求响应常用注释
  • 【LeetCode】大厂面试算法真题回忆(36)--相同数字的积木游戏
  • FFmpeg + ‌Qt‌ 简单视频播放器代码
  • 【算法笔记】图论基础(一):建图、存图、树和图的遍历、拓扑排序、最小生成树
  • 如何在 Bash 中不依赖 curl 或 wget 发出 HTTP 请求并实现文件传输——/dev/tcp的妙用
  • illustrate:一款蛋白/核酸结构快速渲染为“卡通风格”的小工具
  • Ciura序列
  • 弱网测试:全链路实战、高阶策略与自动化落地
  • 多线程14(哈希表与文件操作IO)
  • CPU架构和微架构
  • 中颖SH366000介绍和使用全解
  • Web安全策略CSP详解与实践
  • HTTP请求过程详解
  • 构建自定义MCP天气服务器:集成Claude for Desktop与实时天气数据
  • /2要求:定义一个方法,根据id查找对应的用户信息 //如果存在,返回id //如果不存在,返回-1
  • 俄宣布停火三天,外交部:希望各方继续通过对话谈判解决危机
  • 西班牙葡萄牙遭遇史上最严重停电:交通瘫了,通信崩了,民众疯抢物资
  • 我国成功发射卫星互联网低轨卫星
  • 广东一公司违规开展学科培训被罚没470万,已注销营业执照
  • 劳动最光荣!2426人受到表彰
  • 特朗普将举行集会庆祝重返白宫执政百日,被指时机不当