衣冠楚楚的是一个开源的、轻量级的“微型ORM”支持许多数据库包括SQL Server, MySQL, SQLite, SQL CE和火鸟。通过使用衣冠楚楚的在您的应用程序可以简化数据访问同时确保高性能。在先前的文章中我提供介绍衣冠楚楚的和检查了衣冠楚楚的扩展库。
在本文中,我将展示如何利用短小精悍的对异步操作的支持在ASP。净的核心。
创建一个新的ASP。网络核心Web API project
首先,让我们创建一个ASP。网络核心project and install the necessary packages. Assuming that Visual Studio 2017 is up and running in your system, follow the steps outlined below to create an ASP.Net Core Web API project.
- 启动Visual Studio 2017 IDE。
- 单击File > New >项目。
- 选择“ASP。网络核心Web Application (.Net Core)" from the list of templates displayed.
- 为项目指定一个名称。
- 点击OK以保存项目。
- 选择“API”在“新的. net核心Web应用程序…”窗口。
- 选择版本的ASP。网络核心you would like to use from the drop-down list at the top.
- 取消“启用码头工人支持”并选择“无身份验证”,我们不会使用这些特性。
- 单击OK。
一旦你的新的ASP。网络核心project has been created, right-click on the project in the Solution Explorer window, create a solution folder named ”Models,” and create a new class. Name this class “Author” — this will be our model class that will hold our data. Here is what our Author class should look like.
公开课的作者
{
公共int关键{得到;设置;}
公共字符串FirstName{得到;设置;}
公共字符串LastName{得到;设置;}
}
接下来,让我们构建一个简单的存储库。这是库的接口。
公共接口IAuthorRepository
{
任务<作者> GetByKey (int键);
}
AuthorRepository类实现了IAuthorRepository接口。这是这门课应该乍一看。
公开课AuthorRepository: IAuthorRepository
{
公共异步任务<作者> GetByKey (int键)
{
/ / TODO
}
}
使用短小精悍的创建异步查询
对于异步操作,衣冠楚楚的提供方法,像ExecuteAsync QueryAsync, QueryFirstAsync, QueryFirstOrDefaultAsync, QuerySingleAsync QuerySingleOrDefaultAsync, QueryMultipleAsync。我们将利用QueryAsync方法。
GetByKey方法接受一个关键(作者的主键值),并返回一个作者记录。请注意以下QueryAsync方法的使用。
公共异步任务<作者> GetByKey (int键)
{
使用(IDbConnection连接= new SqlConnection (connectionString))
{
字符串查询= "选择pKey、名、姓从作者pKey = @key”;
如果连接。状态! = ConnectionState.Open)
connection.Open ();
var =结果等待连接。作者QueryAsync < >(查询,新{pKey =键});
返回result.FirstOrDefault ();
}
}
注册这个库的管道,你应该利用ConfigureServices方法如下所示。注意,这个方法将由运行时自动调用。
公共空间ConfigureServices (IServiceCollection服务)
{
服务。AddTransient < IAuthorRepository AuthorRepository > ();
services.AddMvc () .SetCompatibilityVersion (CompatibilityVersion.Version_2_1);
}
因为连接字符串中指定appSettings。json文件在ASP。网络核心applications, we need a way to ensure that this connection string is available to the repository. To achieve this, you can leverage the IConfiguration object available as part of ASP.Net Core. The following code snippet illustrates how to do this.
公开课AuthorRepository: IAuthorRepository
{
私人只读的IConfiguration _config;
私人只读的字符串connectionString =零;
公共AuthorRepository (IConfiguration配置)
{
_config =配置;
connectionString = _config。GetConnectionString (“IDGConnectionString”);
}
/ /其他方法
}
最后,这是控制器类,供您参考。注意作者库都被注入。
(路线(“api /(控制器)”)
(ApiController)
公开课AuthorController: ControllerBase
{
私人只读的IAuthorRepository _authorRepository;
公共AuthorController (IAuthorRepository authorRepository)
{
_authorRepository = authorRepository;
}
(HttpGet)
[路线(“{id}”)]
公共异步任务< ActionResult <作者> > GetByKey (int id)
{
返回等待_authorRepository.GetByKey (id);
}
}
对象关系映射器,又称orm,用于消除之间存在的“阻抗不匹配”编程语言的对象模型和关系数据库的数据模型。衣冠楚楚的是一个简单、灵活、快速、轻量级的ORM由山姆藏红花的堆栈溢出。和衣冠楚楚的是免费开源的。您可以了解更多关于衣冠楚楚的ORM通过阅读我以前的两个短小精悍的文章在这里和在这里。