#region Apache Notice
/*****************************************************************************
* $Revision: 408099 $
* $LastChangedDate$
* $LastChangedBy$
*
* iBATIS.NET Data Mapper
* Copyright (C) 2008/2005 - The Apache Software Foundation
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
********************************************************************************/
#endregion
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using Apache.Ibatis.Common.Utilities;
using Apache.Ibatis.DataMapper.Data;
using Apache.Ibatis.DataMapper.DataExchange;
using Apache.Ibatis.DataMapper.Exceptions;
using Apache.Ibatis.DataMapper.MappedStatements;
using Apache.Ibatis.DataMapper.Model.Cache;
using Apache.Ibatis.DataMapper.Model.ParameterMapping;
using Apache.Ibatis.DataMapper.Model.ResultMapping;
using Apache.Ibatis.DataMapper.Session;
namespace Apache.Ibatis.DataMapper.Model
{
///
/// The default implementation of contract.
///
[DebuggerDisplay("DefaultModelStore: {Id}")]
public class DefaultModelStore : IModelStore
{
private IDataMapper dataMapper = null;
private ISessionFactory sessionFactory = null;
private ISessionStore sessionStore = null;
private DataExchangeFactory dataExchangeFactory = null;
private readonly IDictionary resultMaps = new Dictionary();
private readonly IDictionary parameterMaps = new Dictionary();
private readonly IDictionary cacheModels = new Dictionary();
private readonly IDictionary mappedStatements = new Dictionary();
private readonly DBHelperParameterCache dbHelperParameterCache = new DBHelperParameterCache();
private readonly string id = string.Empty;
///
/// Initializes a new instance of the class.
///
public DefaultModelStore()
{
id = HashCodeProvider.GetIdentityHashCode(this).ToString();
}
#region IModelStore Members
///
/// Name used to identify the
///
///
public string Id { get { return id; } }
///
/// Gets or sets the data mapper.
///
/// The data mapper.
public IDataMapper DataMapper
{
get { return dataMapper; }
set { dataMapper = value; }
}
///
/// Gets or sets the session store.
///
/// The session store.
public ISessionStore SessionStore
{
get { return sessionStore; }
set { sessionStore = value; }
}
///
/// Gets the session factory.
///
/// The session factory.
public ISessionFactory SessionFactory
{
get { return sessionFactory; }
set { sessionFactory = value; }
}
///
/// Gets the DB helper parameter cache.
///
/// The DB helper parameter cache.
public DBHelperParameterCache DBHelperParameterCache
{
get { return dbHelperParameterCache; }
}
///
/// Gets or sets the data exchange factory.
///
/// The data exchange factory.
public DataExchangeFactory DataExchangeFactory
{
get { return dataExchangeFactory; }
set { dataExchangeFactory = value; }
}
///
/// Gets a ResultMap by Id
///
/// The ResultMap id.
/// The ResultMap
public IResultMap GetResultMap(string resultMapId)
{
if (resultMaps.ContainsKey(resultMapId) == false)
{
throw new DataMapperException("The DataMapper does not contain an ResultMap named " + resultMapId);
}
return resultMaps[resultMapId];
}
///
/// Adds a (named) ResultMap
///
/// The ResultMap to add
public void AddResultMap(IResultMap resultMap)
{
if (resultMaps.ContainsKey(resultMap.Id))
{
throw new DataMapperException("The DataMapper already contains an ResultMap named " + resultMap.Id);
}
resultMaps.Add(resultMap.Id, resultMap);
}
///
/// Get a ParameterMap by id
///
/// The id of the ParameterMap
/// The ParameterMap
public ParameterMap GetParameterMap(string parameterMapId)
{
if (!parameterMaps.ContainsKey(parameterMapId))
{
throw new DataMapperException("The DataMapper does not contain an ParameterMap named " + parameterMapId + ". ");
}
return parameterMaps[parameterMapId];
}
///
/// Adds a (named) ParameterMap.
///
/// the ParameterMap to add
public void AddParameterMap(ParameterMap parameterMap)
{
if (parameterMaps.ContainsKey(parameterMap.Id))
{
throw new DataMapperException("The DataMapper already contains an ParameterMap named " + parameterMap.Id);
}
parameterMaps.Add(parameterMap.Id, parameterMap);
}
///
/// Gets a MappedStatement by name
///
/// The id of the statement
/// The MappedStatement
public IMappedStatement GetMappedStatement(string mappedStatementId)
{
if (!mappedStatements.ContainsKey(mappedStatementId))
{
throw new DataMapperException("The DataMapper does not contain a MappedStatement named " + mappedStatementId);
}
return mappedStatements[mappedStatementId];
}
///
/// Adds a (named) MappedStatement.
///
/// The statement to add
public void AddMappedStatement(IMappedStatement mappedStatement)
{
if (mappedStatements.ContainsKey(mappedStatement.Id))
{
throw new DataMapperException("The DataMapper already contains a MappedStatement named " + mappedStatement.Id);
}
mappedStatements.Add(mappedStatement.Id, mappedStatement);
}
///
/// Gets a cache model by id
///
/// The id of the cache model
/// The cache model
public CacheModel GetCacheModel(string cacheModelId)
{
if (!cacheModels.ContainsKey(cacheModelId))
{
throw new DataMapperException("The DataMapper does not contain a CacheModel named " + cacheModelId);
}
return cacheModels[cacheModelId];
}
///
/// Adds a (named) cache model.
///
/// The cache model.
public void AddCacheModel(CacheModel cacheModel)
{
if (cacheModels.ContainsKey(cacheModel.Id))
{
throw new DataMapperException("The DataMapper already contains a CacheModel named " + cacheModel.Id);
}
cacheModels.Add(cacheModel.Id, cacheModel);
}
///
/// Flushes all cached objects that belong to this
///
public void FlushCaches()
{
foreach(CacheModel cacheModel in cacheModels.Values)
{
cacheModel.Cache.Clear();
}
}
#endregion
///
/// Returns a that represents the current .
///
///
/// A that represents the current .
///
public override string ToString()
{
StringBuilder builder = new StringBuilder();
int level = 1;
builder.AppendLine("DataSource:");
builder.AppendLine(string.Empty.PadLeft(level * 3, ' ') + sessionFactory.DataSource);
builder.AppendLine(string.Empty);
builder.AppendLine("CacheModels (" + cacheModels.Count + "):");
IEnumerator caches = cacheModels.Values.GetEnumerator();
while (caches.MoveNext())
{
builder.AppendLine(string.Empty.PadLeft(level * 3, ' ') + caches.Current.Id + "/" + caches.Current.Cache);
}
builder.AppendLine(string.Empty);
builder.AppendLine("ResultMaps (" + resultMaps.Count + "):");
IEnumerator rMaps = resultMaps.Values.GetEnumerator();
while (rMaps.MoveNext())
{
builder.AppendLine(string.Empty.PadLeft(level * 3, ' ') + rMaps.Current.Id + "/" + rMaps.Current.Class.Name);
for (int i = 0; i < rMaps.Current.Parameters.Count; i++)
{
builder.AppendLine(string.Empty.PadLeft(level * 2 * 3, ' ') + " Argument: "+(rMaps.Current.Parameters[i]).ArgumentName);
}
for (int i = 0; i < rMaps.Current.Properties.Count; i++ )
{
builder.AppendLine(string.Empty.PadLeft(level * 2 * 3, ' ') + " Property: " + rMaps.Current.Properties[i].PropertyName);
}
}
builder.AppendLine(string.Empty);
builder.AppendLine("ParameterMaps (" + parameterMaps.Count + "):");
IEnumerator pMaps = parameterMaps.Values.GetEnumerator();
while (pMaps.MoveNext())
{
builder.AppendLine(string.Empty.PadLeft(level * 3, ' ') + pMaps.Current.Id + "/" + pMaps.Current.Class.Name);
for (int i = 0; i < pMaps.Current.Properties.Count; i++)
{
builder.AppendLine(string.Empty.PadLeft(level * 2 * 3, ' ') + " Property: " + pMaps.Current.Properties[i].PropertyName);
}
}
builder.AppendLine(string.Empty);
builder.AppendLine("MappedStatements (" + mappedStatements.Count + "):");
IEnumerator statements = mappedStatements.Values.GetEnumerator();
while (statements.MoveNext())
{
builder.AppendLine(string.Empty.PadLeft(level * 3, ' ') + statements.Current.Id);
}
builder.AppendLine(string.Empty);
return builder.ToString();
}
}
}