Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/nuget/BenchmarkDotNet-0.13.10
Browse files Browse the repository at this point in the history
  • Loading branch information
ardalis authored Jan 16, 2024
2 parents f020025 + 75d657d commit d38ce33
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Features>strict</Features>
</PropertyGroup>
<PropertyGroup>
<SolutionDir Condition="'$(SolutionDir)'==''">$(MSBuildThisFileDirectory)</SolutionDir>
<RepoRelativeProjectDir>$([MSBuild]::MakeRelative($(RepoRoot), $(MSBuildProjectDirectory)))</RepoRelativeProjectDir>

<!-- Define useful flags based on project name conventions -->
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="7.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.2" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="1.1.1" />
<PackageVersion Include="Moq" Version="4.18.4" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.2" />
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Install-Package Ardalis.SmartEnum -Version 2.1.0

## Usage

Define your smart enum by inheriting from `SmartEnum<TEnum>` where `TEnum` is the type you're declaring. For [example](/src/SmartEnum.UnitTests/TestEnum.cs):
Define your smart enum by inheriting from `SmartEnum<TEnum>` where `TEnum` is the type you're declaring. For [example](/test/SmartEnum.UnitTests/TestEnum.cs):

```csharp
using Ardalis.SmartEnum;
Expand Down
6 changes: 5 additions & 1 deletion src/SmartEnum.EFCore/SmartEnumConverterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;

namespace SmartEnum.EFCore
Expand All @@ -23,6 +25,7 @@ public static void ConfigureSmartEnum(this ModelConfigurationBuilder configurati
var propertyTypes = modelBuilder.Model.GetEntityTypes()
.SelectMany(e => e.ClrType.GetProperties())
.Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>)))
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() == null)
.Select(p => p.PropertyType)
.Distinct();

Expand Down Expand Up @@ -52,7 +55,8 @@ public static void ConfigureSmartEnum(this ModelBuilder modelBuilder)
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var properties = entityType.ClrType.GetProperties()
.Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>)));
.Where(p => TypeUtil.IsDerived(p.PropertyType, typeof(SmartEnum<,>)))
.Where(p => p.GetCustomAttribute<NotMappedAttribute>() == null);

foreach (var property in properties)
{
Expand Down
4 changes: 4 additions & 0 deletions test/SmartEnum.EFCore.IntegrationTests/Entities/SomeEntity.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

namespace SmartEnum.EFCore.IntegrationTests.Entities
{
Expand All @@ -24,6 +25,9 @@ public class SomeEntity

public List<SomeOwnedEntity> OwnedEntities { get; set; }

[NotMapped]
public TestEnum NotMappedTest { get; set; }

public class Configuration : IEntityTypeConfiguration<SomeEntity>
{
public void Configure(EntityTypeBuilder<SomeEntity> builder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.EntityFrameworkCore;

using System.ComponentModel.DataAnnotations.Schema;

namespace SmartEnum.EFCore.IntegrationTests.Entities
{
[Owned]
Expand All @@ -18,5 +20,8 @@ public class SomeOuterOwnedEntity
public TestBaseEnumWithDerivedValues Test4 { get; set; }

public SomeOwnedEntity InnerOwnedEntity { get; set; }

[NotMapped]
public TestEnum NotMappedTest { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.EntityFrameworkCore;

using System.ComponentModel.DataAnnotations.Schema;

namespace SmartEnum.EFCore.IntegrationTests.Entities
{
[Owned]
Expand All @@ -16,5 +18,8 @@ public class SomeOwnedEntity
public TestStringEnum Test3 { get; set; }

public TestBaseEnumWithDerivedValues Test4 { get; set; }

[NotMapped]
public TestEnum NotMappedTest { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ private static SomeEntity CreateEntity()
Test2 = TestDerivedEnum.One,
Test3 = TestStringEnum.One,
Test4 = DerivedTestEnumWithValues1.A,
NotMappedTest = TestEnum.One,
OwnedEntity = new SomeOwnedEntity
{
Value = 2,
Expand Down Expand Up @@ -143,27 +144,31 @@ private static void VerifyEntity(SomeEntity entity)
entity.Test2.Should().Be(TestDerivedEnum.One);
entity.Test3.Should().Be(TestStringEnum.One);
entity.Test4.Should().Be(DerivedTestEnumWithValues1.A);
entity.NotMappedTest.Should().BeNull();

entity.OwnedEntity.Value.Should().Be(2);
entity.OwnedEntity.Weekday.Should().Be(Weekday.Friday);
entity.OwnedEntity.Test1.Should().Be(TestEnum.Two);
entity.OwnedEntity.Test2.Should().Be(TestDerivedEnum.One);
entity.OwnedEntity.Test3.Should().Be(TestStringEnum.Two);
entity.OwnedEntity.Test4.Should().Be(DerivedTestEnumWithValues1.B);
entity.OwnedEntity.NotMappedTest.Should().BeNull();

entity.OuterOwnedEntity.Value.Should().Be(3);
entity.OuterOwnedEntity.Weekday.Should().Be(Weekday.Friday);
entity.OuterOwnedEntity.Test1.Should().Be(TestEnum.Two);
entity.OuterOwnedEntity.Test2.Should().Be(TestDerivedEnum.One);
entity.OuterOwnedEntity.Test3.Should().Be(TestStringEnum.Two);
entity.OuterOwnedEntity.Test4.Should().Be(DerivedTestEnumWithValues1.B);
entity.OuterOwnedEntity.NotMappedTest.Should().BeNull();

entity.OuterOwnedEntity.InnerOwnedEntity.Value.Should().Be(4);
entity.OuterOwnedEntity.InnerOwnedEntity.Weekday.Should().Be(Weekday.Friday);
entity.OuterOwnedEntity.InnerOwnedEntity.Test1.Should().Be(TestEnum.Two);
entity.OuterOwnedEntity.InnerOwnedEntity.Test2.Should().Be(TestDerivedEnum.One);
entity.OuterOwnedEntity.InnerOwnedEntity.Test3.Should().Be(TestStringEnum.Two);
entity.OuterOwnedEntity.InnerOwnedEntity.Test4.Should().Be(DerivedTestEnumWithValues1.B);
entity.OuterOwnedEntity.InnerOwnedEntity.NotMappedTest.Should().BeNull();

entity.OwnedEntities.Should().SatisfyRespectively(
o =>
Expand All @@ -174,6 +179,7 @@ private static void VerifyEntity(SomeEntity entity)
o.Test2.Should().Be(TestDerivedEnum.One);
o.Test3.Should().Be(TestStringEnum.Three);
o.Test4.Should().Be(DerivedTestEnumWithValues1.A);
o.NotMappedTest.Should().BeNull();
},
o =>
{
Expand All @@ -183,6 +189,7 @@ private static void VerifyEntity(SomeEntity entity)
o.Test2.Should().Be(TestDerivedEnum.One);
o.Test3.Should().Be(TestStringEnum.One);
o.Test4.Should().Be(DerivedTestEnumWithValues1.B);
o.NotMappedTest.Should().BeNull();
});
}
}
Expand Down

0 comments on commit d38ce33

Please sign in to comment.