Skip to content

Commit b43e4c8

Browse files
committed
Added Dapper JSON Type Handler example
In json/Dapper-ORM is added new example that maps string[] and object properties to JSON.
1 parent 0eccc61 commit b43e4c8

File tree

5 files changed

+68
-70
lines changed

5 files changed

+68
-70
lines changed

samples/features/json/Dapper-Orm/Controllers/ProductController.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Dapper;
22
using Microsoft.AspNetCore.Mvc;
3+
using System.Collections.Generic;
34
using System.Data;
45
using System.IO;
56
using System.Threading.Tasks;
@@ -96,5 +97,25 @@ from Product
9697
group by Color
9798
FOR JSON PATH");
9899
}
100+
101+
// GET api/Product/Report2
102+
[HttpGet("ORM")]
103+
public List<Product> ORM()
104+
{
105+
var products = connection.Query<Product>(@"select * from Product");
106+
return products.AsList();
107+
}
108+
}
109+
110+
public class Product
111+
{
112+
public int ProductID;
113+
public string Name;
114+
public string Color;
115+
public string Size;
116+
public double Price;
117+
public int Quantity;
118+
public object Data;
119+
public string[] Tags;
99120
}
100121
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net451</TargetFramework>
5+
<PreserveCompilationContext>true</PreserveCompilationContext>
6+
<AssemblyName>Dapper-Orm</AssemblyName>
7+
<OutputType>Exe</OutputType>
8+
<PackageId>Dapper-Orm</PackageId>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<None Include="App.config" />
13+
<None Update="wwwroot\**\*;Views">
14+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
15+
</None>
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="Dapper" Version="1.50.2" />
20+
<PackageReference Include="Dapper.JsonTypeMapping" Version="0.8.2" />
21+
<PackageReference Include="Dapper.Stream" Version="0.1.0" />
22+
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.0.3" />
23+
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.0.2" />
24+
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.0.3" />
25+
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.0.2" />
26+
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.0.2" />
27+
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="1.0.2" />
28+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.0.2" />
29+
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.0.2" />
30+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.0.2" />
31+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.0.2" />
32+
<PackageReference Include="System.Data.Common" Version="4.3.0" />
33+
<PackageReference Include="System.Data.SqlClient" Version="4.3.0" />
34+
</ItemGroup>
35+
36+
<ItemGroup Condition=" '$(TargetFramework)' == 'net451' ">
37+
<Reference Include="System" />
38+
<Reference Include="Microsoft.CSharp" />
39+
</ItemGroup>
40+
41+
</Project>

samples/features/json/Dapper-Orm/ProductCatalog.xproj

Lines changed: 0 additions & 22 deletions
This file was deleted.

samples/features/json/Dapper-Orm/Startup.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Microsoft.AspNetCore.Builder;
1+
using Dapper;
2+
using Microsoft.AspNetCore.Builder;
23
using Microsoft.AspNetCore.Hosting;
34
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.DependencyInjection;
@@ -25,6 +26,10 @@ public Startup(IHostingEnvironment env)
2526
// This method gets called by the runtime. Use this method to add services to the container.
2627
public void ConfigureServices(IServiceCollection services)
2728
{
29+
// Map sting[] and object properties in model to JSON column in database.
30+
SqlMapper.AddTypeHandler(typeof(string[]), new StringArrayJsonMapper());
31+
SqlMapper.AddTypeHandler(typeof(object), new ObjectJsonMapper());
32+
2833
string ConnString = Configuration["ConnectionStrings:ProductCatalog"];
2934
services.AddTransient<IDbConnection>(_ => new SqlConnection(ConnString));
3035

samples/features/json/Dapper-Orm/project.json

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)