init
This commit is contained in:
		
							
								
								
									
										19
									
								
								Framework/Entities/Associations/AssociationAttribute.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								Framework/Entities/Associations/AssociationAttribute.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,19 @@
 | 
			
		||||
using System;
 | 
			
		||||
 | 
			
		||||
namespace Kreta.Framework.Entities.Associations
 | 
			
		||||
{
 | 
			
		||||
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
 | 
			
		||||
    public class AssociationAttribute : Attribute
 | 
			
		||||
    {
 | 
			
		||||
        public string AssociationName
 | 
			
		||||
        {
 | 
			
		||||
            get;
 | 
			
		||||
            private set;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public AssociationAttribute(string associationName)
 | 
			
		||||
        {
 | 
			
		||||
            AssociationName = associationName;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								Framework/Entities/Associations/AssociationHandler.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								Framework/Entities/Associations/AssociationHandler.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
namespace Kreta.Framework.Entities.Associations
 | 
			
		||||
{
 | 
			
		||||
    public class AssociationHandler<LeftEntity, RightEntity>
 | 
			
		||||
        where LeftEntity : Entity
 | 
			
		||||
        where RightEntity : Entity
 | 
			
		||||
    {
 | 
			
		||||
        internal protected AssociationHandler()
 | 
			
		||||
        {
 | 
			
		||||
            //for internal constructs only
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public virtual void BeforeInsert(LeftEntity leftEntity, RightEntity rightEntity)
 | 
			
		||||
        {
 | 
			
		||||
            //dummy handler
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public virtual void AfterInsert(LeftEntity leftEntity, RightEntity rightEntity)
 | 
			
		||||
        {
 | 
			
		||||
            //dummy handler
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public virtual void BeforeDelete(LeftEntity leftEntity, RightEntity rightEntity)
 | 
			
		||||
        {
 | 
			
		||||
            //dummy handler
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public virtual void AfterDelete(LeftEntity leftEntity, RightEntity rightEntity)
 | 
			
		||||
        {
 | 
			
		||||
            //dummy handler
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										43
									
								
								Framework/Entities/Associations/AssociationHandlerManager.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								Framework/Entities/Associations/AssociationHandlerManager.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
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>();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user