BusinessBase.cs :  » Persistence-Frameworks » ORM.NET » OrmLib » 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 » ORM.NET 
ORM.NET » OrmLib » BusinessBase.cs
//This file is part of ORM.NET.
//
//ORM.NET is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//ORM.NET is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with ORM.NET; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;

namespace OrmLib{
  /// <summary>
  /// Privides the base implimentation of each business object.
  /// </summary>
  /// <remarks>
  /// This class is abstract an should not be instantiated directly.
  /// </remarks>
  [Serializable()]
  public abstract class BusinessBase : IEditableObject, ICustomTypeDescriptor
  {
    /// <summary>
    /// This is an internal field and should not be used.
    /// </summary>
    /// <remarks>
    /// The underlying row of the object wrapper.
    /// </remarks>
    protected internal DataRow dataRow = null;

    /// <summary>
    /// Represents an array of the objects' parents.
    /// That is, all the collections that it is in.
    /// </summary>
    /// <remarks>
    /// Removing from this List will stop events being fired on the removed collection.
    /// Add to this list will cause events to be fired on the added collection.
    /// </remarks>
    internal protected ArrayList Parents = new ArrayList();

    /// <summary>
    /// Used for IEditableObject
    /// </summary>
    private bool editMode = false;

    /// <summary>
    /// Used for IEditableObject to delete if CancelEdit() is called.
    /// </summary>
    internal bool isNew = false;

    /// <summary>
    /// When CommitAll() is called, the underlying row will be deleted from the database
    /// </summary>
    public virtual void Delete()
    {
      while ( this.Parents.Count > 0 ) 
      {
        ((IList)this.Parents[0]).Remove(this);
      }

      dataRow.Delete();
    }

    /// <summary>
    /// Method to fire the onListItemChanged event of all parent Collections
    /// </summary>
    protected virtual void OnChanged() 
    {
      foreach ( CollectionTemplate ct in this.Parents )
      {
        ct.OnPropertyChanged(this);
      }
    }

    /// <summary>
    /// Builds the propertydescriptor collection for data binding
    /// </summary>
    /// <remarks>
    /// This method makes it possible to edit SqlTypes in the grid,
    /// and traverse child collections.
    /// </remarks>
    /// <param name="attributes"></param>
    /// <returns></returns>
    private PropertyDescriptorCollection GetPropertyDescriptorCollection( Attribute[] attributes )
    {
      ArrayList output = new ArrayList();

      foreach ( PropertyInfo pi in this.GetType().GetProperties() )
      {
        // dont do any indexers
        if ( pi.MemberType == MemberTypes.Property && pi.GetIndexParameters().Length > 0 ) continue;

        object[] atts = pi.GetCustomAttributes(typeof(BindableAttribute),true);
        if ( atts.Length == 1 && atts[0] as BindableAttribute != null && ((BindableAttribute)atts[0]).Bindable == true ) continue;

        if ( pi.PropertyType.Namespace == "System.Data.SqlTypes" )
        {
          // create the base type property descriptor
          output.Add(OrmLib.SqlPropertyDescriptor.GetProperty( pi.Name, pi.PropertyType ) );
        }
        else
        {  
          output.Add(GenericPropertyDescriptor.GetProperty(pi));
        }
      }
      return new PropertyDescriptorCollection((PropertyDescriptor[])output.ToArray(typeof(PropertyDescriptor)));
    }

    #region IEditableObject explicit interface definitions

    /// <summary>
    /// <see cref="IEditableObject.BeginEdit()"/>
    /// </summary>
    void IEditableObject.BeginEdit() 
    {
      if ( ! editMode )
      {
        this.dataRow.BeginEdit();
        editMode = true;
      }
    }

    /// <summary>
    /// <see cref="IEditableObject.CancelEdit()"/>
    /// </summary>
    void IEditableObject.CancelEdit() 
    {
      if ( editMode )
      {
        if ( isNew && !DesignMode )// this.Delete();
        {
          while ( this.Parents.Count > 0 ) 
          {
            ((IList)this.Parents[0]).Remove(this);
          }
        }

        this.dataRow.CancelEdit();
        editMode = false;
      }
    }

    private bool DesignMode
    {
      get
      {
        foreach ( CollectionTemplate collection in this.Parents ) 
        {
          if ( collection.Site != null && collection.Site.DesignMode )
            return true;
        }
        return false;
      }
    }
    /// <summary>
    /// <see cref="IEditableObject.EndEdit()"/>
    /// </summary>
    void IEditableObject.EndEdit() 
    {
      if ( editMode )
      {
        this.dataRow.EndEdit();
        editMode = false;
        isNew = false;
      }
    }

    #endregion
    #region ICustomTypeDescriptor explicit interface definitions
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetAttributes()"/>
    /// </summary>
    AttributeCollection ICustomTypeDescriptor.GetAttributes()
    {
      return TypeDescriptor.GetAttributes(this, true);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetClassName()"/>
    /// </summary>
    string ICustomTypeDescriptor.GetClassName()
    {
      return TypeDescriptor.GetClassName(this, true);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetComponentName()"/>
    /// </summary>
    string ICustomTypeDescriptor.GetComponentName()
    {
      return TypeDescriptor.GetComponentName(this, true);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetConverter()"/>
    /// </summary>
    TypeConverter ICustomTypeDescriptor.GetConverter()
    {
      return TypeDescriptor.GetConverter(this, true);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetDefaultEvent()"/>
    /// </summary>
    EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
    {
      return TypeDescriptor.GetDefaultEvent(this, true);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetDefaultProperty()"/>
    /// </summary>
    PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
    {
      return null;
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetEditor(System.Type)"/>
    /// </summary>
    object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
    {
      return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetEvents()"/>
    /// </summary>
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
    {
      return TypeDescriptor.GetEvents(this, true);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetEvents()"/>
    /// </summary>
    EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
    {
      return TypeDescriptor.GetEvents(this, attributes, true);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetProperties()"/>
    /// </summary>
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
    {
      return ((ICustomTypeDescriptor)this).GetProperties(new Attribute[0]);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetProperties()"/>
    /// </summary>
    PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
    {
      return GetPropertyDescriptorCollection(attributes);
    }
    /// <summary>
    /// <see cref="ICustomTypeDescriptor.GetPropertyOwner(System.ComponentModel.PropertyDescriptor)"/>
    /// </summary>
    object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
    {
      return this;
    }
    #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.