You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user-cs@ibatis.apache.org by Morten Schmidt <sc...@knowledgelab.sdu.dk> on 2005/06/13 09:01:43 UTC

Embedded SqlMap.config into .dll

Hi 
 
(Sorry Larry, but I need a more elaborate explanation) 
 
I almost have everything configured and compiled the way I wan't it...
but 1 thing is missing. 
I have all my resources files compiled into my .dll, including
Providers.config. And it's working... 
I wan't to be able to distribute the .dll to my colleagues, but only the
.dll and the Properties.xml file. This means my SqlMap.config needs
embedding too... 
 
I have compiled SqlMap.config into my .dll file, but I can't seem to get
.NET (my webapp) to look for it embedded, instead of looking in the
directory where web.config resides... (root dir) 
 
How do I telle my webapp to look for SqlMap.config embedded in
BudgetModel.DataMapper.dll instead og my root directory? 
 
Larry told me something like: 
 
XmlDocument sqlMapConfigResource = 
Resources.GetEmbeddedResourceAsXmlDocument(SqlMap.Config, 
vendorinquiry); 
 
But I don't quite understand this... Should this be placed in my .dll
file somewhere? in Session_Start / Application_Start? I'm a bit
confused. Is this all the code needed? If it is, why do I need the
XmlDocument sqlMapConfigResource = part? 
 
If I get this working, I'll try to find the time to follow Larry's
request on updating the Wiki with the information I gathered from this
question, and all my previous questions ;o) 
 
/morten 


Re: Embedded SqlMap.config into .dll

Posted by Roberto R <ro...@gmail.com>.
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.DataMapperproject 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. 

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

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

I am going to put my sqlMap.config file and providers.config files into a 
"Config" directory of this "Data" project/assembly and put my
SqlMap.xmlfiles 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

I'll have my sqlMap.config file look something like this for 
providers.config:

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

And my SqlMap.xml files will be configured like this:

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

Now to configure and use my embedded sqlMap.config file also put in 
"Config"...

In this "Data" project/assembly, I can create create my own custom singleton 
for configuring and returning a SqlMapper (grab the Mapper.cs source and 
modify it basically). I'll make use of the IBatisNet.Common.Resources class 
to grab my embedded sqlMap.config file as an XmlDocument instance. I'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(false);
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, I can use this new custom singleton in the rest of my app like this:

//Return all categories (no param object/null passed)
IList categories = MyMapper.Instance().QueryForList("Category.Select",null);

Or:

//Pass a param CategoryId of 1
Category someCategory = MyMapper.Instance().QueryForObject("
Category.SelectOne",1) as Category;

Hope this helps!

Roberto


On 6/13/05, Morten Schmidt <sc...@knowledgelab.sdu.dk> wrote:
> 
>  Hi 
>   (Sorry Larry, but I need a more elaborate explanation) 
>   I almost have everything configured and compiled the way I wan't it... 
> but 1 thing is missing. 
>  I have all my resources files compiled into my .dll, including 
> Providers.config. And it's working... 
>  I wan't to be able to distribute the .dll to my colleagues, but only the 
> .dll and the Properties.xml file. This means my SqlMap.config needs 
> embedding too... 
>   I have compiled SqlMap.config into my .dll file, but I can't seem to get 
> .NET (my webapp) to look for it embedded, instead of looking in the 
> directory where web.config resides... (root dir) 
>   How do I telle my webapp to look for SqlMap.config embedded in 
> BudgetModel.DataMapper.dll instead og my root directory? 
>   Larry told me something like: 
>   XmlDocument sqlMapConfigResource = 
>  Resources.GetEmbeddedResourceAsXmlDocument("SqlMap.Config, 
>  vendorinquiry"); 
>   But I don't quite understand this... Should this be placed in my .dll 
> file somewhere? in Session_Start / Application_Start? I'm a bit confused. Is 
> this all the code needed? If it is, why do I need the "XmlDocument 
> sqlMapConfigResource =" part? 
>   If I get this working, I'll try to find the time to follow Larry's 
> request on updating the Wiki with the information I gathered from this 
> question, and all my previous questions ;o) 
>   /morten 
>