#region Apache Notice /***************************************************************************** * $Header: $ * $Revision: 515691 $ * $Date$ * * 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; using System.Data; using Apache.Ibatis.Common.Data; using Apache.Ibatis.Common.Contracts; using Apache.Ibatis.DataMapper.Session.Transaction; namespace Apache.Ibatis.DataMapper.Session { /// /// Default implementation of the contract. /// Create instance. /// public class DefaultSessionFactory : ISessionFactory { [NonSerialized] private IDataSource dataSource = null; [NonSerialized] private readonly ISessionStore sessionStore = null; private readonly ITransactionManager transactionManager = null; /// /// Initializes a new instance of the class. /// /// The data source. /// The session store. /// The transaction manager. public DefaultSessionFactory(IDataSource dataSource, ISessionStore sessionStore, ITransactionManager transactionManager) { Contract.Require.That(dataSource, Is.Not.Null).When("retrieving argument dataSource in DefaultSessionFactory constructor."); Contract.Require.That(sessionStore, Is.Not.Null).When("retrieving argument sessionStore in DefaultSessionFactory constructor."); Contract.Require.That(transactionManager, Is.Not.Null).When("retrieving argument transactionManager in DefaultSessionFactory constructor."); this.dataSource = dataSource; this.sessionStore = sessionStore; this.transactionManager = transactionManager; } #region ISessionFactory Members /// /// Gets the data source. /// /// The data source. public virtual IDataSource DataSource { get { return dataSource; } set { dataSource = value; } } /// /// Open a ISession on the given connection /// /// The connection. /// if set to true [auto close]. /// A session /// /// The ISession is save in the /// public ISession OpenSession(IDbConnection connection, bool autoClose) { ISession session = new DataMapperSession(connection, this, transactionManager, autoClose); sessionStore.Store(session); return session; } /// /// Create a database connection and open a ISession on it. /// /// A session /// /// The ISession is save in the /// public ISession OpenSession() { IDbConnection connection = dataSource.DbProvider.CreateConnection(); connection.ConnectionString = dataSource.ConnectionString; return OpenSession(connection, true); } /// /// Remove the specified session from the /// public void Dispose() { sessionStore.Dispose(); } ///// ///// Gets the transaction manager. ///// ///// The transaction manager. //public ITransactionManager TransactionManager //{ // get { throw new Exception("The method or operation is not implemented."); } //} #endregion } }