Commit 40a91918 authored by Jörg B's avatar Jörg B

Added async/await to Dapper.Rainbow. .Net 4.5 Hooray \o/

I kept my hands off Sam's original Code as much as possible, only made
the two major classes partial and added the .Net 4.5 Async other half in
the new project (basically what Dapper NET45 does to Dapper).
parent 5472d2ab
...@@ -31,6 +31,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution ...@@ -31,6 +31,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Dapper.Contrib.nuspec = Dapper.Contrib.nuspec Dapper.Contrib.nuspec = Dapper.Contrib.nuspec
Dapper.EntityFramework.nuspec = Dapper.EntityFramework.nuspec Dapper.EntityFramework.nuspec = Dapper.EntityFramework.nuspec
Dapper.nuspec = Dapper.nuspec Dapper.nuspec = Dapper.nuspec
Dapper.Rainbow.nuspec = Dapper.Rainbow.nuspec
License.txt = License.txt License.txt = License.txt
EndProjectSection EndProjectSection
EndProject EndProject
...@@ -42,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.EntityFramework NET4 ...@@ -42,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.EntityFramework NET4
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.Contrib NET45", "Dapper.Contrib NET45\Dapper.Contrib NET45.csproj", "{302EC82F-A81B-48C5-B653-B5C75D2BD103}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.Contrib NET45", "Dapper.Contrib NET45\Dapper.Contrib NET45.csproj", "{302EC82F-A81B-48C5-B653-B5C75D2BD103}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.Rainbow NET45", "Dapper.Rainbow NET45\Dapper.Rainbow NET45.csproj", "{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
...@@ -182,6 +185,16 @@ Global ...@@ -182,6 +185,16 @@ Global
{302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|Mixed Platforms.Build.0 = Release|Any CPU {302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|x86.ActiveCfg = Release|Any CPU {302EC82F-A81B-48C5-B653-B5C75D2BD103}.Release|x86.ActiveCfg = Release|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Debug|x86.ActiveCfg = Debug|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Release|Any CPU.Build.0 = Release|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
......
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{DB42428F-3C2B-4C9E-9B7A-5E43B53D6613}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Dapper.Rainbow</RootNamespace>
<AssemblyName>Dapper.Rainbow</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;ASYNC</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;ASYNC</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Dapper.Rainbow\Database.cs">
<Link>Database.cs</Link>
</Compile>
<Compile Include="..\Dapper.Rainbow\IgnorePropertyAttribute.cs">
<Link>IgnorePropertyAttribute.cs</Link>
</Compile>
<Compile Include="..\Dapper.Rainbow\Properties\AssemblyInfo.cs">
<Link>Properties\AssemblyInfo.cs</Link>
</Compile>
<Compile Include="..\Dapper.Rainbow\Snapshotter.cs">
<Link>Snapshotter.cs</Link>
</Compile>
<Compile Include="..\Dapper.Rainbow\SqlCompactDatabase.cs">
<Link>SqlCompactDatabase.cs</Link>
</Compile>
<Compile Include="DatabaseAsync.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Dapper NET45\Dapper NET45.csproj">
<Project>{0fff5bc7-0a4b-4d87-835e-4fad70937507}</Project>
<Name>Dapper NET45</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
\ No newline at end of file
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Dapper
{
public abstract partial class Database<TDatabase> : IDisposable where TDatabase : Database<TDatabase>, new()
{
public partial class Table<T, TId>
{
/// <summary>
/// Insert a row into the db asynchronously
/// </summary>
/// <param name="data">Either DynamicParameters or an anonymous type or concrete type</param>
/// <returns></returns>
public virtual async Task<int?> InsertAsync(dynamic data)
{
var o = (object)data;
List<string> paramNames = GetParamNames(o);
paramNames.Remove("Id");
string cols = string.Join(",", paramNames);
string cols_params = string.Join(",", paramNames.Select(p => "@" + p));
var sql = "set nocount on insert " + TableName + " (" + cols + ") values (" + cols_params + ") select cast(scope_identity() as int)";
return (await database.QueryAsync<int?>(sql, o)).Single();
}
/// <summary>
/// Update a record in the DB asynchronously
/// </summary>
/// <param name="id"></param>
/// <param name="data"></param>
/// <returns></returns>
public async Task<int> UpdateAsync(TId id, dynamic data)
{
List<string> paramNames = GetParamNames((object)data);
var builder = new StringBuilder();
builder.Append("update ").Append(TableName).Append(" set ");
builder.AppendLine(string.Join(",", paramNames.Where(n => n != "Id").Select(p => p + "= @" + p)));
builder.Append("where Id = @Id");
DynamicParameters parameters = new DynamicParameters(data);
parameters.Add("Id", id);
return await database.ExecuteAsync(builder.ToString(), parameters);
}
/// <summary>
/// Delete a record for the DB asynchronously
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<bool> DeleteAsync(TId id)
{
return (await database.ExecuteAsync("delete from " + TableName + " where Id = @id", new { id })) > 0;
}
/// <summary>
/// Grab a record with a particular Id from the DB asynchronously
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<T> GetAsync(TId id)
{
return (await database.QueryAsync<T>("select * from " + TableName + " where Id = @id", new { id })).FirstOrDefault();
}
public virtual async Task<T> FirstAsync()
{
return (await database.QueryAsync<T>("select top 1 * from " + TableName)).FirstOrDefault();
}
public async Task<IEnumerable<T>> AllAsync()
{
return await database.QueryAsync<T>("select * from " + TableName);
}
}
public async Task<int> ExecuteAsync(string sql, dynamic param = null)
{
return await connection.ExecuteAsync(sql, param as object, transaction, this.commandTimeout);
}
public async Task<IEnumerable<T>> QueryAsync<T>(string sql, dynamic param = null)
{
return await connection.QueryAsync<T>(sql, param as object, transaction, commandTimeout);
}
public async Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TReturn>(string sql, Func<TFirst, TSecond, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
{
return await connection.QueryAsync(sql, map, param as object, transaction, buffered, splitOn);
}
public async Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TReturn>(string sql, Func<TFirst, TSecond, TThird, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
{
return await connection.QueryAsync(sql, map, param as object, transaction, buffered, splitOn);
}
public async Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TFourth, TReturn>(string sql, Func<TFirst, TSecond, TThird, TFourth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
{
return await connection.QueryAsync(sql, map, param as object, transaction, buffered, splitOn);
}
public async Task<IEnumerable<TReturn>> QueryAsync<TFirst, TSecond, TThird, TFourth, TFifth, TReturn>(string sql, Func<TFirst, TSecond, TThird, TFourth, TFifth, TReturn> map, dynamic param = null, IDbTransaction transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null)
{
return await connection.QueryAsync(sql, map, param as object, transaction, buffered, splitOn);
}
public async Task<IEnumerable<dynamic>> QueryAsync(string sql, dynamic param = null)
{
return await connection.QueryAsync(sql, param as object, transaction);
}
public async Task<SqlMapper.GridReader> QueryMultipleAsync(string sql, dynamic param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
{
return await SqlMapper.QueryMultipleAsync(connection, sql, param, transaction, commandTimeout, commandType);
}
}
}
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Dapper.Rainbow NET45")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Dapper.Rainbow NET45")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e12ca1da-f0a4-4e5b-a353-2d1f24d49831")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<package > <package >
<metadata> <metadata>
<id>Dapper.Rainbow</id> <id>Dapper.Rainbow</id>
<version>0.1.2</version> <version>0.2.0</version>
<title>Dapper.Rainbow</title> <title>Dapper.Rainbow</title>
<authors>Sam Saffron</authors> <authors>Sam Saffron</authors>
<owners>Sam Saffron</owners> <owners>Sam Saffron</owners>
...@@ -18,6 +18,8 @@ ...@@ -18,6 +18,8 @@
Data explorer uses "Rainbow" exclusively for all database access. Data explorer uses "Rainbow" exclusively for all database access.
</summary> </summary>
<releaseNotes> <releaseNotes>
* version 0.2:
.Net 4.5 async/await capability added.
* version 0.1: * version 0.1:
A demo is available at https://gist.github.com/1599013 . A demo is available at https://gist.github.com/1599013 .
The implementation was extracted from http://data.stackexchange.com source at: http://code.google.com/p/stack-exchange-data-explorer/ . The implementation was extracted from http://data.stackexchange.com source at: http://code.google.com/p/stack-exchange-data-explorer/ .
...@@ -26,10 +28,11 @@ ...@@ -26,10 +28,11 @@
<copyright>Copyright Sam Saffron 2012</copyright> <copyright>Copyright Sam Saffron 2012</copyright>
<tags>orm dapper micro-orm</tags> <tags>orm dapper micro-orm</tags>
<dependencies> <dependencies>
<dependency id="Dapper" version="1.8" /> <dependency id="Dapper" version="1.34" />
</dependencies> </dependencies>
</metadata> </metadata>
<files> <files>
<file src="bin\Release\Dapper.Rainbow.dll" target="lib\net40" /> <file src="Dapper.Rainbow NET45\bin\Release\Dapper.Rainbow.*" target="lib\net45" />
<file src="Dapper.Rainbow\bin\Release\Dapper.Rainbow.*" target="lib\net40" />
</files> </files>
</package> </package>
\ No newline at end of file
...@@ -46,11 +46,6 @@ ...@@ -46,11 +46,6 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SqlCompactDatabase.cs" /> <Compile Include="SqlCompactDatabase.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="Dapper.Rainbow.nuspec">
<SubType>Designer</SubType>
</None>
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Dapper NET40\Dapper NET40.csproj"> <ProjectReference Include="..\Dapper NET40\Dapper NET40.csproj">
<Project>{daf737e1-05b5-4189-a5aa-dac6233b64d7}</Project> <Project>{daf737e1-05b5-4189-a5aa-dac6233b64d7}</Project>
......
...@@ -22,9 +22,9 @@ namespace Dapper ...@@ -22,9 +22,9 @@ namespace Dapper
/// A container for a database, assumes all the tables have an Id column named Id /// A container for a database, assumes all the tables have an Id column named Id
/// </summary> /// </summary>
/// <typeparam name="TDatabase"></typeparam> /// <typeparam name="TDatabase"></typeparam>
public abstract class Database<TDatabase> : IDisposable where TDatabase : Database<TDatabase>, new() public abstract partial class Database<TDatabase> : IDisposable where TDatabase : Database<TDatabase>, new()
{ {
public class Table<T, TId> public partial class Table<T, TId>
{ {
internal Database<TDatabase> database; internal Database<TDatabase> database;
internal string tableName; internal string tableName;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment