Files
BeWoPlaner/Shared/Core/AbstractIDSpecificDefaultClass.cs

96 lines
3.3 KiB
C#
Raw Normal View History

2016-06-27 01:45:38 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace BS.Shared.Core
{
public class AbstractIDSpecificDefaultClass<T> where T : AbstractIDSpecificDefaultClass<T>, new()
{
#region Constants and Fields
protected string _SpecificID = string.Empty;
private static readonly Dictionary<string, T> _InstanceCache = new Dictionary<string, T>();
#endregion
#region Constructors and Destructors
protected AbstractIDSpecificDefaultClass() {}
#endregion
#region Public Methods
public static T GetInstance(string specificId, string tenant = null)
{
if (specificId == null)
{
specificId = tenant;
}
2016-06-27 01:45:38 +02:00
if (!string.IsNullOrEmpty(specificId))
{
if (_InstanceCache.ContainsKey(specificId))
{
return _InstanceCache[specificId];
}
var allemblyTypes = typeof(T).Assembly.GetTypes();
var assignableForms = allemblyTypes.Where(i => typeof(T).IsAssignableFrom(i));
var x = assignableForms.FirstOrDefault(t => t.GetCustomAttributes(typeof(IDSpecificClassAttribute), true).ToList().Exists(i =>
{
if(i is IDSpecificClassAttribute attribute)
{
if(attribute.Identifiers != null)
{
return attribute.Identifiers.Contains(specificId);
}
if(!string.IsNullOrEmpty(attribute.Identifier))
{
return attribute.Identifier.Equals(specificId);
}
}
return false;
}));
2016-06-27 01:45:38 +02:00
var cbSpecific = typeof(T).Assembly.GetTypes()
.Where(i => typeof(T).IsAssignableFrom(i))
.FirstOrDefault(t => t.GetCustomAttributes(typeof(IDSpecificClassAttribute), true)
.ToList()
.Exists(i =>
{
var attr = (IDSpecificClassAttribute)i;
if (attr.Identifiers != null)
return attr.Identifiers.Contains(specificId);
if (!String.IsNullOrEmpty(attr.Identifier))
return attr.Identifier.Equals(specificId);
return false;
}));
if (cbSpecific != null)
{
var instance = Activator.CreateInstance(cbSpecific) as T;
instance._SpecificID = specificId;
_InstanceCache.Add(specificId, instance);
return _InstanceCache[specificId];
}
}
if (!_InstanceCache.ContainsKey(string.Empty))
{
_InstanceCache.Add(string.Empty,
new T
{
_SpecificID = specificId
});
}
return _InstanceCache[string.Empty];
}
#endregion
}
}