Dashboard > iBATIS DataMapper > Home > Frequently Asked Questions > How do I use an embedded sqlMap.config file
How do I use an embedded sqlMap.config file
Added by Roberto Rabe, last edited by Ted Husted on Sep 15, 2005  (view change)
Labels: 
(None)


The easy way of using your sqlMap.config file with the DataMapper

The really basic way to configure and use the DataMapper is to use the Mapper singleton provided by the framework. If you have the DataMapper source, the code for this singleton is found in the IBatisNet.DataMapper project directory and is named Mapper.cs.

You use the singleton by calling its Mapper.Instance() method. This method gets and returns a SqlMapper instance. The SqlMapper instance is configured and built by the framework through the DomSqlMapBuilder class by looking in the same directory of your web.config or app.config file for a file called sqlMap.config.

Using an embedded sqlMap.config file

Now, to use an embedded sqlMap.config file, you will need to create your own class to configure and build a SqlMapper instance. This is required since the Mapper singleton provided by the framework doesn't look for an embedded sqlMap.config file. You can do this by creating a singleton in your app that looks very much like the Mapper singleton previously mentioned.

For example, let's say we have an app that has its own "Data" project/assembly where we will embed our sqlMap.config file, providers.config file, and SqlMap.xml files. The fully qualified name of this assembly is MyApp.Data.

We are going to put our sqlMap.config file and providers.config files into a "Config" directory of this "Data" project/assembly and put our SqlMap.xml files into a "Maps" directory of this "Data" project/assembly:

/MyApp (solution directory)
    ../MyApp.Data (project directory)
    ..../Config
    ......sqlMap.config
    ......providers.config
    ..../Maps
    ......Category.xml
    ......Product.xml
    ......Suppliers.xml
    

We'll have our sqlMap.config file look something like this for providers.config:

...snip...
    <providers embedded="Config.providers.config, MyApp.Data"/>
    ...snip...

And our SqlMap.xml files will be configured in our sqlMap.config file like this:

...snip...
    <sqlMaps>
    <sqlMap embedded="Maps.Category.xml, MyApp.Data"/>
    <sqlMap embedded="Maps.Product.xml, MyApp.Data"/>
    <sqlMap embedded="Maps.Supplier.xml, MyApp.Data"/>
    </sqlMaps>
    ...snip...

A custom Mapper singleton and SqlMapper configuration with the DomSqlMapBuilder

Now to configure a SqlMapper using our embedded sqlMap.config file that was also put into "Config"...

In this "Data" project/assembly, we can create create our own custom singleton for configuring and returning a SqlMapper (simply grab the Mapper.cs source and modify it). We'll make use of the IBatisNet.Common.Resources class to load our embedded sqlMap.config file as an XmlDocument instance. We'll then pass that XmlDocument instance to the DataMapper framework's DomSqlMapBuilder class for configuring and building a SqlMapper instance.

You'll want to look at the InitMapper() method in the following code example:

using System.Xml;
    using IBatisNet.Common.Utilities;
    using IBatisNet.DataMapper;
    using IBatisNet.DataMapper.Configuration;
    
    namespace MyApp.Data
    {
    /// <summary>
        /// A singleton class to access the SqlMapper defined by the SqlMap.Config
        /// </summary>
        public class MyMapper
    {
    #region Fields
    private static volatile SqlMapper _Mapper = null;
    #endregion
    
    /// <summary>
            /// static Configure constructor that can be
            /// used for callback
            /// </summary>
            /// <param name="obj"></param>
            protected static void Configure(object obj)
    {
    _Mapper = null;
    }
    
    /// <summary>
            /// Init the 'default' SqlMapper defined by the SqlMap.Config file.
            /// </summary>
            protected static void InitMapper()
    {
    DomSqlMapBuilder builder = new DomSqlMapBuilder();
    XmlDocument sqlMapConfig = Resources.GetEmbeddedResourceAsXmlDocument("Config.sqlMap.config,MyApp.Data");
    _Mapper = builder.Configure(sqlMapConfig);
    }
    
    /// <summary>
            /// Get the instance of the SqlMapper defined by the SqlMap.Config file.
            /// </summary>
            /// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>
            public static SqlMapper Instance()
    {
    if (_Mapper == null)
    {
    lock (typeof (SqlMapper))
    {
    if (_Mapper == null) // double-check
                        {
    InitMapper();
    }
    }
    }
    return _Mapper;
    }
    
    /// <summary>
            /// Get the instance of the SqlMapper defined by the SqlMap.Config file. (Convenience form of Instance method.)
            /// </summary>
            /// <returns>A SqlMapper initalized via the SqlMap.Config file.</returns>
            public static SqlMapper Get()
    {
    return Instance();
    }
    }
    }

Now, we can use this new custom singleton in the rest of our app like this:

// Return all categories (no param object/null passed).
    // useStatementNamespaces is set to true in this example!
    IList categories = MyMapper.Instance().QueryForList("Category.Select",null);

Or:

// Pass a param CategoryId of 1.
    // useStatementNamespaces is set to true in this example!
    Category someCategory = MyMapper.Instance().QueryForObject("Category.SelectOne",1) as Category;

Site running on a free Atlassian Confluence Open Source Project License granted to OSS. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators