Here's the final version. Perhaps something like this functionality could find its way into a later version of LLBLGen.
using System;
using System.Collections;
using SD.LLBLGen.Pro.ORMSupportClasses;
namespace LLBLGenExtensions
{
public interface IEntityHelper
{
IEntityCollection2 GetEntityCollection(Type type);
IEntity2 GetEntityInstance(Type type);
IPrefetchPath2 GetEntityPreFetchPath(Type type, string viewName);
}
public abstract class EntityHelperBase : IEntityHelper
{
protected class PrefetchPathData
{
private int entityType;
public int EntityType
{
get { return entityType; }
set { entityType = value; }
}
private IPrefetchPathElement2 [] pathElements;
public IPrefetchPathElement2[] PathElements
{
get { return pathElements; }
set { pathElements = value; }
}
}
protected delegate IEntityFactory2 FactoryRetriever();
protected Hashtable htFactories = new Hashtable();
private Hashtable htPrefetch = new Hashtable();
public abstract IEntityCollection2 GetEntityCollection(Type type);
protected abstract void CreateFactoryHashtable();
protected abstract void CreatePreFetchPaths();
public EntityHelperBase()
{
CreateFactoryHashtable();
CreatePreFetchPaths();
}
protected void AddPrefetchData(Type type, int entityType, string viewName, params IPrefetchPathElement2 [] pathElements)
{
PrefetchPathData pathData = new PrefetchPathData();
pathData.EntityType = entityType;
pathData.PathElements = pathElements;
htPrefetch.Add(MakeKey(type, viewName), pathData);
}
protected static string MakeKey(Type type, string key)
{
return string.Concat(type.ToString(), key);
}
public IPrefetchPath2 GetEntityPreFetchPath(Type type, string key)
{
PrefetchPathData pathData = (PrefetchPathData)htPrefetch[MakeKey(type, key)];
if (pathData == null)
{
return null;
}
IPrefetchPath2 prefetchPath = new PrefetchPath2(pathData.EntityType);
foreach(IPrefetchPathElement2 path in pathData.PathElements)
{
prefetchPath.Add(path);
}
return prefetchPath;
}
public IEntity2 GetEntityInstance(Type type)
{
return GetFactory(type).Create();
}
protected IEntityFactory2 GetFactory(Type type)
{
FactoryRetriever retriever1 = (FactoryRetriever)htFactories[type];
return retriever1();
}
}
}
Template:
using System;
using <[RootNamespace]>.EntityClasses;
using <[RootNamespace]>.HelperClasses;
using <[RootNamespace]>.FactoryClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using LLBLGenExtensions;
namespace <[RootNamespace]>
{
public class EntityHelper : EntityHelperBase
{
protected override void CreatePreFetchPaths()
{
//e.g. AddPrefetchData(typeof(OrderEntity), (int)EntityType.OrderEntity, "WithCustomer", OrderEntity.PrefetchPathCustomer);
#region Custom User Code
#endregion
}
public override IEntityCollection2 GetEntityCollection(Type type)
{
return new EntityCollection(GetFactory(type));
}
protected override void CreateFactoryHashtable()
{
<[Foreach Entity CrLf]> htFactories.Add(typeof(<[CurrentEntityName]>Entity), new FactoryRetriever(Get<[CurrentEntityName]>EntityFactory));<[NextForeach]>
}
<[Foreach Entity CrLf]>
private IEntityFactory2 Get<[CurrentEntityName]>EntityFactory()
{
return new <[CurrentEntityName]>EntityFactory();
}<[NextForeach]>
}
}
Example:
Type objectType = typeof(OrderEntity);
IEntityHelper LLBLGenHelper = new EntityHelper();
IEntity2 entityInstance = LLBLGenHelper.GetEntityInstance(objectType);
DataAccessAdapter da = new DataAccessAdapter();
da.FetchEntity(entityInstance, LLBLGenHelper.GetEntityPreFetchPath(objectType, "WithCustomer"));
IEntityCollection2 collection = LLBLGenHelper.GetEntityCollection(objectType);