ScalarFieldName

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
 

If your method returns a dictionary of scalar values, you will have to specify the name or index of the field used to populate the scalar list. The ScalarFieldName attribute allows you to do that.

ScalarFieldName.cs
using System;
using System.Collections.Generic;

using NUnit.Framework;

using BLToolkit.DataAccess;

namespace HowTo.DataAccess
{
    [TestFixture]
    public class ScalarFieldName
    {
        public abstract class TestAccessor : DataAccessor
        {
            [SqlQuery("SELECT PersonID, FirstName FROM Person")]
            [Index("PersonID")]
            [ScalarFieldName("FirstName")]
            public abstract Dictionary<int, string> GetPersonNameDictionary();
        }

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

            IDictionary<int, string> dic = pa.GetPersonNameDictionary();

            Assert.AreEqual("John", dic[1]);
        }
    }
}
Person.cs
using System;

using BLToolkit.DataAccess;
using BLToolkit.Mapping;

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

        public string LastName;
        public string FirstName;
        public string MiddleName;
        public Gender Gender;
    }
}
Gender.cs
using System;

using BLToolkit.Mapping;

namespace HowTo.DataAccess
{
    public enum Gender
    {
        [MapValue("F")] Female,
        [MapValue("M")] Male,
        [MapValue("U")] Unknown,
        [MapValue("O")] Other
    }
}
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add
            key   = "ConnectionString"
            value = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"/>
    </appSettings>
</configuration>
Create.sql script
 
© 2010 www.bltoolkit.net
[email protected]