XmlExtension

BLToolkit.NET

Business Logic Toolkit for .NET www.bltoolkit.net
 

BLToolkit supports different ways to provide metadata information for business objects. One of the ways is using attributes. Another one is XmlExtension. XmlExtension is a simple xml file where you can specify a type and its attributes.

XmlExtension.cs
using NUnit.Framework;

using BLToolkit.DataAccess;
using BLToolkit.Mapping;
using BLToolkit.Reflection.Extension;

namespace HowTo.DataAccess
{
    [TestFixture]
    public class XmlExtension
    {
        public class MyPersonObject
        {
            [MapField("PersonID")]
            public int    ID;

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

        [Test]
        public void Test()
        {
            SqlQuery<MyPersonObject> query = new SqlQuery<MyPersonObject>();

            query.Extensions = TypeExtension.GetExtensions("XmlExtension.xml");

            MyPersonObject person = query.SelectByKey(1);

            Assert.IsNotNull(person);
        }
    }
}
XmlExtension.xml
<?xml version="1.0" encoding="utf-8" ?>
<Types
    xmlns    = "urn:schemas-bltoolkit-net:typeext"
    xmlns:da = "urn:schemas-bltoolkit-net:dataaccess">

    <Type Name="MyPersonObject" da:TableName="Person">
        <Member Name="ID" da:PrimaryKey="1" da:NonUpdatable="1" />
    </Type>
</Types>
DataAccessor.SelectByKetSql method generates and executes the following SQL statement:
SELECT
    [MiddleName],
    [PersonID],
    [LastName],
    [FirstName]
FROM
    [Person]
WHERE
    [PersonID] = @PersonID
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]