Created
March 23, 2012 12:18
-
-
Save nakamura-to/2170174 to your computer and use it in GitHub Desktop.
Soma DbTransaction Usage Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Data.Common; | |
using System.Threading; | |
using Soma.Core; | |
namespace DbTransactionExample | |
{ | |
public static class LocalDbExtensions | |
{ | |
private static readonly ThreadLocal<DbTransaction> ThreadLocal = new ThreadLocal<DbTransaction>(); | |
public static void Begin(this LocalDb db, DbConnection con) | |
{ | |
con.Open(); | |
ThreadLocal.Value = con.BeginTransaction(); | |
} | |
public static void Commit(this LocalDb db, DbConnection con) | |
{ | |
ThreadLocal.Value.Commit(); | |
} | |
public static void Rollback(this LocalDb db, DbConnection con) | |
{ | |
ThreadLocal.Value.Rollback(); | |
} | |
public static void SetDbTransaction(DbCommand command) | |
{ | |
command.Transaction = ThreadLocal.Value; | |
} | |
} | |
internal class MyCommandObserver : ICommandObserver | |
{ | |
public void NotifyExecuting(DbCommand command, PreparedStatement statement, out object userState) | |
{ | |
LocalDbExtensions.SetDbTransaction(command); | |
userState = null; | |
} | |
public void NotifyExecuted(DbCommand command, PreparedStatement statement, object userState) | |
{ | |
} | |
} | |
internal class MyConfig : MsSqlConfig | |
{ | |
private static readonly ICommandObserver MyCommandObserver = new MyCommandObserver(); | |
public override string ConnectionString | |
{ | |
get { return @"Data Source=.\SQLEXPRESS;Initial Catalog=Soma.Tutorial;Integrated Security=True"; } | |
} | |
public override ICommandObserver CommandObserver | |
{ | |
get | |
{ | |
return MyCommandObserver; | |
} | |
} | |
} | |
internal class Employee | |
{ | |
[Id(IdKind.Identity)] | |
public int EmployeeId { get; set; } | |
public string EmployeeName { get; set; } | |
public int DepartmentId { get; set; } | |
[Version] | |
public int VersionNo { get; set; } | |
public override string ToString() | |
{ | |
const string fmt = "EmployeeId: {0}, EmployeeName: {1}, DepartmentId: {2}, VersionNo: {3}"; | |
return string.Format(fmt, EmployeeId, EmployeeName, DepartmentId, VersionNo); | |
} | |
} | |
internal class Program | |
{ | |
private static void Main() | |
{ | |
var db = new LocalDb(new MyConfig()); | |
using (var con = db.CreateConnection()) | |
{ | |
db.Begin(con); | |
var emp = db.Find<Employee>(con, 1); | |
Console.WriteLine("FOUND ENTITY : \n{0}\n", emp); | |
emp.EmployeeName = "Hoge"; | |
db.Update(con, emp); | |
Console.WriteLine("UPDATED ENTITY : \n{0}\n", emp); | |
db.Delete(con, emp); | |
Console.WriteLine("DELETED ENTITY : \n{0}\n", emp); | |
db.Rollback(con); | |
} | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment