GenericDataLink.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpers » DataLink » 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 » FileHelpers Library 
FileHelpers Library » FileHelpers » DataLink » GenericDataLink.cs
#region "   Copyright 2005-07 to Marcos Meli - http://www.marcosmeli.com.ar" 

// Errors, suggestions, contributions, send a mail to: marcos@filehelpers.com.

#endregion

using System;
using System.Reflection;

namespace FileHelpers.DataLink{
  /// <summary>
  /// This class has the responsability to enable the two directional
  /// transformation.
  /// <list type="bullet">
  /// <item> DataStorage &lt;-> DataStorage </item>
  /// </list>
  /// </summary>
  /// <remarks>
  /// <para>Uses two <see cref="DataStorage"/> to accomplish this task.</para>
  /// </remarks>
  /// <seealso href="quick_start.html">Quick Start Guide</seealso>
  /// <seealso href="class_diagram.html">Class Diagram</seealso>
  /// <seealso href="examples.html">Examples of Use</seealso>
  /// <seealso href="example_datalink.html">Example of the DataLink</seealso>
  /// <seealso href="attributes.html">Attributes List</seealso>
  public sealed class GenericDataLink
  {
    #region "  Constructor  "

    /// <summary>Create a new instance of the class.</summary>
    /// <param name="provider1">The First <see cref="DataStorage"/> used to insert/extract records .</param>
    /// <param name="provider2">The Second <see cref="DataStorage"/> used to insert/extract records .</param>
    public GenericDataLink(DataStorage provider1, DataStorage provider2)
    {
      if (provider1 == null)
        throw new ArgumentException("provider1 cant be null", "provider1");
      else
        mDataStorage1 = provider1;

      if (provider2 == null)
        throw new ArgumentException("provider2 cant be null", "provider2");
      else
        mDataStorage2 = provider2;

      ValidateRecordTypes();
    }

    #endregion

    private DataStorage mDataStorage1;
    private DataStorage mDataStorage2;

    /// <summary>Extract the records from DataStorage1 and Insert them to the DataStorage2.</summary>
    /// <returns>The Copied records.</returns>
    public object[] CopyDataFrom1To2()
    {
      object[] res = DataStorage1.ExtractRecords();
      DataStorage2.InsertRecords(res);
      return res;
    }

    /// <summary>Extract the records from DataStorage2 and Insert them to the DataStorage1.</summary>
    /// <returns>The Copied records.</returns>
    public object[] CopyDataFrom2To1()
    {
      object[] res = DataStorage2.ExtractRecords();
      DataStorage1.InsertRecords(res);
      return res;
    }

    MethodInfo mConvert1to2 = null;
    MethodInfo mConvert2to1 = null;


    /// <summary>The fisrt <see cref="DataStorage"/> of the <see cref="GenericDataLink"/>.</summary>
    public DataStorage DataStorage1
    {
      get { return mDataStorage1; }
    }

    /// <summary>The second <see cref="DataStorage"/> of the <see cref="GenericDataLink"/>.</summary>
    public DataStorage DataStorage2
    {
      get { return mDataStorage2; }
    }

    private void ValidateRecordTypes()
    {
      if (DataStorage1.RecordType == null)
        throw new BadUsageException("DataLink1 cant have a null RecordType.");

      if (DataStorage2.RecordType == null)
        throw new BadUsageException("DataLink2 cant have a null RecordType.");

      if (DataStorage1.RecordType != DataStorage2.RecordType)
      {
        mConvert1to2 = GetTransformMethod(DataStorage1.RecordType, DataStorage2.RecordType);
        if (mConvert1to2 == null)
          throw new BadUsageException("You must to define a method in the class " + DataStorage1.RecordType.Name + " with the attribute [TransfortToRecord(typeof(" + DataStorage2.RecordType.Name + "))]");

        mConvert2to1 = GetTransformMethod(DataStorage2.RecordType, DataStorage1.RecordType);
        if (mConvert2to1 == null)
          throw new BadUsageException("You must to define a method in the class " + DataStorage2.RecordType.Name + " with the attribute [TransfortToRecord(typeof(" + DataStorage1.RecordType.Name + "))]");
      }
    }

    private MethodInfo GetTransformMethod(Type sourceType, Type destType)
    {
      MethodInfo[] methods = sourceType.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
//      foreach (MethodInfo m in methods)
//      {
//        if (m.IsDefined(typeof (TransformToRecordAttribute), false))
//        {
//          TransformToRecordAttribute ta = (TransformToRecordAttribute) m.GetCustomAttributes(typeof (TransformToRecordAttribute), false)[0];
//          if (ta.TargetType == destType)
//          {
//            return m;
//          }
//        }
//      }

      return null;
    }

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