// ========================================================
namespace Kerosene.ORM.Core.Concrete
{
using Kerosene.Tools;
using System;
using System.Runtime.Serialization;
using System.Text;
// ====================================================
/// <summary>
/// Represents the alias of a given element in a command.
/// </summary>
[Serializable]
public class KAliasBase : IKAlias
{
bool _IsDisposed = false;
IKAliasCollection _Owner = null;
string _Alias = null;
string _Table = null;
/// <summary>
/// Creates a new empty and orphan instance.
/// </summary>
public KAliasBase() { }
/// <summary>
/// Gets whether this instance has been disposed or not.
/// </summary>
public bool IsDisposed
{
get { return _IsDisposed; }
}
/// <summary>
/// Disposes this instance, and removes it from its owner it any.
/// </summary>
public void Dispose()
{
if (!IsDisposed) { OnDispose(true); GC.SuppressFinalize(this); }
}
~KAliasBase()
{
if (!IsDisposed) OnDispose(false);
}
protected virtual void OnDispose(bool disposing)
{
if (!IsDisposed && disposing)
{
if (_Owner != null)
{
var temp = _Owner; _Owner = null;
if (temp != null && !temp.IsDisposed) temp.Remove(this);
}
}
_IsDisposed = true;
}
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (IsDisposed) throw new ObjectDisposedException(this.ToString());
info.AddValue("Alias", _Alias);
info.AddValue("Table", _Table);
}
protected KAliasBase(SerializationInfo info, StreamingContext context)
{
_Alias = info.GetString("Alias");
_Table = info.GetString("Table");
}
/// <summary>
/// Returns a new orphan instance that otherwise is a copy of the current one.
/// </summary>
public KAliasBase Clone()
{
if (IsDisposed) throw new ObjectDisposedException(this.ToString());
var cloned = new KAliasBase();
OnClone(cloned); return cloned;
}
IKAlias IKAlias.Clone()
{
return this.Clone();
}
object ICloneable.Clone()
{
return this.Clone();
}
protected virtual void OnClone(object cloned)
{
var temp = (KAliasBase)cloned;
temp._Alias = _Alias;
temp._Table = _Table;
}
/// <summary>
/// Returns whether this instance can be considered equivalent to the other one given.
/// </summary>
/// <param name="other">The other one to compare this one with.</param>
public bool Equals(KAliasBase other)
{
if (object.ReferenceEquals(this, other)) return true;
if (object.ReferenceEquals(null, other)) return false;
if (this.IsDisposed) return false;
if (other.IsDisposed) return false;
var sensitive = _Owner == null ? IKAliasCollectionHelper.DefaultCaseSensitive : _Owner.CaseSensitive;
if (string.Compare(_Alias, other._Alias, ignoreCase: !sensitive) != 0) return false;
if (string.Compare(_Table, other._Table, ignoreCase: !sensitive) != 0) return false;
return true;
}
bool IEquatable<IKAlias>.Equals(IKAlias other)
{
if (Object.ReferenceEquals(this, other)) return true;
if (Object.ReferenceEquals(null, other)) return false;
var temp = other as KAliasBase; if (Object.ReferenceEquals(null, temp)) return false;
return Equals(temp);
}
public override bool Equals(object other)
{
if (Object.ReferenceEquals(this, other)) return true;
if (Object.ReferenceEquals(null, other)) return false;
var temp = other as KAliasBase; if (Object.ReferenceEquals(null, temp)) return false;
return Equals(temp);
}
public override int GetHashCode()
{
return ToString().GetHashCode();
}
public static bool operator ==(KAliasBase left, KAliasBase right)
{
if (object.ReferenceEquals(left, null) && object.ReferenceEquals(right, null)) return true;
if (object.ReferenceEquals(left, null) && !object.ReferenceEquals(right, null)) return false;
if (!object.ReferenceEquals(left, null) && object.ReferenceEquals(right, null)) return false;
if (object.ReferenceEquals(left, right)) return true;
return left.Equals(right);
}
public static bool operator !=(KAliasBase left, KAliasBase right)
{
return !(left == right);
}
/// <summary>
/// Returns the string representation of this instance.
/// </summary>
public override string ToString()
{
StringBuilder sb = new StringBuilder();
if (IsDisposed) sb.Append("disposed::[");
sb.AppendFormat("{0} => {1}",
_Alias ?? "-",
_Table ?? "-");
if (IsDisposed) sb.Append("]");
return sb.ToString();
}
/// <summary>
/// Gets the collection this parameter belongs to, or null if it is an orphan instance.
/// </summary>
public IKAliasCollection Owner
{
get { return _Owner; }
set
{
if (value == null)
{
var temp = _Owner; _Owner = null;
if (temp != null && !temp.IsDisposed) temp.Remove(this);
}
else
{
if (IsDisposed) throw new ObjectDisposedException(this.ToString());
if (object.ReferenceEquals(_Owner, value)) return;
if (value.IndexOf(this) < 0) value.Add(this);
}
_Owner = value;
}
}
/// <summary>
/// Gets the actual alias maintained by this instance.
/// <para>The setter fails if this instance is not an orphan one.</para>
/// </summary>
public string Alias
{
get { return _Alias; }
set
{
if (IsDisposed) throw new ObjectDisposedException(this.ToString());
if (Owner != null) throw new NotOrphanException(string.Format("This '{0}' is not orphan.", this));
_Alias = IKAliasHelper.ValidateAlias(value);
}
}
/// <summary>
/// Gets the table name (or element) this alias refer to, or null if it refers to the default one.
/// <para>The setter fails if this instance is not an orphan one.</para>
/// </summary>
public string Table
{
get { return _Table; }
set
{
if (IsDisposed) throw new ObjectDisposedException(this.ToString());
if (Owner != null) throw new NotOrphanException(string.Format("This '{0}' is not orphan.", this));
_Table = IKAliasHelper.ValidateTable(value);
}
}
}
}
// ========================================================