Skip to content
Igor Tkachev edited this page May 20, 2016 · 1 revision

Home / DataAccess

Open and Configure

OpenConfig.cs

using System;

using NUnit.Framework;

using BLToolkit.Data;
using BLToolkit.DataAccess;
using BLToolkit.Mapping;

namespace HowTo.DataAccess
{
    [TestFixture]
    public class OpenConfig
    {
        public class Person
        {
            [MapField("PersonID"), PrimaryKey, NonUpdatable]
            public int    ID;

            public string LastName;
            public string FirstName;
            public string MiddleName;
        }

        public abstract class TestAccessor : DataAccessor
        {
            public abstract Person SelectByKey(int id);
            public abstract Person SelectByKey(DbManager db, int id);
        }

        // DbManager is created by DataAccessor.
        //
        [Test]
        public void Test1()
        {
            TestAccessor ta = DataAccessor.CreateInstance<TestAccessor>();

            Person person = ta.SelectByKey(1);

            Assert.IsNotNull(person);
        }

        // DataAccessor takes DbManager as a parameter.
        //
        [Test]
        public void Test2()
        {
            using (DbManager db = new DbManager())
            {
                TestAccessor ta = DataAccessor.CreateInstance<TestAccessor>(db);

                Person person = ta.SelectByKey(1);

                Assert.IsNotNull(person);
            }
        }

        // DataAccessor method takes DbManager as a parameter.
        //
        [Test]
        public void Test3()
        {
            using (DbManager db = new DbManager())
            {
                TestAccessor ta = DataAccessor.CreateInstance<TestAccessor>();

                Person person = ta.SelectByKey(db, 1);

                Assert.IsNotNull(person);
            }
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add
            name             = "DemoConnection"
            connectionString = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"
            providerName     = "System.Data.SqlClient" />
    </connectionStrings>
</configuration>
Clone this wiki locally