using System; using System.Collections.Generic; using System.Linq; namespace Kreta.Framework.Entities.Associations { public static class AssociationHandlerManager { private static Dictionary associationHandlers = null; private static void Initialize() { if (associationHandlers == null) { associationHandlers = new Dictionary(); 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 Create(string associationName) where LeftEntity : Entity where RightEntity : Entity { Initialize(); return associationHandlers.TryGetValue(associationName.ToLower(), out Type type) ? (AssociationHandler)Activator.CreateInstance(type, true) : new AssociationHandler(); } } }