Skip to content

Commit

Permalink
Supported loop transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
seayxu committed Aug 16, 2018
1 parent 52e3b6f commit 7df0689
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 64 deletions.
38 changes: 23 additions & 15 deletions GodSharp.Data.Dapper.sln
@@ -1,37 +1,45 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.15
VisualStudioVersion = 15.0.27703.2047
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{88A87CD9-82FA-4EE9-8F29-886163298E32}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F58AD8DB-BA8E-4FD4-8114-9C9B9087B5F6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GodSharp.Data.Dapper", "src\GodSharp.Data.Dapper\GodSharp.Data.Dapper.csproj", "{6ED5410E-A945-4B17-AD71-BEC9BB62CA7E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GodSharp.Data.Dapper", "src\GodSharp.Data.Dapper\GodSharp.Data.Dapper.csproj", "{38D326CD-B657-46BE-AC5C-D336EEA2251F}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{F5FE834D-22E0-4CB2-A485-85337CD7D085}"
ProjectSection(SolutionItems) = preProject
appveyor.yml = appveyor.yml
.gitignore = .gitignore
README.md = README.md
EndProjectSection
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{A5633B99-3BD3-4EC1-888D-E5D74D8104EB}"
ProjectSection(SolutionItems) = preProject
appveyor.yml = appveyor.yml
README.md = README.md
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{CD2327D4-61BF-4E96-A096-88BE587EBC29}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GodSharp.Data.Dapper.UnitTest", "test\GodSharp.Data.Dapper.UnitTest\GodSharp.Data.Dapper.UnitTest.csproj", "{3C8DCBF0-B79A-412C-8DDA-9AE86CE5BF93}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6ED5410E-A945-4B17-AD71-BEC9BB62CA7E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6ED5410E-A945-4B17-AD71-BEC9BB62CA7E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6ED5410E-A945-4B17-AD71-BEC9BB62CA7E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6ED5410E-A945-4B17-AD71-BEC9BB62CA7E}.Release|Any CPU.Build.0 = Release|Any CPU
{38D326CD-B657-46BE-AC5C-D336EEA2251F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{38D326CD-B657-46BE-AC5C-D336EEA2251F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{38D326CD-B657-46BE-AC5C-D336EEA2251F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{38D326CD-B657-46BE-AC5C-D336EEA2251F}.Release|Any CPU.Build.0 = Release|Any CPU
{3C8DCBF0-B79A-412C-8DDA-9AE86CE5BF93}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3C8DCBF0-B79A-412C-8DDA-9AE86CE5BF93}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3C8DCBF0-B79A-412C-8DDA-9AE86CE5BF93}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3C8DCBF0-B79A-412C-8DDA-9AE86CE5BF93}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{6ED5410E-A945-4B17-AD71-BEC9BB62CA7E} = {88A87CD9-82FA-4EE9-8F29-886163298E32}
{38D326CD-B657-46BE-AC5C-D336EEA2251F} = {F58AD8DB-BA8E-4FD4-8114-9C9B9087B5F6}
{3C8DCBF0-B79A-412C-8DDA-9AE86CE5BF93} = {CD2327D4-61BF-4E96-A096-88BE587EBC29}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDB1F4C-F82C-460D-9227-282EAD7EE8A0}
SolutionGuid = {FCB36CC9-E169-4B04-913A-F84B79545612}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -14,7 +14,7 @@ DbContext for Dapper.

