Skip to content

Latest commit

 

History

History
57 lines (44 loc) · 2 KB

DataAccess.md

File metadata and controls

57 lines (44 loc) · 2 KB

#Data Access by Froggy and Scope

Introduction

Why one more data access layer? The answer is: I use this filosofy by seven years, thats incredibly simple, works and the learning curve is almost none.

There is no penallity of performance, and it is stable and do not have the hell that some technologies bug us.

Details

DbCommandUtil

Simplify the use of DbCommand and DbDataAdapter and try to not compete with both, only facilitate they use.

Scope

Allow reuse of connection and transacions, between call stacks of distinct methods.

Example

[Test]
public void NestedWithTransactionTest()
{
    using (var scope = new Scope(new DAScopeContext(TransactionOption.Required)))
    {
        Assert.IsNotNull(scope.GetDAScopeContext());
        Assert.IsNotNull(scope.GetDAScopeContext().Connection);
        Assert.IsNotNull(scope.GetDAScopeContext().Transaction);
        // Integrated with scope: use the same connection and have automatic 
        // enlistment with local transaction
        var comm = new DbCommandUtil("SELECT * FROM TEST WHERE id = @id");
        comm.AddParameter("@id", DbType.Int32, 5);
        var testDT = comm.GetDataTable();
        NestedLevel1WithTransactionTest(scope);
        scope.Complete();
    }
}

private static void NestedLevel1WithTransactionTest(Scope expected)
{
    using (var scope = new Scope( new DAScopeContext(TransactionOption.Required) ))
    {
        Assert.AreSame(expected, Scope.Current);
        Assert.AreEqual(Scope.Current, scope);
        Assert.IsNotNull(scope.GetDAScopeContext());
        Assert.IsNotNull(scope.GetDAScopeContext().Connection);
        Assert.IsNotNull(scope.GetDAScopeContext().Transaction);
        Assert.AreSame(Scope.Current.GetDAScopeContext(), scope.GetDAScopeContext());
        Assert.AreSame(Scope.Current.GetDAScopeContext().Connection, scope.GetDAScopeContext().Connection);
        Assert.AreSame(Scope.Current.GetDAScopeContext().Transaction, scope.GetDAScopeContext().Transaction);

        scope.Complete();
    }
}