DBActionTemplate.cs :  » Persistence-Frameworks » Data-Holder » DataHolder » DataPersistence » Template » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » Data Holder 
Data Holder » DataHolder » DataPersistence » Template » DBActionTemplate.cs
/*
 * Namespace Summary
 * Copyright (C) 2005+ Bogdan Damian Constantin
 * E-Mail: damianbcpetro@gmail.com
 * WEB: http://www.sourceforge.net/projects/dataholder
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License 2.1 or later, as
 * published by the Free Software Foundation. See the included License.txt
 * or http://www.gnu.org/copyleft/lesser.html for details.
 *
 */

using System;
using System.Data;
using DataHolder.Containers;
using DataHolder.DataPersistence;
using DataHolder.DataPersistence.DBAProvider;

namespace DataHolder.DataPersistence.Template{
  /// <summary>
  /// This is an abstract class to be used in order to save or load data
  /// </summary>
  public class GeneralDBActionTemplate : IDisposable
  {
    protected IDbConnection Connection;
    protected IDbTransaction Transaction;
    protected CommandProvider l_CommandProvider;
    protected DatabasePersistor l_DBPersistor;

    protected virtual void OpenConnection()
    {
      Connection.Open();
    }

    protected virtual object GetPersistenceInfo()
    {
      return null;
    }

    #region LoadData

    public virtual GenericDataCollection LoadData(Type CollectionType, SelectCommandOption SelCmdOption, string PropertiesIdentifier)
    {
      bool connectionIsAllreadyOpened = (Connection.State == ConnectionState.Open);
      if(!connectionIsAllreadyOpened)
        Connection.Open();
      try
      {
        GenericDataCollection gecoll = (GenericDataCollection)Activator.CreateInstance(CollectionType);
        GenericDataPropertiesHolder [] gdphs = PersistenceManager.GetAllProperties(gecoll.GenericDataType, PropertiesIdentifier);
        l_DBPersistor.LoadDates(Connection, Transaction, l_CommandProvider, gdphs, gecoll, SelCmdOption, GetPersistenceInfo());
        gecoll.AcceptChanges();
        if(!connectionIsAllreadyOpened)
          Connection.Close();
        return gecoll;
      }
      catch(Exception ex)
      {
        if(!connectionIsAllreadyOpened && Connection.State == ConnectionState.Open)
          Connection.Close();
        throw new ApplicationException("error Loading data from database", ex);
      }
    }

    public GenericDataCollection LoadData(Type CollectionType, SelectCommandOption SelCmdOption)
    {
      return LoadData(CollectionType, SelCmdOption, null);
    }
    #endregion

    #region LoadDataPagedData
    public virtual GenericDataCollection LoadDataPagedData(out int RowsCount, Type CollectionType, FilterSentence sentence, 
      OrderByInfoCollection ordInfo, string PropertiesIdentifier, int PageSize, int CurrentPageIndex, int EmptySpacesInPage)
    {
      RowsCount = 0;
      bool connectionIsAllreadyOpened = (Connection.State == ConnectionState.Open);
      if(!connectionIsAllreadyOpened)
        Connection.Open();
      try
      {
        GenericDataCollection gecoll = (GenericDataCollection)Activator.CreateInstance(CollectionType);
        GenericDataPropertiesHolder gdph =  null;
        if(PropertiesIdentifier != null)
          gdph = PersistenceManager.GetProperty(gecoll.GenericDataType, PropertiesIdentifier);
        else
          gdph = PersistenceManager.GetProperty(gecoll.GenericDataType);

        IDbCommand SelectCommand = l_CommandProvider.GetSelectCommand(gdph, sentence, ordInfo, CommandLockType.Nolock, null);
        SelectCommand.Connection = Connection;
        RowsCount = l_DBPersistor.LoadPagedDates(SelectCommand, ref gdph, ref gecoll, PageSize, CurrentPageIndex, EmptySpacesInPage);
        gecoll.AcceptChanges();
        if(!connectionIsAllreadyOpened)
          Connection.Close();
        return gecoll;
      }
      catch(Exception ex)
      {
        if(!connectionIsAllreadyOpened && Connection.State == ConnectionState.Open)
          Connection.Close();
        throw ex;
      }
    }

    public GenericDataCollection LoadDataPagedData(out int RowsCount, Type CollectionType, FilterSentence sentence,  
      OrderByInfoCollection ordercols,int PageSize, int CurrentPageIndex, int EmptySpacesInPage)
    {
      RowsCount = 0;
      return LoadDataPagedData(out RowsCount, CollectionType, sentence, ordercols, null, PageSize, CurrentPageIndex, EmptySpacesInPage);
    }
    #endregion

    #region SaveData

    protected virtual void BeforeBeginTransactionAction()
    {
      //TODO
    }

    protected virtual void AfterBeginTransactionAction()
    {
      //TODO
    }

    protected virtual void BeforeCommitTransactionAction()
    {
      //TODO
    }

    protected virtual void ExecuteSaveCommands(GenericDataCollection gecoll, GenericDataPropertiesHolder  [] gdphs)
    {
      l_DBPersistor.SaveDates(Connection, Transaction, l_CommandProvider, gdphs, true, gecoll, GetPersistenceInfo());
    }

    protected virtual void ExecuteSaveCommands(GenericData ge, GenericDataPropertiesHolder  [] gdphs)
    {
      l_DBPersistor.SaveData(Connection, Transaction, l_CommandProvider, gdphs, true, ge, GetPersistenceInfo());
    }


    public void SaveData(GenericData ge, string PropertiesIdentifier)
    {
      
      try
      {
        OpenConnection();
        GenericDataPropertiesHolder  [] gdphs = PersistenceManager.GetAllProperties(ge.GetType(), PropertiesIdentifier);

        BeforeBeginTransactionAction();
        Transaction = Connection.BeginTransaction();
        AfterBeginTransactionAction();
        ExecuteSaveCommands(ge, gdphs);
        BeforeCommitTransactionAction();
        Transaction.Commit();
        ge.AcceptChanges();
        Transaction = null;
        Connection.Close();
      }
      catch(Exception ex)
      {
        if(Connection.State != ConnectionState.Closed)
        {
          if(Transaction != null)
            Transaction.Rollback();
          Transaction = null;
          Connection.Close();
        }
        throw ex;
      }
    }

    public void SaveData(GenericDataCollection gecoll, string PropertiesIdentifier)
    {
      
      try
      {
        OpenConnection();
        GenericDataPropertiesHolder  [] gdphs = PersistenceManager.GetAllProperties(gecoll.GenericDataType, PropertiesIdentifier);

        BeforeBeginTransactionAction();
        Transaction = Connection.BeginTransaction();
        AfterBeginTransactionAction();
        ExecuteSaveCommands(gecoll, gdphs);
        BeforeCommitTransactionAction();
        Transaction.Commit();
        gecoll.AcceptChanges();
        Transaction = null;
        Connection.Close();
      }
      catch(Exception ex)
      {
        if(Connection.State != ConnectionState.Closed)
        {
          if(Transaction != null)
            Transaction.Rollback();
          Transaction = null;
          Connection.Close();
        }
        throw ex;
      }
    }

    public void SaveData(GenericDataCollection gecoll)
    {
      SaveData(gecoll, null);
    }

    #endregion

    #region IDisposable Members

    public void Dispose()
    {
      if(Transaction != null)
      {
        Transaction.Rollback();
        Transaction = null;
      }
      if(Connection != null)
      {
        if(Connection.State == System.Data.ConnectionState.Open)
          Connection.Close();
        Connection.Dispose();
      }
    }

    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.