webapi开发框架实践
项目链接以及目录结构
liuzhixin405/efcore-template (github.com)

这是一个纯webapi的开发框架。
1、支持的orm有efcore6、dapper,可以灵活切换数据库。
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Elfie.Model;
using Microsoft.EntityFrameworkCore;
using project.Context;
using project.Repositories;
using project.Services;
using RepositoryComponent.DbFactories; namespace project.Extensions
{
public static partial class TheExtensions
{
public static void AddDatabase(this WebApplicationBuilder builder)
{
///sqlserver
if (builder.Configuration["DbType"]?.ToLower() == "sqlserver")
{
builder.Services.AddDbContext<ReadProductDbContext>(options => options.UseSqlServer(builder.Configuration["ConnectionStrings:SqlServer:ReadConnection"]), ServiceLifetime.Scoped);
builder.Services.AddDbContext<WriteProductDbContext>(options => options.UseSqlServer(builder.Configuration["ConnectionStrings:SqlServer:WriteConnection"]), ServiceLifetime.Scoped); }
///mysql
else if (builder.Configuration["DbType"]?.ToLower() == "mysql")
{
builder.Services.AddDbContext<ReadProductDbContext>(options => options.UseMySQL(builder.Configuration["ConnectionStrings:MySql:ReadConnection"]), ServiceLifetime.Scoped);
builder.Services.AddDbContext<WriteProductDbContext>(options => options.UseMySQL(builder.Configuration["ConnectionStrings:MySql:WriteConnection"]), ServiceLifetime.Scoped); }
else
{
//throw new ArgumentNullException("δ����ȷ��ע�����ݿ�");
builder.Services.AddDbContext<ReadProductDbContext>(options => options.UseInMemoryDatabase("test_inmemory_db"), ServiceLifetime.Scoped);
builder.Services.AddDbContext<WriteProductDbContext>(options => options.UseInMemoryDatabase("test_inmemory_db"), ServiceLifetime.Scoped); } builder.Services.AddScoped<Func<ReadProductDbContext>>(provider => () => provider.GetService<ReadProductDbContext>() ?? throw new ArgumentNullException("ReadProductDbContext is not inject to program"));
builder.Services.AddScoped<Func<WriteProductDbContext>>(provider => () => provider.GetService<WriteProductDbContext>() ?? throw new ArgumentNullException("WriteProductDbContext is not inject to program")); builder.Services.AddScoped<DbFactory<WriteProductDbContext>>();
builder.Services.AddScoped<DbFactory<ReadProductDbContext>>(); builder.Services.AddTransient<IReadProductRepository, ProductReadRepository>();
builder.Services.AddTransient<IWriteProductRepository, ProductWriteRepository>();
builder.Services.AddTransient<IProductService, ProductService>(); builder.Services.AddTransient<ICustomerService, CustomerService>();
}
}
}
2、至于消息中间件有rabbitmq、kafka,也是通过配置文件来指定哪一个实现。
using MessageMiddleware.Factory;
using MessageMiddleware.RabbitMQ; namespace project.Extensions
{
public static partial class TheExtensions
{
public static void AddMq(this WebApplicationBuilder builder)
{
var rabbitMqSetting = new RabbitMQSetting
{
ConnectionString = builder.Configuration["MqSetting:RabbitMq:ConnectionString"].Split(';'),
Password = builder.Configuration["MqSetting:RabbitMq:PassWord"],
Port = int.Parse(builder.Configuration["MqSetting:RabbitMq:Port"]),
SslEnabled = bool.Parse(builder.Configuration["MqSetting:RabbitMq:SslEnabled"]),
UserName = builder.Configuration["MqSetting:RabbitMq:UserName"],
};
var kafkaSetting = new MessageMiddleware.Kafka.Producers.ProducerOptions
{
BootstrapServers = builder.Configuration["MqSetting:Kafka:BootstrapServers"],
SaslUsername = builder.Configuration["MqSetting:Kafka:SaslUserName"],
SaslPassword = builder.Configuration["MqSetting:Kafka:SaslPassWord"],
Key = builder.Configuration["MqSetting:Kafka:Key"]
};
var mqConfig = new MQConfig
{
ConsumerLog = bool.Parse(builder.Configuration["MqSetting:ConsumerLog"]),
PublishLog = bool.Parse(builder.Configuration["MqSetting:PublishLog"]),
Rabbit = rabbitMqSetting,
Use = int.Parse(builder.Configuration["MqSetting:Use"]),
Kafka = kafkaSetting
};
builder.Services.AddSingleton<MQConfig>(sp => mqConfig);
builder.Services.AddMQ(mqConfig);
}
}
}
3、该项目还集成了mongodb和elasticsearch,在project项目中没有写实现案例,实现起来也很简单。
4、下面是分布式雪花id的实现,先注入代码,使用的时候直接使用distributedid即可。
builder.Services.AddDistributedLock(x =>
{
x.LockType = LockType.InMemory;
x.RedisEndPoints = new string[] { builder.Configuration["DistributedRedis:ConnectionString"] ?? throw new Exception("$未能获取distributedredis连接字符串")};
}).AddCache(new CacheOptions
{
CacheType = CacheTypes.Redis,
RedisConnectionString = builder.Configuration["DistributedRedis:ConnectionString"] ?? throw new Exception("$未能获取distributedredis连接字符串")
}).AddDistributedId(new DistributedIdOptions
{
Distributed = true
});
newProduct.Id = _distributedId.NewLongId().ToString();
5、缓存使用的是分布式缓存和内存缓存,其中分布式缓存有一般实现和指定序列化格式的实现。
using System.Text;
using System.Text.Json.Serialization;
using MessagePack;
using StackExchange.Redis.Extensions.Core;
using StackExchange.Redis.Extensions.Core.Abstractions;
using StackExchange.Redis.Extensions.Core.Configuration;
using StackExchange.Redis.Extensions.Core.Implementations; namespace project.Utility.Helper
{
public class CacheHelper
{
private static IRedisClientFactory _factory_with_msgpack;
private static IRedisDatabase _redis_with_msgpack => _factory_with_msgpack.GetDefaultRedisDatabase(); private static IRedisClientFactory _factory;
private static IRedisDatabase _redis => _factory.GetDefaultRedisDatabase();
public static void Init(IConfiguration configuration)
{
var config = configuration.GetSection("Redis").Get<RedisConfiguration>();
_factory = new RedisClientFactory(new[] { config }, null, new RedisSerializer());
_factory_with_msgpack = new RedisClientFactory(new[] { config }, null, new RedisMessagepackSerializer());
}
static CacheHelper() { } public static T Get<T>(string key)
{
return _redis.GetAsync<T>(key).GetAwaiter().GetResult();
}
public static async Task<T> GetAsync<T>(string key)
{
return await _redis.GetAsync<T>(key);
}
public static async Task<T> GetAsync_With_Msgpack<T>(string key)
{
return await _redis_with_msgpack.GetAsync<T>(key);
} public static string Get(string key)
{
return _redis.GetAsync<string>(key).GetAwaiter().GetResult();
} public static bool Set(string key, object value, TimeSpan expiresIn)
{
return _redis.AddAsync(key, value, expiresIn).GetAwaiter().GetResult();
}
public static async Task<bool> SetAsync(string key, object value, TimeSpan expiresIn)
{
return await _redis.AddAsync(key, value, expiresIn);
} public static async Task<bool> SetAsync_With_Msgpack(string key, object value, TimeSpan expiresIn)
{
return await _redis_with_msgpack.AddAsync(key, value, expiresIn);
} /// <summary>
/// 以秒为单位,返回给定 key 的剩余生存时间
/// </summary> public static long GetExpirin(string key)
{
var timespan = _redis.Database.KeyTimeToLive(key);
if (timespan == null) { return 0; }
return (long)timespan.Value.TotalSeconds;
}
public static bool KeyExpire(string key, TimeSpan expiresIn)
{
return _redis.Database.KeyExpire(key, expiresIn);
}
public static async Task<bool> RemoveKeyAsync(string key)
{
return await _redis.Database.KeyDeleteAsync(key);
}
public static long RemoveKey(string key)
{
var result = _redis.Database.KeyDelete(key);
return result ? 1 : 0;
}
} public class RedisSerializer : ISerializer
{
public T? Deserialize<T>(byte[] serializedObject)
{
var data = Encoding.UTF8.GetString(serializedObject);
return System.Text.Json.JsonSerializer.Deserialize<T>(data);
} public byte[] Serialize<T>(T? item)
{
var data = System.Text.Json.JsonSerializer.Serialize(item);
return Encoding.UTF8.GetBytes(data);
}
} public class RedisMessagepackSerializer : ISerializer
{
private MessagePackSerializerOptions _options;
public RedisMessagepackSerializer()
{
_options = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray);
}
public T? Deserialize<T>(byte[] serializedObject)
{
return MessagePackSerializer.Deserialize<T>(serializedObject, _options);
} public byte[] Serialize<T>(T? item)
{
return MessagePackSerializer.Serialize(item, _options);
}
}
}
6、单元测试、集成测试没有写。
更细节的需要自己看代码,这应该是一个基本的开发具备的功能。
该项目下载下来可以直接运行。
webapi开发框架实践的更多相关文章
- Restful WebApi开发实践
随笔分类 - Restful WebApi开发实践 C#对WebApi数据操作 摘要: ## 目标简化并统一程序获取WebApi对应实体数据的过程,方便对实体进行扩充.原理就是数据服务使用反射发现 ...
- 一次asp.net core3.1打造webapi开发框架的实践
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAigAAAAbCAYAAABWfHSvAAAH30lEQVR4nO1dy5GsMAx80RIESRAEST ...
- 快读《ASP.NET Core技术内幕与项目实战》WebApi3.1:WebApi最佳实践
本节内容,涉及到6.1-6.6(P155-182),以WebApi说明为主.主要NuGet包:无 一.创建WebApi的最佳实践,综合了RPC和Restful两种风格的特点 1 //定义Person类 ...
- 42岁大龄程序员的迷茫,看我最新尝鲜.net 5+Dapper搭建的WebAPI框架
42岁大龄程序员的迷茫 我真傻,真的.我单知道雪天是野兽在深山里没有食吃,会到村里来;我不知道春天也会有-- 我真傻,真的.我单知道程序员要活到老学到老,年龄大了要失业;我不知道码农(新型农民工)也会 ...
- WebAPI
WebAPI的Host OWIN IIS WebAPI 的handler和Filter有啥区别? WebAPI 常用 Filters Exception Filter Timer Filter Lo ...
- C#对WebApi数据操作
目标 简化并统一程序获取WebApi对应实体数据的过程,方便对实体进行扩充.原理就是数据服务使用反射发现数据提供者,处理好泛型就行. 相关传送门:Restful WebApi开发实践 先来看下最后的请 ...
- 适合WebApi的简单的C#状态机实现
目标 采用了Restful WebApi的架构,再把业务逻辑状态转移放到后端就有点违背初衷了.实际上只要后端Api的权限设置的好,把状态转移放到前端也未尝不可.我考虑的结果是,一般如果变更这个状态本身 ...
- 大道至简、大智若愚—GO语言最佳详解实践
导读:2007年,受够了C++煎熬的Google首席软件工程师Rob Pike纠集Robert Griesemer和Ken Thompson两位牛人,决定创造一种新语言来取代C++, 这就是Gol ...
- 你应该知道Go语言的几个优势
要说起GO语言的优势,我们就得从GO语言的历史讲起了-- 本文由腾讯技术工程官方号发表在腾讯云+社区 2007年,受够了C++煎熬的Google首席软件工程师Rob Pike纠集Robert Grie ...
- 基于.NetCore开发博客项目 StarBlog - (3) 模型设计
系列文章 基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客? 基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目 基于.NetC ...
随机推荐
- hvv蓝初面试常见漏洞问题(上)
1.SQL注入 漏洞成因: 可控变量 变量会带入数据库查询 变量不存在过滤或者变量过滤不严格 注入流程 判断是否有注入点 order by 判断字段数量 union select 报错查看注入点 使用 ...
- 如何在前端应用中合并多个 Excel 工作簿
本文由葡萄城技术团队于博客园原创并首发.葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言|问题背景 SpreadJS是纯前端的电子表格控件,可以轻松加载 Excel 工作簿中的数据 ...
- Kubernetes(k8s)健康性检查:livenessprobe探测和readinessprobe探测
目录 一.系统环境 二.前言 三.Kubernetes健康性检查简介 四.创建没有探测机制的pod 五.添加livenessprobe探测 5.1 使用command的方式进行livenessprob ...
- 攻防世界_ezmaze
题目:ezmaze re选手投递区 链接:https://adworld.xctf.org.cn/challenges/details?hash=8254ba70-6bfd-11ed-ab28-000 ...
- Windows AD域查询属性-密码过期时间
Windows AD域查询属性-密码过期时间 Windows PowerShell命令方式查询: net user zhou /domain找出 SamAccountName 的值为zhou的用户部分 ...
- 浙大Jarvisoj [XMAN]level6 Writeup
分析代码 初始化 0x0804A2EC:保存malloc(0xC10)返回的指针 malloc(0xC10) 0 1 ... ... value note 总数:256 已使用 note 数 0 一. ...
- 前端vue可以左右滚动的切换的tabs tabs选项卡 滑动动画效果 自动宽度
前端vue可以左右滚动的切换的tabs tabs选项卡 滑动动画效果 自动宽度, 下载完整代码请访问https://ext.dcloud.net.cn/plugin?id=13003 效果图如下: ...
- 构建 JavaScript ChatGPT 插件
聊天插件系统是一种令人兴奋的新方式,可以扩展ChatGPT的功能,纳入您自己的业务数据,并为客户与您的业务互动增加另一个渠道.在这篇文章中,我将解释什么是聊天插件,它们能做什么,以及你如何用JavaS ...
- C# 集合类 入门
什么是集合类? 集合类的位置在System.Collections.Generic命名空间中. 在我看来,集合类和大学里<数据结构>中所学的各种结构很像.集合类中包含Queue<T& ...
- 详解RISC v中断
声明 本文为本人原创,未经许可严禁转载.部分图源自网络,如有侵权,联系删除. RISC-V 中断与异常 trap(陷阱)可以分为异常与中断.在 RISC v 下,中断有三种来源:software in ...