3 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 // This facility was a contribution kindly
20 // donated by Gilles Bayon <gilles.bayon@gmail.com>
26 namespace Castle
.Facilities
.IBatisNetIntegration
29 using System
.Configuration
;
30 using System
.Reflection
;
32 using Castle
.Core
.Configuration
;
33 using Castle
.MicroKernel
.Facilities
;
34 using Castle
.Services
.Transaction
;
35 using IBatisNet
.Common
.Logging
;
36 using IBatisNet
.DataMapper
;
38 public class IBatisNetFacility
: AbstractFacility
40 public static readonly String MAPPER_CONFIG_FILE
= "_IBATIS_MAPPER_CONFIG_FILE_";
41 public static readonly String MAPPER_CONFIG_EMBEDDED
= "_IBATIS_MAPPER_CONFIG_EMBEDDED_";
42 public static readonly String MAPPER_CONFIG_CONNECTION_STRING
= "_IBATIS_MAPPER_CONFIG_CONNECTIONSTRING_";
44 private static readonly ILog _logger
= LogManager
.GetLogger(MethodBase
.GetCurrentMethod().DeclaringType
);
46 public IBatisNetFacility()
50 #region IFacility Members
52 protected override void Init()
54 if (FacilityConfig
== null)
56 String message
= "The IBatisNetFacility requires an external configuration";
58 throw new ConfigurationErrorsException(message
);
61 Kernel
.ComponentModelBuilder
.AddContributor(new AutomaticSessionInspector());
62 Kernel
.AddComponent("IBatis.session.interceptor", typeof(AutomaticSessionInterceptor
));
63 Kernel
.AddComponent("IBatis.transaction.manager", typeof(ITransactionManager
), typeof(DefaultTransactionManager
));
67 foreach(IConfiguration factoryConfig
in FacilityConfig
.Children
)
69 if (factoryConfig
.Name
== "sqlMap")
71 ConfigureFactory(factoryConfig
);
78 String message
= "You need to configure at least one sqlMap for IBatisNetFacility";
80 throw new ConfigurationErrorsException(message
);
86 private void ConfigureFactory(IConfiguration config
)
88 String id
= config
.Attributes
["id"];
89 if (id
== string.Empty
)
91 String message
= "The IBatisNetFacility requires each SqlMapper to have an ID.";
93 throw new ConfigurationErrorsException(message
);
97 if (_logger
.IsDebugEnabled
)
99 _logger
.Debug(string.Format("[{0}] was specified as the SqlMapper ID.", id
));
103 String fileName
= config
.Attributes
["config"];
104 if (fileName
== String
.Empty
)
106 if (_logger
.IsDebugEnabled
)
108 _logger
.Debug("No filename was specified, using [sqlMap.config].");
110 fileName
= "sqlMap.config"; // default name
113 String connectionString
= config
.Attributes
["connectionString"];
115 bool isEmbedded
= false;
116 String embedded
= config
.Attributes
["embedded"];
117 if (embedded
!= null)
121 isEmbedded
= Convert
.ToBoolean(embedded
);
122 if (_logger
.IsDebugEnabled
)
124 _logger
.Debug("The SqlMap.config was set to embedded.");
129 if (_logger
.IsWarnEnabled
)
133 "The SqlMap.config had a value set for embedded, [{0}], but it was not able to parsed as a Boolean.",
134 embedded
.ToString()), ex
);
140 ComponentModel model
= new ComponentModel(id
, typeof(ISqlMapper
), null);
141 model
.ExtendedProperties
.Add(MAPPER_CONFIG_FILE
, fileName
);
142 model
.ExtendedProperties
.Add(MAPPER_CONFIG_EMBEDDED
, isEmbedded
);
143 model
.ExtendedProperties
.Add(MAPPER_CONFIG_CONNECTION_STRING
, connectionString
);
144 model
.LifestyleType
= LifestyleType
.Singleton
;
145 model
.CustomComponentActivator
= typeof(SqlMapActivator
);
147 Kernel
.AddCustomComponent(model
);