Skip to content
Merged
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,15 @@ To install the minimum requirements:
Install-Package Ardalis.SmartEnum
```

To install support for serialization, select the lines that apply:
To install support for serialization, AutoFixture or EF Core select the lines that apply:

```
Install-Package Ardalis.SmartEnum.AutoFixture
Install-Package Ardalis.SmartEnum.JsonNet
Install-Package Ardalis.SmartEnum.Utf8Json
Install-Package Ardalis.SmartEnum.MessagePack
Install-Package Ardalis.SmartEnum.ProtoBufNet
Install-Package Ardalis.SmartEnum.EFCore
```

## Usage
Expand Down Expand Up @@ -339,6 +340,19 @@ protected override void OnModelCreating(ModelBuilder builder)
}
```

#### Using SmartEnum.EFCore

If you have installed `Ardalis.SmartEnum.EFCore` it is sufficient to add the following line at the end of the `OnModelCreating` method:

```csharp
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
...

modelBuilder.ConfigureSmartEnum();
}
```

## AutoFixture support

New instance of a `SmartEnum` should not be created. Instead, references to the existing ones should always be used. [AutoFixture](https://github.com/AutoFixture/AutoFixture) by default doesn't know how to do this. The `Ardalis.SmartEnum.AutoFixture` package includes a specimen builder for `SmartEnum`. Simply add the customization to the `IFixture` builder:
Expand Down
21 changes: 19 additions & 2 deletions SmartEnum.sln
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{FA199ECB-5F2
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks", "{5952C160-ABAA-4302-9150-0928E82545B1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartEnum.SystemTextJson", "src\SmartEnum.SystemTextJson\SmartEnum.SystemTextJson.csproj", "{A7F1C0E7-F641-4800-98DA-FA7A7B7B76E9}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartEnum.SystemTextJson", "src\SmartEnum.SystemTextJson\SmartEnum.SystemTextJson.csproj", "{A7F1C0E7-F641-4800-98DA-FA7A7B7B76E9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmartEnum.SystemTextJson.UnitTests", "test\SmartEnum.SystemTextJson.UnitTests\SmartEnum.SystemTextJson.UnitTests.csproj", "{3EBD6CA5-CF2C-4350-922E-CEE2AE01445A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartEnum.SystemTextJson.UnitTests", "test\SmartEnum.SystemTextJson.UnitTests\SmartEnum.SystemTextJson.UnitTests.csproj", "{3EBD6CA5-CF2C-4350-922E-CEE2AE01445A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartEnum.EFCore", "src\SmartEnum.EFCore\SmartEnum.EFCore.csproj", "{CDA8EF6E-F678-4FD3-80B0-5C61FC76EE51}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SmartEnum.EFCore.IntegrationTests", "src\SmartEnum.EFCore.UnitTests\SmartEnum.EFCore.IntegrationTests.csproj", "{DF5E3B19-AE8E-48A4-B023-4FA42D6EA8FF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "IntegrationTests", "IntegrationTests", "{EF5634F4-4667-4481-934C-D1CFA042AD0B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -120,6 +126,14 @@ Global
{3EBD6CA5-CF2C-4350-922E-CEE2AE01445A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EBD6CA5-CF2C-4350-922E-CEE2AE01445A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EBD6CA5-CF2C-4350-922E-CEE2AE01445A}.Release|Any CPU.Build.0 = Release|Any CPU
{CDA8EF6E-F678-4FD3-80B0-5C61FC76EE51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDA8EF6E-F678-4FD3-80B0-5C61FC76EE51}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDA8EF6E-F678-4FD3-80B0-5C61FC76EE51}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDA8EF6E-F678-4FD3-80B0-5C61FC76EE51}.Release|Any CPU.Build.0 = Release|Any CPU
{DF5E3B19-AE8E-48A4-B023-4FA42D6EA8FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF5E3B19-AE8E-48A4-B023-4FA42D6EA8FF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF5E3B19-AE8E-48A4-B023-4FA42D6EA8FF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF5E3B19-AE8E-48A4-B023-4FA42D6EA8FF}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -140,6 +154,9 @@ Global
{66EF3D1A-32AE-4B28-BC3E-7441615722A9} = {79268877-BBEF-4DE2-B8D9-697F21933159}
{A7F1C0E7-F641-4800-98DA-FA7A7B7B76E9} = {FA199ECB-5F29-442A-AAC6-91DBCB7A5A04}
{3EBD6CA5-CF2C-4350-922E-CEE2AE01445A} = {79268877-BBEF-4DE2-B8D9-697F21933159}
{CDA8EF6E-F678-4FD3-80B0-5C61FC76EE51} = {FA199ECB-5F29-442A-AAC6-91DBCB7A5A04}
{DF5E3B19-AE8E-48A4-B023-4FA42D6EA8FF} = {EF5634F4-4667-4481-934C-D1CFA042AD0B}
{EF5634F4-4667-4481-934C-D1CFA042AD0B} = {79268877-BBEF-4DE2-B8D9-697F21933159}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {46896DE3-41B8-442F-A6FB-6AC9F11CCBCE}
Expand Down
13 changes: 13 additions & 0 deletions src/SmartEnum.EFCore.UnitTests/DbContext/Entities/SomeEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SmartEnum.EFCore.IntegrationTests.DbContext.Entities
{
public class SomeEntity
{
public int Id { get; set; }

public Weekday Weekday { get; set; }
}
}
31 changes: 31 additions & 0 deletions src/SmartEnum.EFCore.UnitTests/DbContext/TestDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using SmartEnum.EFCore.IntegrationTests.DbContext.Entities;
using System;
using System.Collections.Generic;
using System.Text;

namespace SmartEnum.EFCore.IntegrationTests.DbContext
{
public class TestDbContext : Microsoft.EntityFrameworkCore.DbContext
{
public TestDbContext(DbContextOptions<TestDbContext> options) : base(options)
{ }

public TestDbContext()
{
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
optionsBuilder.UseSqlite("Data Source=:memory:;");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ConfigureSmartEnum();
}

public DbSet<SomeEntity> SomeEntities { get; set; }
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace SmartEnum.EFCore.IntegrationTests.Migrations
{
public partial class Initial : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "SomeEntities",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Weekday = table.Column<int>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_SomeEntities", x => x.Id);
});
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "SomeEntities");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SmartEnum.EFCore.IntegrationTests.DbContext;

namespace SmartEnum.EFCore.IntegrationTests.Migrations
{
[DbContext(typeof(TestDbContext))]
partial class TestDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062");

modelBuilder.Entity("SmartEnum.EFCore.IntegrationTests.DbContext.Entities.SomeEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();

b.Property<int?>("Weekday");

b.HasKey("Id");

b.ToTable("SomeEntities");
});
#pragma warning restore 612, 618
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.2.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.2.4" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="FluentAssertions" Version="5.5.3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SmartEnum.EFCore\SmartEnum.EFCore.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions src/SmartEnum.EFCore.UnitTests/SmartEnumEFCoreDbTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using FluentAssertions;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using SmartEnum.EFCore.IntegrationTests.DbContext;
using SmartEnum.EFCore.IntegrationTests.DbContext.Entities;
using System;
using System.Linq;
using Xunit;

namespace SmartEnum.EFCore.IntegrationTests
{
public class SmartEnumEFCoreDbTests
{
[Fact]
public void Test_SaveAndRetrieveData()
{
// In-memory database only exists while the connection is open
using (var connection = new SqliteConnection("DataSource=:memory:"))
{
connection.Open();

var options = new DbContextOptionsBuilder<TestDbContext>()
.UseSqlite(connection)
.Options;

using (var db = new TestDbContext(options))
{
db.Database.EnsureCreated();

db.SomeEntities.Add(new SomeEntity
{
Weekday = Weekday.Thursday,
});

db.SaveChanges();
}

using (var db = new TestDbContext(options))
{
var entities = db.SomeEntities.ToList();

entities.Count.Should().Be(1);
entities.Single().Weekday.Should().Be(Weekday.Thursday);
}
}
}
}
}
22 changes: 22 additions & 0 deletions src/SmartEnum.EFCore.UnitTests/Weekday.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Ardalis.SmartEnum;
using System;
using System.Collections.Generic;
using System.Text;

namespace SmartEnum.EFCore.IntegrationTests
{
public sealed class Weekday : SmartEnum<Weekday, int>
{
public static readonly Weekday Monday = new Weekday(nameof(Monday), 1);
public static readonly Weekday Tuesday = new Weekday(nameof(Tuesday), 2);
public static readonly Weekday Wednesday = new Weekday(nameof(Wednesday), 3);
public static readonly Weekday Thursday = new Weekday(nameof(Thursday), 4);
public static readonly Weekday Friday = new Weekday(nameof(Friday), 5);
public static readonly Weekday Saturday = new Weekday(nameof(Saturday), 6);
public static readonly Weekday Sunday = new Weekday(nameof(Sunday), 7);

private Weekday(string name, int value) : base(name, value)
{
}
}
}
29 changes: 29 additions & 0 deletions src/SmartEnum.EFCore/SmartEnum.EFCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<PackageId>Ardalis.SmartEnum.EFCore</PackageId>
<Title>Ardalis.SmartEnum.EFCore</Title>
<Company>Ardalis.com</Company>
<Summary>EFCore support for Ardalis.SmartEnum.</Summary>
<PackageTags>enum;smartenum;netstandard2.0;entityframeworkcore</PackageTags>
<PackageReleaseNotes></PackageReleaseNotes>
<Version>1.0.0</Version>
<AssemblyName>Ardalis.SmartEnum.EFCore</AssemblyName>
<RootNamespace>Ardalis.SmartEnum.EFCore</RootNamespace>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<LangVersion>7.3</LangVersion>
<Features>strict</Features>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\SmartEnum\SmartEnum.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
</ItemGroup>
</Project>
Loading