项目作者: kubagdynia

项目描述 :
.NET Core example of using Dapper with the custom Xml and Json mappers
高级语言: C#
项目地址: git://github.com/kubagdynia/DapperMappers.git
创建时间: 2019-12-14T07:14:30Z
项目社区:https://github.com/kubagdynia/DapperMappers

开源协议:MIT License

下载


DapperMappers

CI

An example of using Dapper with the custom Xml and Json mappers. Can be used to serialize and deserialize objects by Dapper.

Project structure

  • DapperMappers.Api - Sample REST API that uses serialization and deserialization to XML (uses the MS SQL Server Express database)
  • DapperMappers.Domain - Sample domain used by the REST API
  • DapperMappers.Domain.Tests - Domain tests using SQLite database
  • DbConnectionExtensions - DBConnection extension

How to use

  • Create a class that implements IXmlObjectType or IJsonObjectType interface
    ```csharp
    public class Book
    {
    public long Id { get; set; }
    public string Title { get; set; }
    public BookDescription Description { get; set; }
    }

public class BookDescription : IXmlObjectType
{
public Learn Learn { get; set; }
public string About { get; set; }
public Features Features { get; set; }
}

public class Learn
{
public List Points { get; set; }
}

public class Features
{
public List Points { get; set; }
}

  1. - Register these new classes in Startup.cs
  2. ```csharp
  3. services.RegisterDapperCustomTypeHandlers(typeof(Book).Assembly);
  • Create a table in the database that contains a column of the XML type
    1. CREATE TABLE [dbo].[Books](
    2. [Id] bigint IDENTITY(1,1) NOT NULL,
    3. [Title] nvarchar(200) NOT NULL,
    4. [Description] xml NULL
    5. CONSTRAINT [PK_Books] PRIMARY KEY CLUSTERED
    6. (
    7. [Id] ASC
    8. )
    9. )
  • Use Dapper to save object data in the database
    1. public async Task SaveBook(Book book)
    2. {
    3. using (var conn = _connectionFactory.Connection())
    4. {
    5. await conn.ExecuteAsync(_@"INSERT INTO Books (Title, Description) VALUES (@Title, @Description)", book);
    6. }
    7. }

How to Run test REST API

  • Download and install .NET 8.0 SDK
  • Download and install MS SQL Server Express
  • Create an empty database called BookDB and run CreateDapperMappersDbSQLServer.sql script
  • Clone or download source code
    1. git clone https://github.com/kubagdynia/DapperMappers.git
  • Set a database connection string in the API project in the appsettings.json
    1. {
    2. "ConnectionStrings": {
    3. "DefaultConnection": "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=BookDB;Integrated Security=True;MultipleActiveResultSets=True;"
    4. }
    5. }
  • If you are running from IDE set the Startup Item to the DapperMappers.API project, not IIS Express
  • Running the API project from the command line
    1. dotnet run --project .\DapperMappers\DapperMappers.Api\
  • Open the API documentation in your browser
    1. https://localhost:5001/swagger

How to Test

Every commit or pull request is built and tested on the Continuous Integration system.

To test locally:

  • Download and install .NET 8.0 SDK
  • Clone or download source code
    1. git clone https://github.com/kubagdynia/DapperMappers.git
  • Start tests from the command line
    1. dotnet test ./DapperMappers/

Technologies

List of technologies, frameworks and libraries used for implementation:

License

This project is licensed under the MIT License.