ObjectType

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
 

The ObjectType attribute associates an actual type with the type returned by an abstract method. Note the ActualType attribute has lower priority.

ActualType.cs
using System.Collections.Generic;

using NUnit.Framework;

using BLToolkit.DataAccess;

namespace HowTo.DataAccess
{
    [TestFixture]
    public class ActualType
    {
        public interface IName
        {
            string Name { get; }
        }

        public class NameBase : IName
        {
            private string _name;
            public  string  Name { get { return _name; } set { _name = value; } }
        }

        public class Name1 : NameBase {}
        public class Name2 : NameBase {}

        [ActualType(typeof(IName), typeof(Name1))]
        public abstract class TestAccessor : DataAccessor
        {
            [SqlQuery("SELECT 'John' as Name")]
            public abstract IName GetName1();

            [SqlQuery("SELECT 'John' as Name"), ObjectType(typeof(Name2))]
            public abstract IName GetName2();

            [SqlQuery("SELECT 'John' as Name")]
            public abstract IList<IName> GetName1List();

            [SqlQuery("SELECT 'John' as Name"), ObjectType(typeof(Name2))]
            public abstract IList<IName> GetName2List();

            [SqlQuery("SELECT 1 as ID, 'John' as Name"), Index("@ID")]
            public abstract IDictionary<int, IName> GetName1Dictionary();

            [SqlQuery("SELECT 1 as ID, 'John' as Name"), Index("@ID"), ObjectType(typeof(Name2))]
            public abstract IDictionary<int, IName> GetName2Dictionary();
        }

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

            Assert.IsTrue(ta.GetName1()              is Name1);
            Assert.IsTrue(ta.GetName2()              is Name2);
            Assert.IsTrue(ta.GetName1List()[0]       is Name1);
            Assert.IsTrue(ta.GetName2List()[0]       is Name2);
            Assert.IsTrue(ta.GetName1Dictionary()[1] is Name1);
            Assert.IsTrue(ta.GetName2Dictionary()[1] is Name2);
        }
    }
}
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]