See [here](https://www.nuget.org/packages/GodSharp.Data.Dapper/).

2. Inherited class `DbContext`.
2. Inherited class `DapperDbContext`.

Use connectionString name in `App.Config` or `Web.config` as constructor parameters.

Expand Down
18 changes: 8 additions & 10 deletions appveyor.yml
@@ -1,14 +1,16 @@
version: 1.0.0.{build}
branches:
only:
- master
- master
- release
- dev
skip_branch_with_pr: true
image: Visual Studio 2017
configuration: Release
platform: Any CPU
clone_folder: c:\projects\GodSharp.Data.Dapper
install:
- ps: dotnet restore -s https://api.nuget.org/v3/index.json
- cmd: nuget restore
nuget:
account_feed: true
project_feed: true
Expand All @@ -21,31 +23,27 @@ test: off
artifacts:
- path: '**\*.nupkg'
name: nuget
test: off
deploy:
- provider: NuGet
server: https://www.nuget.org/api/v2/package
api_key:
secure: s388/c8yQWnGyZ9v9cUDfBe4ud6MFl/ouUEEBruiN5j5+LjSQ4giGKMKuLuvglS1
skip_symbols: true
artifact: /.*\.nupkg/
on:
branch: master
APPVEYOR_REPO_TAG: true
branch: /(release)|(dev)/
- provider: NuGet
server: https://www.myget.org/F/godsharp/api/v2/package
api_key:
secure: CKm5m2qGYNYnh5yDUE2zKfN+JKBmO5xmtG80AP0yni9yPBqDOaRQ48Y/4k8KGo2y
skip_symbols: true
artifact: /.*\.nupkg/
on:
branch: master
APPVEYOR_REPO_TAG: true
branch: /(release)|(dev)/
- provider: GitHub
auth_token:
secure: uCHTzET52dtiexqTKhgQmup+EBGZs1Afw6eBMfvN4PbQ720YaybNd890Y96ISZ0T
artifact: /.*\.nupkg/
draft: true
force_update: true
on:
branch: master
APPVEYOR_REPO_TAG: true
branch: /(release)|(dev)/
@@ -1,13 +1,13 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Threading;
using Dapper;
#if NET35
using System.Linq;
#endif
#if NFX
#if !NETSTANDARD1_3
using GodSharp.Data.Dapper.Extension;

#endif

// ReSharper disable PrivateFieldCanBeConvertedToLocalVariable
Expand All @@ -20,7 +20,7 @@ namespace GodSharp.Data.Dapper
/// Dapper DbContext
/// </summary>
/// <seealso cref="System.IDisposable" />
public class DbContext : IDisposable
public class DapperDbContext : IDisposable
{
#region // Fileds

Expand Down Expand Up @@ -49,15 +49,22 @@ public class DbContext : IDisposable
/// </summary>
private DbConnectionFactory _factory;

/// <summary>
/// The transaction level
/// </summary>
private int _transactionLevel;

#endregion

#region // Constructor methods

/// <summary>
/// Initializes a new instance of the <see cref="DbContext"/> class.
/// Initializes a new instance of the <see cref="DapperDbContext"/> class.
/// </summary>
protected DbContext()
protected DapperDbContext()
{
_transactionLevel = 0;

_factory = new DbConnectionFactory();

// ReSharper disable once VirtualMemberCallInConstructor
Expand All @@ -70,10 +77,10 @@ protected DbContext()
}

/// <summary>
/// Initializes a new instance of the <see cref="DbContext"/> class.
/// Initializes a new instance of the <see cref="DapperDbContext"/> class.
/// </summary>
/// <param name="connectionStringName">Name of the connection string key.</param>
protected DbContext(string connectionStringName)
protected DapperDbContext(string connectionStringName)
{
_factory = new DbConnectionFactory(connectionStringName);

Expand All @@ -98,9 +105,15 @@ protected virtual void OnConfiguration(DbConnectionFactory factory)
/// <param name="il">The il.</param>
protected void BeginTransaction(IsolationLevel? il = null)
{
if (_dbTransaction == null)
int level = Interlocked.Increment(ref _transactionLevel);

if (level == 1 && _dbTransaction != null)
{
throw new InvalidOperationException("transaction exist.");
}
else if (level == 1 && _dbTransaction == null)
{
Open();
Open();

_dbTransaction =
il == null ? _dbConnection.BeginTransaction() : _dbConnection.BeginTransaction(il.Value);
Expand All @@ -114,20 +127,32 @@ protected void BeginTransaction(IsolationLevel? il = null)
/// <returns></returns>
protected int Commit()
{
if (_dbTransaction != null)
int number = 0;

int level = Interlocked.Decrement(ref _transactionLevel);

if (level == 0)
{
if (_hasError)
if (_dbTransaction != null)
{
_dbTransaction.Rollback();
}
else
{
_dbTransaction.Commit();
if (_hasError)
{
_dbTransaction.Rollback();
_affectedRowNumber = -1;
}
else
{
_dbTransaction.Commit();
number = _affectedRowNumber;
}
}
}
else if (level < 0)
{
Reset();

int number = _affectedRowNumber;
Reset();
throw new InvalidOperationException("commit number more than begin transaction number.");
}

return number;
}
Expand Down Expand Up @@ -173,6 +198,7 @@ public void Reset()
_hasError = false;
_dbTransaction = null;
_affectedRowNumber = 0;
_transactionLevel = 0;
}

#endregion
Expand Down Expand Up @@ -625,7 +651,7 @@ protected IEnumerable<TReturn> Query<TReturn>(CommandDefinition command,bool wit
}
#endif

#if WF
#if !NETSTANDARD1_3
/// <summary>
/// This returns a <see cref="DataTable"/>.
/// </summary>
Expand Down Expand Up @@ -1062,7 +1088,7 @@ protected virtual void Dispose(bool disposing)
/// <summary>
/// Dispose
/// </summary>
~DbContext()
~DapperDbContext()
{
Dispose(false);
}
Expand Down
@@ -1,4 +1,4 @@
#if WF
#if !NETSTANDARD1_3
using Dapper;
using System.Data;

Expand Down
31 changes: 14 additions & 17 deletions src/GodSharp.Data.Dapper/GodSharp.Data.Dapper.csproj
@@ -1,30 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyVersion>1.0.0.6</AssemblyVersion>
<AssemblyFileVersion>1.0.0.6</AssemblyFileVersion>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Title>GodSharp.Data.Dapper</Title>
<AssemblyVersion>1.0.0.7</AssemblyVersion>
<AssemblyFileVersion>1.0.0.7</AssemblyFileVersion>
<Version>1.0.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>seayxu</Authors>
<Description>DbContext for Dapper.</Description>
<Copyright>Copyright © GodSharp 2017</Copyright>
<NeutralLanguage>en-US</NeutralLanguage>
<Title>GodSharp.Data.Dapper</Title>
<PackageId>GodSharp.Data.Dapper</PackageId>
<PackageTags>Dapper,GodSharp.Data.Dapper,GodSharp</PackageTags>
<PackageReleaseNotes>Add 'withoutTranTransaction' signature decide whether to use a transaction.</PackageReleaseNotes>
<PackageReleaseNotes>Supported loop transaction.</PackageReleaseNotes>
<Company>GodSharp</Company>
<Authors>seayxu</Authors>
<Description>DbContext for Dapper.</Description>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/godsharp/GodSharp.Data.Dapper.git</RepositoryUrl>
<PackageProjectUrl>https://github.com/godsharp/GodSharp.Data.Dapper</PackageProjectUrl>
<Copyright>Copyright © GodSharp 2017</Copyright>
<PackageIconUrl>https://avatars3.githubusercontent.com/u/26563296</PackageIconUrl>
<PackageLicenseUrl>https://github.com/godsharp/GodSharp.Data.Dapper/blob/master/LICENSE</PackageLicenseUrl>
<Version>1.0.0-preview6</Version>
<EnableDefaultItems>true</EnableDefaultItems>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TargetFrameworks Condition="'$(LibraryFrameworks)'==''">netstandard2.0;netstandard1.3;net451;net45;net40;net35;</TargetFrameworks>
<TargetFrameworks Condition="'$(LibraryFrameworks)'!=''">$(LibraryFrameworks)</TargetFrameworks>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworks>net35;net40;net45;net451;netstandard1.3;netstandard2.0;</TargetFrameworks>
<!--<TargetFrameworks Condition="'$(LibraryFrameworks)'==''">net35;net40;net45;net451;netstandard1.3;netstandard2.0;</TargetFrameworks> -->
<!--<TargetFrameworks Condition="'$(LibraryFrameworks)'!=''">$(LibraryFrameworks)</TargetFrameworks> -->
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='net35'">
<DefineConstants>NFX;NET35;WF;$(AdditionalConstants)</DefineConstants>
Expand All @@ -35,7 +32,7 @@
<PropertyGroup Condition="'$(TargetFramework)'=='net45'">
<DefineConstants>NFX;NET45;WF;$(AdditionalConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='net45'">
<PropertyGroup Condition="'$(TargetFramework)'=='net451'">
<DefineConstants>NFX;NET451;WF;$(AdditionalConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='netstandard1.3'">
Expand Down Expand Up @@ -81,7 +78,7 @@
<PackageReference Include="System.Data.Common" Version="4.3.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="Dapper" Version="1.50.4-alpha1-00070" />
<PackageReference Include="Dapper" Version="1.50.4" />
<PackageReference Include="GodSharp.Data.Common.DbProvider" Version="1.0.0-preview1" />
</ItemGroup>
</Project>
46 changes: 46 additions & 0 deletions test/GodSharp.Data.Dapper.UnitTest/DbContextTest.cs
@@ -0,0 +1,46 @@
using Xunit;

namespace GodSharp.Data.Dapper.UnitTest
{
public class DbContextTest
{
[Fact]
public void Test1()
{
}
}

public class Sample1DapperDbContext : DapperDbContext
{
public Sample1DapperDbContext() : base("")
{
}
}

public class Sample2DapperDbContext : DapperDbContext
{
public Sample2DapperDbContext(string name) : base(name)
{
}
}

public class Sample3DapperDbContext : DapperDbContext
{
protected override void OnConfiguration(DbConnectionFactory factory)
{
base.OnConfiguration(factory);

// SQL Server
factory.Use("System.Data.SqlClient", "Data Source=localhost;Initial Catalog=master;User ID=sa;Password=1234;");

// MySql with Pomelo
factory.Use("Pomelo.Data.MySql", "Data Source=localhost;Initial Catalog=user;User ID=root;Password=1234;");

// Sqlite
factory.Use("Microsoft.Data.Sqlite", "Data Source=data.db;");

// PostgreSql
factory.Use("Npgsql", "Host=localhost;Database=postgres;Username=postgres;Password=1234;");
}
}
}

0 comments on commit 7df0689

Please sign in to comment.