This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View 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;
}
}
}

View 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
}
}
}

View 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>();
}
}
}