Skip to content

Commit d0b727f

Browse files
committed
Create description only when needed
1 parent 0d27e1b commit d0b727f

File tree

6 files changed

+80
-20
lines changed

6 files changed

+80
-20
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ public enum SpecialOccasion
2222
Valentines,
2323

2424
[Description("Marriage anniversary")]
25-
Wedding,
25+
WeddingDay,
2626
}
2727
}

src/SpatialFocus.EntityFrameworkCore.Extensions/EnumLookupExtension.cs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,26 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
3838

3939
IMutableEntityType entityType = property.DeclaringEntityType;
4040

41-
Type concreteType = enumOptions.UseNumberLookup
42-
? typeof(EnumWithNumberLookup<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType())
43-
: 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+
4461
EntityTypeBuilder enumLookupBuilder = modelBuilder.Entity(concreteType);
4562

4663
string typeName = propertyType.GetEnumOrNullableEnumType().Name;
@@ -56,8 +73,7 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
5673
{
5774
modelBuilder.Entity(concreteType).HasIndex(nameof(EnumWithNumberLookup<Enum>.Name)).IsUnique();
5875
}
59-
60-
if (!enumOptions.UseNumberLookup)
76+
else
6177
{
6278
Type converterType = typeof(EnumToStringConverter<>).MakeGenericType(propertyType.GetEnumOrNullableEnumType());
6379
ValueConverter valueConverter = (ValueConverter)Activator.CreateInstance(converterType, new object[] { null });
@@ -73,10 +89,6 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
7389

7490
ConcreteTypeSeededList.Add(concreteType);
7591

76-
Dictionary<int, string> enumValueDescriptions = Enum.GetValues(propertyType.GetEnumOrNullableEnumType())
77-
.Cast<Enum>()
78-
.ToDictionary(Convert.ToInt32, GetEnumDescription);
79-
8092
// TODO: Check status of https://github.com/aspnet/EntityFrameworkCore/issues/12194 before using migrations
8193
object[] data = Enum.GetValues(propertyType.GetEnumOrNullableEnumType())
8294
.OfType<object>()
@@ -88,14 +100,22 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
88100
{
89101
concreteType.GetProperty(nameof(EnumWithNumberLookup<object>.Id)).SetValue(instance, x);
90102
concreteType.GetProperty(nameof(EnumWithNumberLookup<object>.Name)).SetValue(instance, x.ToString());
91-
concreteType.GetProperty(nameof(EnumWithNumberLookup<object>.Description))
92-
.SetValue(instance, enumValueDescriptions[(int)x]);
103+
104+
if (usesDescription)
105+
{
106+
concreteType.GetProperty(nameof(EnumWithNumberLookupAndDescription<object>.Description))
107+
.SetValue(instance, enumValueDescriptions[(int)x]);
108+
}
93109
}
94110
else
95111
{
96112
concreteType.GetProperty(nameof(EnumWithStringLookup<object>.Id)).SetValue(instance, x);
97-
concreteType.GetProperty(nameof(EnumWithNumberLookup<object>.Description))
98-
.SetValue(instance, enumValueDescriptions[(int)x]);
113+
114+
if (usesDescription)
115+
{
116+
concreteType.GetProperty(nameof(EnumWithNumberLookupAndDescription<object>.Description))
117+
.SetValue(instance, enumValueDescriptions[(int)x]);
118+
}
99119
}
100120

101121
return instance;
@@ -108,9 +128,9 @@ public static void ConfigureEnumLookup(this ModelBuilder modelBuilder, EnumLooku
108128

109129
public static string GetEnumDescription(Enum value)
110130
{
111-
FieldInfo fi = value.GetType().GetField(value.ToString());
131+
FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
112132

113-
DescriptionAttribute attribute = (DescriptionAttribute)fi.GetCustomAttribute(typeof(DescriptionAttribute), true);
133+
DescriptionAttribute attribute = (DescriptionAttribute)fieldInfo.GetCustomAttribute(typeof(DescriptionAttribute), true);
114134

115135
return attribute?.Description;
116136
}

src/SpatialFocus.EntityFrameworkCore.Extensions/EnumWithNumberLookup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ public EnumWithNumberLookup(T value)
1919
Name = value.ToString();
2020
}
2121

22-
public string Description { get; set; }
23-
2422
[DatabaseGenerated(DatabaseGeneratedOption.None)]
2523
public T Id { get; set; }
2624

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

src/SpatialFocus.EntityFrameworkCore.Extensions/EnumWithStringLookup.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ public EnumWithStringLookup(T value)
1818
Id = value;
1919
}
2020

21-
public string Description { get; set; }
22-
2321
[DatabaseGenerated(DatabaseGeneratedOption.None)]
2422
public T Id { get; set; }
2523
}
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)