// --------------------------------------------------------------------------------------------------------------------
// <copyright file="LLBLGenHelper.cs" company="Catel development team">
// Copyright (c) 2008 - 2011 Catel development team. All rights reserved.
// </copyright>
// <summary>
// LLBLGen Pro helper class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Reflection;
using log4net;
namespace Catel.LLBLGen
{
/// <summary>
/// LLBLGen Pro helper class.
/// </summary>
/// <remarks>
/// This class extensively use reflection so no actual reference to LLBLGen is required.
/// </remarks>
public static class LLBLGenHelper
{
#region Variables
/// <summary>
/// The <see cref="ILog">log</see> object.
/// </summary>
private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
#endregion
/// <summary>
/// Determines whether the specified type is an LLBLGen type (entity or collection).
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>
/// <c>true</c> if the specified type is an LLBLGen type; otherwise, <c>false</c>.
/// </returns>
/// <remarks>
/// This method uses the <see cref="IsEntityCollection(System.Type)"/> and <see cref="IsEntity(System.Type)"/> methods.
/// </remarks>
public static bool IsLLBLGenType(Type type)
{
return IsEntity(type) || IsEntityCollection(type);
}
/// <summary>
/// Determines whether the specified object is an LLBLGen type (entity or collection).
/// </summary>
/// <param name="entity">The entity to check.</param>
/// <returns>
/// <c>true</c> if the specified object is an LLBLGen type; otherwise, <c>false</c>.
/// </returns>
/// <remarks>
/// This method uses the <see cref="IsEntityCollection(System.Type)"/> and <see cref="IsEntity(System.Type)"/> methods.
/// </remarks>
public static bool IsLLBLGenType(object entity)
{
return IsEntity(entity) || IsEntityCollection(entity);
}
/// <summary>
/// Determines whether the specified type is an LLBLGen entity.
/// </summary>
/// <param name="type">The type to check.</param>
/// <returns>
/// <c>true</c> if the specified type is an LLBLGen entity; otherwise, <c>false</c>.
/// </returns>
public static bool IsEntity(Type type)
{
return GetIEntityCore(type) != null;
}
/// <summary>
/// Determines whether the specified object is an LLBLGen entity.
/// </summary>
/// <param name="entity">The entity to check.</param>
/// <returns>
/// <c>true</c> if the specified object is an LLBLGen entity; otherwise, <c>false</c>.
/// </returns>
public static bool IsEntity(object entity)
{
return GetIEntityCore(entity) != null;
}
/// <summary>
/// Determines whether the specified type is an LLBLGen entity collection.
/// </summary>
/// <param name="type">The typ to checke.</param>
/// <returns>
/// <c>true</c> if the specified type is an LLBLGen entity collection; otherwise, <c>false</c>.
/// </returns>
public static bool IsEntityCollection(Type type)
{
return GetIEntityCollectionCore(type) != null;
}
/// <summary>
/// Determines whether the specified object is an LLBLGen entity collection.
/// </summary>
/// <param name="entity">The entity to check.</param>
/// <returns>
/// <c>true</c> if the specified object is an LLBLGen entity collection; otherwise, <c>false</c>.
/// </returns>
public static bool IsEntityCollection(object entity)
{
return GetIEntityCollectionCore(entity) != null;
}
/// <summary>
/// Gets the <c>IEntityCore.ValidateEntity</c> method if available.
/// </summary>
/// <param name="entity">The entity to get the method from.</param>
/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
public static MethodInfo GetValidateEntityMethod(object entity)
{
return GetMethod(entity, "ValidateEntity");
}
/// <summary>
/// Gets the <c>IEntityCore.SaveFields(string)</c> method if available.
/// </summary>
/// <param name="entity">The entity to get the method from.</param>
/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
public static MethodInfo GetSaveFieldsMethod(object entity)
{
return GetMethod(entity, "SaveFields", new[] { typeof(string) });
}
/// <summary>
/// Gets the <c>IEntityCore.GetDiscardSavedFieldsMethod()</c> method if available.
/// </summary>
/// <param name="entity">The entity to get the method from.</param>
/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
public static MethodInfo GetDiscardSavedFieldsMethod(object entity)
{
return GetMethod(entity, "DiscardSavedFields");
}
/// <summary>
/// Gets the <c>IEntityCore.RollbackFields(string)</c> method if available.
/// </summary>
/// <param name="entity">The entity to get the method from.</param>
/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
public static MethodInfo GetRollbackFieldsMethod(object entity)
{
return GetMethod(entity, "RollbackFields", new[] { typeof(string) });
}
/// <summary>
/// Gets the refetch method.
/// </summary>
/// <param name="entity">The entity to get the method from.</param>
/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
public static MethodInfo GetRefetchMethod(object entity)
{
return GetMethod(entity, "Refetch");
}
/// <summary>
/// Gets the name of the related entity property. For example, if <c>Product</c> is passed of an <c>Order</c> entity, this
/// method will return <c>ProductID</c>.
/// </summary>
/// <param name="type">The type of the LLBLGen entity.</param>
/// <param name="property">The property to map.</param>
/// <returns>
/// The name of the mapped property, or <c>null</c> if the property cannot be mapped.
/// </returns>
/// <remarks>
/// For now, this method returns the property name with "ID" appended. Still waiting for feedback from LLBLGen team.
/// TODO: actually retrieve the property name correctly
/// </remarks>
public static string GetRelatedEntityPropertyName(Type type, string property)
{
return string.Format("{0}ID", property);
}
/// <summary>
/// Gets a method with the specified name of the <c>IEntityCore</c> interface.
/// </summary>
/// <param name="entity">The entity to get the method from.</param>
/// <param name="methodName">Name of the method.</param>
/// <returns><see cref="MethodInfo"/> or <c>null</c> if the method is not found.</returns>
/// <remarks>
/// This method only supports the retrieval of public instance methods that have no parameters.
/// </remarks>
public static MethodInfo GetMethod(object entity, string methodName)
{
return GetMethod(entity, methodName, Type.EmptyTypes);
}
/// <summary>
/// Gets a method with the specified name of the <c>IEntityCore</c> interface.
/// </summary>
/// <param name="entity">The entity to get the method from.</param>
/// <param name="methodName">Name of the method.</param>
/// <param name="types">The types of the parameters.</param>
/// <returns>
/// <see cref="MethodInfo"/> or <c>null</c> if the method is not found.
/// </returns>
/// <remarks>
/// This method only supports the retrieval of public instance methods.
/// </remarks>
public static MethodInfo GetMethod(object entity, string methodName, Type[] types)
{
if (entity == null)
{
return null;
}
Type type = entity.GetType();
return type.GetMethod(methodName, types);
}
/// <summary>
/// Gets the object ID of a <c>IEntityCore</c> if the object is an LLBLGen entity.
/// </summary>
/// <param name="entity">The entity to get the object ID from.</param>
/// <returns>
/// The object ID of the entity, or <c>null</c> if the object is not an entity.
/// </returns>
public static object GetEntityObjectID(object entity)
{
var entityInterface = GetIEntityCore(entity);
if (entityInterface == null)
{
return null;
}
var propertyInfo = entityInterface.GetProperty("ObjectID");
if (propertyInfo == null)
{
return null;
}
if (!propertyInfo.CanRead)
{
return null;
}
return propertyInfo.GetValue(entity, null);
}
/// <summary>
/// Gets the <c>IEntityCore</c> interface of the type if available.
/// </summary>
/// <param name="type">The type to get the interface from.</param>
/// <returns>
/// <c>IEntityCore</c> or <c>null</c> if this type has no support for LLBLGen Pro.
/// </returns>
public static Type GetIEntityCore(Type type)
{
return type.GetInterface("IEntityCore", false);
}
/// <summary>
/// Gets the <c>IEntityCore</c> interface of the object if available.
/// </summary>
/// <param name="entity">The entity to get the interface from.</param>
/// <returns><c>IEntityCore</c> or <c>null</c> if this object has no support for LLBLGen Pro.</returns>
public static Type GetIEntityCore(object entity)
{
if (entity == null)
{
return null;
}
return GetIEntityCore(entity.GetType());
}
/// <summary>
/// Gets the <c>IEntityCollectionCore</c> interface of the type if available.
/// </summary>
/// <param name="type">The type to get the interface from.</param>
/// <returns><c>IEntityCollectionCore</c> or <c>null</c> if this type has no support for LLBLGen Pro.</returns>
public static Type GetIEntityCollectionCore(Type type)
{
return type.GetInterface("IEntityCollectionCore", false);
}
/// <summary>
/// Gets the <c>IEntityCollectionCore</c> interface of the object if available.
/// </summary>
/// <param name="entity">The entity to get the interface from.</param>
/// <returns><c>IEntityCollectionCore</c> or <c>null</c> if this object has no support for LLBLGen Pro.</returns>
public static Type GetIEntityCollectionCore(object entity)
{
if (entity == null)
{
return null;
}
return GetIEntityCollectionCore(entity.GetType());
}
}
}