Is there an alternative to Curiously Recurring Template Pattern?

I have been causing some headaches in the last couple of weeks with a curiously repeating pattern .

Following these two of my questions:

How can I improve the following example:

public class DocketType : Enumeration<DocketType, int, string>
{
    public static DocketType Withdrawal = new DocketType(2, "Withdrawal");
    public static DocketType Installation = new DocketType(3, "Installation");

    private DocketType(int docketTypeId, string description) 
        : base(docketTypeId, description) { }
}

I need a static method that I do not need to repeat in the class Enumeration:

public abstract class Enumeration<TEnum, X, Y> : IComparable 
    where TEnum : Enumeration<TEnum, X, Y> 
{        
    protected Enumeration(X value, Y displayName)
    {
        AddToStaticCache(this);
    }
    public static TEnum Resolve(X value)
    {
        return Cache[value] as TEnum;
    }
}

The problem with this, as you will see from my second related question, is that the call Enumeration<DocketType, int, string>.Resolve(X value);does not cause the creation of static objects DocketType.

. , . ChildResolve, Resolve Enumeration. !

:

, , :

static Enumeration()
{
    GetAll();
}

public static void GetAll()
{
    var type = typeof(TEnum);
    var fields = type.GetFields(BindingFlags.Public | 
        BindingFlags.Static | BindingFlags.DeclaredOnly);

    foreach (var info in fields)
    {
        var locatedValue = info.GetValue(null) as Enumeration<TEnum, X, Y>;
        Cache.Add(locatedValue.Value, locatedValue);
    }
}

, CodeCampServer MVC, !

+3
4

, - :

public class DocketType : Enumeration<DocketType, int, string>
{
    public static readonly DocketType Withdrawal =
        new DocketType(2, "Withdrawal");

    public static readonly DocketType Installation =
        new DocketType(3, "Installation");

    private DocketType(int docketTypeId, string description)
        : base(docketTypeId, description) { }
}

public abstract class Enumeration<TEnum, TId, TDescription> : IComparable
    where TEnum : Enumeration<TEnum, TId, TDescription>
{
    private static readonly Dictionary<TId, TEnum> _cache;

    static Enumeration()
    {
        Type t = typeof(TEnum);
        _cache = t.GetFields(BindingFlags.Public | BindingFlags.Static)
                  .Where(f => f.FieldType == t)
                  .Select(f => (TEnum)f.GetValue(null))
                  .ToDictionary(e => e.Id, e => e);
    }

    public static TEnum Resolve(TId id)
    {
        return _cache[id];
    }

    public TId Id { get; private set; }
    public TDescription Description { get; private set; }

    protected Enumeration(TId id, TDescription description)
    {
        Id = id;
        Description = description;
    }

    // IComparable
    public int CompareTo(object obj)
    {
        // TODO
        throw new NotImplementedException();
    }
}
+3

, . , .

:

// The Collection of values to be enumerated
public class DocketEnum : EnumarationCollection<DocketType, int, string>
{
        // Values are fields on a statically instanced version of this class
    public DocketType Withdrawal = new DocketType(2, "Withdrawal");
    public DocketType Installation = new DocketType(3, "Installation");

    // The publicly accessible static enumeration 
    public static DocketEnum Values = new DocketEnum();
}

// The actual value class
public class DocketType : EnumerationValue<DocketType, int, string>
{
        // Call through to the helper base constructor
    public DocketType(int docketTypeId, string description) 
        : base(docketTypeId, description) { }
}

// Base class for the enumeration
public abstract class EnumarationCollection<TType, X, Y>
    where TType : EnumerationValue<TType, X, Y> 
{
            // Resolve looks at the static Dictionary in the base helpers class
    public TType Resolve(X value)
    {
        return Cache[value] as TType;
    }

    public static Dictionary<X, EnumerationValue<TType, X, Y> > Cache = new Dictionary<X, EnumerationValue<TType, X, Y>>();
}

// Base class for the value
public abstract class EnumerationValue<TType, X, Y> 
    where TType : EnumerationValue<TType, X, Y> 
{        
        // helper constructer talks directly the the base helper class for the Enumeration
    protected EnumerationValue(X value, Y displayName)
    {
        EnumarationCollection<TType, X,Y >.Cache.Add(value, this as TType);
    }
}



class MainClass
{
    public static void Main (string[] args)
    {
                    // You can immediately resolve to the enumeration
        Console.WriteLine(DocketEnum.Values.Resolve(2).ToString());
    }
}
+2

- " ". AppDomain.Current.GetAssemblies() . , , , ( , ). .GetTypes() .

, , :

Type[] subclasses = AppDomain.CurrentDomain.GetAssemblies()
    .Where(x => Attribute.IsDefined(typeof(MyEnumeartionAssemblyAttribute)))
    .SelectMany(x => x.GetTypes())
    .Where(x => x.BaseType != null && 
           x.BaseType.IsGenericType && 
           x.BaseType.GetGenericTypeDefinition() == typeof(Enumeration<,,>));

System.Type , .

0

If you want to force the static constructor of the class to run, you can use RuntimeHelpers.RunClassConstructor . You can call it from the static constructor Enumeration<TEnum, X, Y>so that it runs the first time you use the static method for any instance of the generic type:

static Enumeration()
{
    RuntimeHelpers.RunClassConstructor(typeof(TEnum).TypeHandle);
}
0
source

Source: https://habr.com/ru/post/1759010/


All Articles