Skip to content

Commit

Permalink
Merge pull request #7 from SpatialFocus/feat/enum-description
Browse files Browse the repository at this point in the history
Added Enum description for lookup #6
  • Loading branch information
pergerch committed Dec 12, 2018
2 parents 1ad473c + d0b727f commit 2b0bbdf
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@

namespace SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.Entities
{
using System.ComponentModel;

public enum SpecialOccasion
{
[Description("Your birth anniversary")]
Birthday = 1,

[Description("Jesus' birth anniversary")]
Christmas,

[Description("Jesus' resurrection anniversary")]
Easter,

[Description("Florist holiday")]
Valentines,

Wedding,
[Description("Marriage anniversary")]
WeddingDay,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ namespace SpatialFocus.EntityFrameworkCore.Extensions
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
Expand Down Expand Up @@ -36,9 +38,26 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku

IMutableEntityType entityType = property.DeclaringEntityType;

Type concreteType = enumOptions.UseNumberLookup
? typeof(EnumWithNumberLookup<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType())
: typeof(EnumWithStringLookup<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType());
Dictionary<int, string> enumValueDescriptions = Enum.GetValues(propertyType.GetEnumOrNullableEnumType())
.Cast<Enum>()
.ToDictionary(Convert.ToInt32, GetEnumDescription);

bool usesDescription = enumValueDescriptions.Values.Any(x => x != null);

Type concreteType;
if (usesDescription)
{
concreteType = enumOptions.UseNumberLookup
? typeof(EnumWithNumberLookupAndDescription<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType())
: typeof(EnumWithStringLookupAndDescription<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType());
}
else
{
concreteType = enumOptions.UseNumberLookup
? typeof(EnumWithNumberLookup<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType())
: typeof(EnumWithStringLookup<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType());
}

EntityTypeBuilder enumLookupBuilder = modelBuilder.Entity(concreteType);

string typeName = propertyType.GetEnumOrNullableEnumType().Name;
Expand All @@ -54,8 +73,7 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
{
modelBuilder.Entity(concreteType).HasIndex(nameof(EnumWithNumberLookup<Enum>.Name)).IsUnique();
}

if (!enumOptions.UseNumberLookup)
else
{
Type converterType = typeof(EnumToStringConverter<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType());
ValueConverter valueConverter = (ValueConverter)Activator.CreateInstance(converterType, new object[] { null });
Expand All @@ -82,10 +100,22 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
{
concreteType.GetProperty(nameof(EnumWithNumberLookup<object>.Id)).SetValue(instance, x);
concreteType.GetProperty(nameof(EnumWithNumberLookup<object>.Name)).SetValue(instance, x.ToString());
if (usesDescription)
{
concreteType.GetProperty(nameof(EnumWithNumberLookupAndDescription<object>.Description))
.SetValue(instance, enumValueDescriptions[(int)x]);
}
}
else
{
concreteType.GetProperty(nameof(EnumWithStringLookup<object>.Id)).SetValue(instance, x);
if (usesDescription)
{
concreteType.GetProperty(nameof(EnumWithNumberLookupAndDescription<object>.Description))
.SetValue(instance, enumValueDescriptions[(int)x]);
}
}
return instance;
Expand All @@ -96,6 +126,15 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
}
}

public static string GetEnumDescription(Enum value)
{
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());

DescriptionAttribute attribute = (DescriptionAttribute)fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute), true);

return attribute?.Description;
}

private static Type GetEnumOrNullableEnumType(this Type propertyType)
{
if (!propertyType.IsEnumOrNullableEnumType())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <copyright file="EnumWithNumberLookupAndDescription.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions
{
public class EnumWithNumberLookupAndDescription<T> : EnumWithNumberLookup<T>
{
public EnumWithNumberLookupAndDescription()
: base()
{
}

public EnumWithNumberLookupAndDescription(T value)
: base(value)
{
}

public string Description { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <copyright file="EnumWithStringLookupAndDescription.cs" company="Spatial Focus">
// Copyright (c) Spatial Focus. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
// </copyright>

namespace SpatialFocus.EntityFrameworkCore.Extensions
{
public class EnumWithStringLookupAndDescription<T> : EnumWithStringLookup<T>
{
public EnumWithStringLookupAndDescription()
: base()
{
}

public EnumWithStringLookupAndDescription(T value)
: base(value)
{
}

public string Description { get; set; }
}
}

0 comments on commit 2b0bbdf

Please sign in to comment.