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

Home / DataAccess

ExecuteDictionary.cs

using System;
using System.Collections.Generic;

using NUnit.Framework;

using BLToolkit.Common;
using BLToolkit.DataAccess;
using BLToolkit.Mapping;

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

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

        public abstract class PersonAccessor : DataAccessor<Person>
        {
            // This method uses Person class primary key information.
            //
            [ActionName("SelectAll")]
            public abstract Dictionary<int,Person> GetPersonDictionary1();

            // Define index field explicitly. "ID" is a field name of the Person class.
            //
            [ActionName("SelectAll")]
            [Index("ID")]
            public abstract Dictionary<int,Person> GetPersonDictionary2();

            // Define index field explicitly. "@PersonID" is a recordset field.
            // Note that the '@' symbol enforces the library to read index value
            // from recordset (not from object).
            //
            [ActionName("SelectAll")]
            [Index("@PersonID")]
            public abstract Dictionary<int,Person> GetPersonDictionary3();

            // This method reads a dictionary containing scalar values.
            //
            [SqlQuery("SELECT PersonID, FirstName FROM Person")]
            [Index("PersonID")]
            [ScalarFieldName("FirstName")]
            public abstract Dictionary<int,string> GetPersonNameDictionary();
        }

        [Test]
        public void Test()
        {
            PersonAccessor pa = DataAccessor.CreateInstance<PersonAccessor>();

            // ExecuteDictionary.
            //
            Dictionary<int,Person> dic;

            dic = pa.GetPersonDictionary1();
            dic = pa.GetPersonDictionary2();
            dic = pa.GetPersonDictionary3();

            foreach (int id in dic.Keys)
                Console.WriteLine("{0}: {1} {2}", id, dic[id].FirstName, dic[id].LastName);

            // ExecuteScalarDictionary.
            //
            Dictionary<int,string> sdic = pa.GetPersonNameDictionary();

            foreach (int id in dic.Keys)
                Console.WriteLine("{0}: {1}", id, sdic[id]);
        }
    }

    [TestFixture]
    public class ExecuteDictionary2
    {
        // This example demonstrates how to use a multiple field key.
        //
        public class Person
        {
            [PrimaryKey(1), MapField("PersonID")]
            public int    ID;
            [PrimaryKey(2)]
            public string LastName;

            public string FirstName;
            public string MiddleName;
        }

        public abstract class PersonAccessor : DataAccessor<Person>
        {
            // This method uses Person class primary key information.
            // Note that the key type of the dictionary must be of IndexValue type.
            // It is required if the index consists of more than one element.
            //
            [ActionName("SelectAll")]
            public abstract Dictionary<CompoundValue,Person> GetPersonDictionary();

            // This method reads a dictionary containing scalar values.
            //
            [SqlQuery("SELECT PersonID, LastName, FirstName FROM Person")]
            [Index("PersonID", "LastName")]
            [ScalarFieldName("FirstName")]
            public abstract Dictionary<CompoundValue,string> GetPersonNameDictionary();
        }

        [Test]
        public void Test()
        {
            PersonAccessor pa = DataAccessor.CreateInstance<PersonAccessor>();

            // ExecuteDictionary.
            //
            Dictionary<CompoundValue,Person> dic = pa.GetPersonDictionary();

            foreach (CompoundValue idx in dic.Keys)
                Console.WriteLine("{0}: {1} {2}", dic[idx].ID, dic[idx].FirstName, dic[idx].LastName);

            // ExecuteScalarDictionary.
            //
            Dictionary<CompoundValue,string> sdic = pa.GetPersonNameDictionary();

            string firstName = sdic[new CompoundValue(2, "Testerson")];

            Assert.AreEqual("Tester", firstName);
        }
    }
}

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>

CreateSql

Clone this wiki locally