Skip to content

DataAccess.XmlExtension

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

Home / DataAccess

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

CreateSql

Clone this wiki locally