Skip to content

DataAccess.Direction

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

Home / DataAccess

The Direction attributes allow controlling mapping from/to a business object. The following table contains brief description of the direction attributes:

Attribute Description
Output Provided members are output parameters of the query.
InputOutput Provided members are input/output parameters of the query.
Ignore Provided members are excluded from mapping.
ReturnValue Provided member is mapped to return value.

Direction.cs

using System;
using NUnit.Framework;
using BLToolkit.DataAccess;

namespace HowTo.DataAccess
{
    [TestFixture]
    public class DirectionTest
    {
        public abstract class PersonAccessor : DataAccessor
        {
            [SprocName("Person_Insert_OutputParameter")]
            public abstract void Insert_OutputParameter([Direction.Output("PERSONID")] Person p);

            [SprocName("Scalar_ReturnParameter")]
            public abstract void Insert_ReturnParameter(
                [Direction.ReturnValue("@PersonID"),
                 Direction.Ignore("PersonID", "FirstName", "LastName", "MiddleName", "Gender")] Person p);

            [SprocName("Scalar_ReturnParameter")]
            public abstract void Insert_ReturnParameter2(
                [Direction.ReturnValue("ID"),
                 Direction.Ignore("PersonID", "FirstName", "LastName", "MiddleName", "Gender")] Person p);
        }

        PersonAccessor Accessor
        {
            get { return DataAccessor.CreateInstance<PersonAccessor>(); }
        }

        [Test]
        public void TestOutputParameter()
        {
            Person p = new Person { FirstName = "Crazy", LastName = "Frog", Gender = Gender.Other };

            Accessor.Insert_OutputParameter(p);

            Assert.IsTrue(p.ID > 0);

            new SprocQuery().Delete(p);
        }

        [Test]
        public void TestReturnParameter()
        {
            Person p = new Person();

            Accessor.Insert_ReturnParameter(p);

            Assert.AreEqual(12345, p.ID);
        }

        [Test]
        public void TestReturnParameter2()
        {
            Person p = new Person();

            Accessor.Insert_ReturnParameter2(p);

            Assert.AreEqual(12345, p.ID);
        }
    }
}

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>
    <connectionStrings>
        <add
            name             = "DemoConnection"
            connectionString = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"
            providerName     = "System.Data.SqlClient" />
    </connectionStrings>
</configuration>

CreateSql

Clone this wiki locally