Skip to content

Commit 2b0bbdf

Browse files
authored
Merge pull request #7 from SpatialFocus/feat/enum-description
Added Enum description for lookup #6
2 parents 1ad473c + d0b727f commit 2b0bbdf

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed

samples/SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo/Entities/SpecialOccasion.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,23 @@
55

66
namespace SpatialFocus.EntityFrameworkCore.Extensions.SQLiteDemo.Entities
77
{
8+
using System.ComponentModel;
9+
810
public enum SpecialOccasion
911
{
12+
[Description("Your birth anniversary")]
1013
Birthday = 1,
1114

15+
[Description("Jesus' birth anniversary")]
1216
Christmas,
1317

18+
[Description("Jesus' resurrection anniversary")]
1419
Easter,
1520

21+
[Description("Florist holiday")]
1622
Valentines,
1723

18-
Wedding,
24+
[Description("Marriage anniversary")]
25+
WeddingDay,
1926
}
2027
}

src/SpatialFocus.EntityFrameworkCore.Extensions/EnumLookupExtension.cs

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ namespace SpatialFocus.EntityFrameworkCore.Extensions
77
{
88
using System;
99
using System.Collections.Generic;
10+
using System.ComponentModel;
1011
using System.Linq;
12+
using System.Reflection;
1113
using Microsoft.EntityFrameworkCore;
1214
using Microsoft.EntityFrameworkCore.Metadata;
1315
using Microsoft.EntityFrameworkCore.Metadata.Builders;
@@ -36,9 +38,26 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
3638

3739
IMutableEntityType entityType = property.DeclaringEntityType;
3840

39-
Type concreteType = enumOptions.UseNumberLookup
40-
? typeof(EnumWithNumberLookup<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType())
41-
: typeof(EnumWithStringLookup<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType());
41+
Dictionary<int, string> enumValueDescriptions = Enum.GetValues(propertyType.GetEnumOrNullableEnumType())
42+
.Cast<Enum>()
43+
.ToDictionary(Convert.ToInt32, GetEnumDescription);
44+
45+
bool usesDescription = enumValueDescriptions.Values.Any(x => x != null);
46+
47+
Type concreteType;
48+
if (usesDescription)
49+
{
50+
concreteType = enumOptions.UseNumberLookup
51+
? typeof(EnumWithNumberLookupAndDescription<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType())
52+
: typeof(EnumWithStringLookupAndDescription<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType());
53+
}
54+
else
55+
{
56+
concreteType = enumOptions.UseNumberLookup
57+
? typeof(EnumWithNumberLookup<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType())
58+
: typeof(EnumWithStringLookup<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType());
59+
}
60+
4261
EntityTypeBuilder enumLookupBuilder = modelBuilder.Entity(concreteType);
4362

4463
string typeName = propertyType.GetEnumOrNullableEnumType().Name;
@@ -54,8 +73,7 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
5473
{
5574
modelBuilder.Entity(concreteType).HasIndex(nameof(EnumWithNumberLookup<Enum>.Name)).IsUnique();
5675
}
57-
58-
if (!enumOptions.UseNumberLookup)
76+
else
5977
{
6078
Type converterType = typeof(EnumToStringConverter<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType());
6179
ValueConverter valueConverter = (ValueConverter)Activator.CreateInstance(converterType, new object[] { null });
@@ -82,10 +100,22 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
82100
{
83101
concreteType.GetProperty(nameof(EnumWithNumberLookup<object>.Id)).SetValue(instance, x);
84102
concreteType.GetProperty(nameof(EnumWithNumberLookup<object>.Name)).SetValue(instance, x.ToString());
103+
104+
if (usesDescription)
105+
{
106+
concreteType.GetProperty(nameof(EnumWithNumberLookupAndDescription<object>.Description))
107+
.SetValue(instance, enumValueDescriptions[(int)x]);
108+
}
85109
}
86110
else
87111
{
88112
concreteType.GetProperty(nameof(EnumWithStringLookup<object>.Id)).SetValue(instance, x);
113+
114+
if (usesDescription)
115+
{
116+
concreteType.GetProperty(nameof(EnumWithNumberLookupAndDescription<object>.Description))
117+
.SetValue(instance, enumValueDescriptions[(int)x]);
118+
}
89119
}
90120

91121
return instance;
@@ -96,6 +126,15 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
96126
}
97127
}
98128

129+
public static string GetEnumDescription(Enum value)
130+
{
131+
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
132+
133+
DescriptionAttribute attribute = (DescriptionAttribute)fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute), true);
134+
135+
return attribute?.Description;
136+
}
137+
99138
private static Type GetEnumOrNullableEnumType(this Type propertyType)
100139
{
101140
if (!propertyType.IsEnumOrNullableEnumType())
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// <copyright file="EnumWithNumberLookupAndDescription.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions
7+
{
8+
public class EnumWithNumberLookupAndDescription<T> : EnumWithNumberLookup<T>
9+
{
10+
public EnumWithNumberLookupAndDescription()
11+
: base()
12+
{
13+
}
14+
15+
public EnumWithNumberLookupAndDescription(T value)
16+
: base(value)
17+
{
18+
}
19+
20+
public string Description { get; set; }
21+
}
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// <copyright file="EnumWithStringLookupAndDescription.cs" company="Spatial Focus">
2+
// Copyright (c) Spatial Focus. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
// </copyright>
5+
6+
namespace SpatialFocus.EntityFrameworkCore.Extensions
7+
{
8+
public class EnumWithStringLookupAndDescription<T> : EnumWithStringLookup<T>
9+
{
10+
public EnumWithStringLookupAndDescription()
11+
: base()
12+
{
13+
}
14+
15+
public EnumWithStringLookupAndDescription(T value)
16+
: base(value)
17+
{
18+
}
19+
20+
public string Description { get; set; }
21+
}
22+
}

0 commit comments

Comments
 (0)