Uploaded image for project: 'iBatis for .NET'
  1. iBatis for .NET
  2. IBATISNET-262

Refactor duplicate code for opening session into DataMapperLocalSessionScope

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • DataMapper 3.0
    • DataMapper 3.0
    • None
    • None

    Description

      Move duplicate code for creating the local session into a class level scope that has access to the private members of DataMapper:

      private class DataMapperLocalSessionScope : IDisposable
      {
      private readonly ISession session;
      private readonly bool isSessionLocal = false;

      public DataMapperLocalSessionScope(DataMapper dataMapper)
      {
      isSessionLocal = false;
      session = dataMapper.sessionStore.CurrentSession;

      if (session == null)

      { session = dataMapper.sessionFactory.OpenSession(); isSessionLocal = true; }

      }

      public ISession Session
      {
      get

      { return session; }

      }

      public void Dispose()
      {
      if (isSessionLocal)

      { session.Close(); }

      }
      }

      to avoid duplicating code across 22 methods. The code in each of the Query... methods is now much shorter:

      public IList QueryForList(string statementId, object parameterObject)
      {
      using (DataMapperLocalSessionScope sessionScope = new DataMapperLocalSessionScope(this))

      { IMappedStatement statement = modelStore.GetMappedStatement(statementId); return statement.ExecuteQueryForList(sessionScope.Session, parameterObject); }

      }

      Attachments

        Activity

          People

            gilles Gilles Bayon
            ron liu ron
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: