在.net core 使用 PetaPoco
在.net core开发过程中,使用最多的就是注入方法。但是在.net core使用PetaPoco时,PetaPoco还不支持进行注入方式进行处理一些问题。
今天对PetaPoco进行了一些扩展,可以很方便的将PetaPoco进行注入操作,使用和EF很相似,但是更加简单
1、对PetaPoco.Compiled进行的一些扩展PetaPoco.Compiled.Extensions库
nuget:https://www.nuget.org/packages/PetaPoco.Compiled.Extensions/ 欢迎使用
github:https://github.com/mzy666888/PetaPoco.Compiled.Extensions 欢迎star
具体扩展内容如下
1.1 创建PetaPocoDBContextOptions类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | namespace PetaPoco.Compiled.Extensions { using Microsoft.Extensions.Options; public class PetaPocoDBContextOptions : IOptions<PetaPocoDBContextOptions> { /// <summary>The default configured TOptions instance</summary> PetaPocoDBContextOptions IOptions<PetaPocoDBContextOptions>.Value => this ; public string ConnectionString { get ; set ; } public string ProviderName { get ; set ; } } } |
1.2 创建接口IPetaPocoDBContext
接口继承IDatabase
1 2 3 4 | public interface IPetaPocoDBContext:IDatabase { } |
1.3 创建类PetaPocoDBContext
类继承IPetaPocoDBContext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | namespace PetaPoco.Compiled.Extensions { using Microsoft.Extensions.Options; using PetaPoco; public abstract class PetaPocoDBContext:Database,IPetaPocoDBContext { /// <summary> /// 构造函数 /// </summary> /// <param name="optionsAccessor"></param> protected PetaPocoDBContext(IOptions<PetaPocoDBContextOptions> optionsAccessor) : base (optionsAccessor.Value.ConnectionString, optionsAccessor.Value.ProviderName) { } } } |
1.4 添加对IServiceCollection的扩展
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 | namespace PetaPoco.Compiled.Extensions { using Microsoft.Extensions.DependencyInjection; public static class PetaPocoDBContextServiceCollectionExtensions { public static IServiceCollection AddPetaPoco<T>( this IServiceCollection services, Action<PetaPocoDBContextOptions> setupAction) where T : class ,IPetaPocoDBContext { if ( null == services) { throw new ArgumentNullException(nameof(services)); } if ( null == setupAction) { throw new ArgumentNullException(nameof(setupAction)); } services.AddOptions(); services.Configure(setupAction); services.AddScoped<IPetaPocoDBContext, T>(); return services; } } } |
这样对PetaPoco的扩展已经完成。
2.在ASP.NET Core MVC中使用PetaPoco.Compiled.Extensions
首先使用nuget对PetaPoco.Compiled.Extensions的引用
使用命令:Install-Package PetaPoco.Compiled.Extensions -Version 0.0.1
添加一个继承PetaPocoDBContext的DBContext类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | namespace PetaPoco.Compiled.Extensions.MvcTest.DBContexts { using Microsoft.Extensions.Options; public class MvcPetaPocoDBContext:PetaPocoDBContext { /// <summary> /// 构造函数 /// </summary> /// <param name="optionsAccessor"></param> public MvcPetaPocoDBContext(IOptions<PetaPocoDBContextOptions> optionsAccessor) : base (optionsAccessor) { } } } |
添加好后,就可以在Startup中进行注入了,如下图所示
需要添加MySQL.Data的nuget引用
在appsettings.json文件中,数据库连接字符串配置如下:
1 2 3 4 5 6 | "ConnectionStrings" : { "MySQL" : { "MvcMySQL" : "server=127.0.0.1;port=3306;uid=root;pwd=123456;database=WireCloud;" , "provider" : "MySql.Data.MySqlClient" } } |
添加数据库表:Users(后续将使用EFCore进行CodeFirst进行处理)
添加对Users的操作接口和实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | namespace PetaPoco.Compiled.Extensions.MvcTest.Services { using PetaPoco.Compiled.Extensions.MvcTest.Models; public interface IUserService { IList<User> GetAll(); } public class UserService : IUserService { private PetaPocoDBContext _context; public UserService(PetaPocoDBContext context) { _context = context; } public IList<User> GetAll() { return _context.Fetch<User>(); } } } |
在HomeController中添加一个Action
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 | namespace PetaPoco.Compiled.Extensions.MvcTest.Controllers { using PetaPoco.Compiled.Extensions.MvcTest.Services; public class HomeController : Controller { private IUserService _userService; public HomeController(IUserService userService) { _userService = userService; } public IActionResult Index() { return View(); } public IActionResult Privacy() { return View(); } [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true )] public IActionResult Error() { return View( new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }); } public IActionResult Users() { return View(_userService.GetAll()); } } } |
View实现
1 2 3 4 5 6 7 8 9 | @model System.Collections.Generic.IList<PetaPoco.Compiled.Extensions.MvcTest.Models.User> <div> @ foreach ( var user in Model) { <div>@user.Uid</div> <div>@user.UserName</div> } </div> |
原文章出处:https://www.freesion.com/article/8951913016/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南