ActionName

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
 

ActionName is a logical name of an operation. By default a method name is an action name, but ActionNameAttribute allows overriding this behavior. Actual stored procedure name will be built depending on naming convention which is defined in the GetDefaultSpName method.

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

namespace HowTo.DataAccess
{
    [TestFixture]
    public class ActionName
    {
        public abstract class PersonAccessor : DataAccessor<Person, PersonAccessor>
        {
            // Default action name is 'SelectByKey'.
            // Stored procedure name is 'Person_SelectByKey'.
            //
            public abstract Person SelectByKey(int @id);

            // Explicit action name is 'SelectByName'.
            // Stored procedure name is 'Person_SelectByName'.
            //
            [ActionName("SelectByName")]
            public abstract Person AnyName    (string @firstName, string @lastName);
        }

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

            Person person1 = pa.SelectByKey(1);

            Assert.IsNotNull(person1);

            Person person2 = pa.AnyName(person1.FirstName, person1.LastName);

            Assert.AreEqual(person1.ID, person2.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>
    <appSettings>
        <add
            key   = "ConnectionString"
            value = "Server=.;Database=BLToolkitData;Integrated Security=SSPI"/>
    </appSettings>
</configuration>
Create.sql script
 
© 2010 www.bltoolkit.net
[email protected]