.Net Core中一个特别重要的特性就是依赖注入功能,那么我们在使用PetaPoco的时候是否也可以使用依赖注入特性呢?

回答当然是可以的啦。使用方法(两种注入方式)如下

services.AddScoped<IDatabase>(
x =>
{
var connectionStrnig = Configuration["ConnectionStrings:MySQL:MvcMySQL"];
var configuration = DatabaseConfiguration.Build().UsingConnectionString(connectionStrnig)
.UsingProvider<MariaDbDatabaseProvider>();
return new PetaPocoMvcDBContext(configuration);
}); services.AddScoped<IDatabase, PetaPocoMvcDBContext>(
(x) =>
{
var connectionStrnig = Configuration["ConnectionStrings:MySQL:MvcMySQL"];
var configuration = DatabaseConfiguration.Build().UsingConnectionString(connectionStrnig)
.UsingProvider<MariaDbDatabaseProvider>();
return new PetaPocoMvcDBContext(configuration);
});

定义的PetaPocoMvcDBContext类:

namespace PetaPocoEfCoreMvc.DBContext
{
using System.Data.Common; using PetaPoco;
using PetaPoco.Core; public class PetaPocoMvcDBContext:Database
{
public PetaPocoMvcDBContext(DbConnection connection, IMapper defaultMapper = null)
: base(connection, defaultMapper)
{
} public PetaPocoMvcDBContext(string connectionString, string providerName, IMapper defaultMapper = null)
: base(connectionString, providerName, defaultMapper)
{
} public PetaPocoMvcDBContext(string connectionString, DbProviderFactory factory, IMapper defaultMapper = null)
: base(connectionString, factory, defaultMapper)
{
} public PetaPocoMvcDBContext(string connectionString, IProvider provider, IMapper defaultMapper = null)
: base(connectionString, provider, defaultMapper)
{
} public PetaPocoMvcDBContext(IDatabaseBuildConfiguration configuration)
: base(configuration)
{
}
}
}

appsetting.json中的数据库连接字符串:

"ConnectionStrings": {
"MySQL": {
"MvcMySQL": "server=127.0.0.1;port=3306;uid=root;pwd=123456;database=WireCloud;",
"provider": "MySql.Data.MySqlClient"
}
}

添加UserService相关类:

namespace PetaPocoEfCoreMvc.Service
{
using PetaPocoEfCoreMvc.Models; public interface IUserService
{
IEnumerable<User> GetAll();
}
} namespace PetaPocoEfCoreMvc.Service
{
using PetaPoco;
using PetaPocoEfCoreMvc.Models; public class UserService:IUserService
{
private readonly IDatabase _database;
    //构造函数注入
public UserService(IDatabase database)
{
_database = database;
} public IEnumerable<User> GetAll()
{
return _database.Fetch<User>();
}
}
}

PetaPoco在ASP.NET Core 2.2中使用注入方式访问数据库的更多相关文章

  1. ASP.NET Core HTTP 管道中的那些事儿

    前言 马上2016年就要过去了,时间可是真快啊. 上次写完 Identity 系列之后,反响还不错,所以本来打算写一个 ASP.NET Core 中间件系列的,但是中间遇到了很多事情.首先是 NPOI ...

  2. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  3. 在ASP.NET Core 1.0中如何发送邮件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:目前.NET Core 1.0中并没有提供SMTP相关的类库,那么要如何从ASP.NE ...

  4. ASP.NET Core 1.0 中使用 Swagger 生成文档

    github:https://github.com/domaindrivendev/Ahoy 之前文章有介绍在ASP.NET WebAPI 中使用Swagger生成文档,ASP.NET Core 1. ...

  5. 用ASP.NET Core 1.0中实现邮件发送功能

    准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试 ...

  6. 在ASP.NET Core Web API中为RESTful服务增加对HAL的支持

    HAL(Hypertext Application Language,超文本应用语言)是一种RESTful API的数据格式风格,为RESTful API的设计提供了接口规范,同时也降低了客户端与服务 ...

  7. 基础教程:视图中的ASP.NET Core 2.0 MVC依赖注入

    问题 如何在ASP.NET Core MVC Views中注入和使用服务. 解 更新 启动 类来为MVC添加服务和中间件. 添加一项服务 添加一个Controller,返回 ViewResult. 添 ...

  8. 在ASP.NET Core 2.0中使用CookieAuthentication

    在ASP.NET Core中关于Security有两个容易混淆的概念一个是Authentication(认证),一个是Authorization(授权).而前者是确定用户是谁的过程,后者是围绕着他们允 ...

  9. 使用Http-Repl工具测试ASP.NET Core 2.2中的Web Api项目

    今天,Visual Studio中没有内置工具来测试WEB API.使用浏览器,只能测试http GET请求.您需要使用Postman,SoapUI,Fiddler或Swagger等第三方工具来执行W ...

随机推荐

  1. Python6大设计原则

    内容总览 六大设计原则都有哪些 一.单一职责原则 二.里氏替换原则 三.依赖倒置原则 四.接口隔离原则 五.迪米特法则 六.开放封闭原则 内容详解 一.单一职责原则 单一职责原则:英文名称是Singl ...

  2. 除了Office和wps,还有什么办公软件比较好用?

    一.不能没有的pdf软件 以下介绍的是PDF(Portable Document Format)常用的阅读.编撰以及其他工具.阅读工具1.Adobe AcrobatReader中文版应用平台:Wind ...

  3. NC nc开发工具java虚拟机参数

        -Dnc.exclude.modules=${FIELD_EX_MODULES} -Dnc.runMode=develop -Dnc.server.location=${FIELD_NC_HO ...

  4. debug,菜鸟必备的求生技能

    突然想写个关于 debug 的文章,来纪念我2天前被自己坑的蠢事…… 前两天,项目的四期送去电科院审查了.因为一些不可描述的原因,我很不喜欢四期.做起来就很烦~临近验收,发现了个比较严重的bug,记录 ...

  5. Matching Networks for One Shot Learning

    1. Introduction In this work, inspired by metric learning based on deep neural features and memory a ...

  6. Mac 电脑设置显示路径

    # 设置 defaults write com.apple.finder _FXShowPosixPathInTitle -bool TRUE;killall Finder # 删除 defaults ...

  7. 安装bazel(syntaxnet依赖工具)

    1.简介   Bazel是一个类似于Make的工具,是Google为其内部软件开发的特点量身定制的工具,如今Google使用它来构建内部大多数的软件.它的功能有诸多亮点: 多语言支持:目前Bazel默 ...

  8. 连接hive

    bin/hiveserver2 nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err & ...

  9. springboot 初始化 web 项目 启动报错。。。一直解决不了

    1. 一个简单的SpringBoot项目,启动时报错信息: ERROR 18688 --- [cat-startStop-1] org.apache.catalina.core.ContainerBa ...

  10. DSP builder安装指南(以9.1为例) 转自http://www.cnblogs.com/sleepy/archive/2011/06/28/2092362.html

    DSP Builder在算法友好的开发环境中帮助设计人员生成DSP设计硬件表征,从而缩短了DSP设计周期.已有的MATLAB函数和Simulink模块可以和Altera DSP Builder模块以及 ...