43 lines
1.8 KiB
C#
43 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace Kreta.Framework.Entities.Associations
|
|
{
|
|
public static class AssociationHandlerManager
|
|
{
|
|
private static Dictionary<string, Type> associationHandlers = null;
|
|
|
|
private static void Initialize()
|
|
{
|
|
if (associationHandlers == null)
|
|
{
|
|
associationHandlers = new Dictionary<string, Type>();
|
|
var handlers = from assembly in AppDomain.CurrentDomain.GetAssemblies()
|
|
where SDAServer.Instance.IsAssemblyAllowed(assembly)
|
|
from type in assembly.GetTypes()
|
|
where type.IsDefined(typeof(AssociationAttribute), false)
|
|
select new
|
|
{
|
|
AssociationName = ((AssociationAttribute)type.GetCustomAttributes(typeof(AssociationAttribute), false)[0]).AssociationName.ToLower(),
|
|
HandlerType = type,
|
|
};
|
|
foreach (var handler in handlers)
|
|
{
|
|
associationHandlers.Add(handler.AssociationName, handler.HandlerType);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static AssociationHandler<LeftEntity, RightEntity> Create<LeftEntity, RightEntity>(string associationName)
|
|
where LeftEntity : Entity
|
|
where RightEntity : Entity
|
|
{
|
|
Initialize();
|
|
|
|
return associationHandlers.TryGetValue(associationName.ToLower(), out Type type)
|
|
? (AssociationHandler<LeftEntity, RightEntity>)Activator.CreateInstance(type, true)
|
|
: new AssociationHandler<LeftEntity, RightEntity>();
|
|
}
|
|
}
|
|
}
|