Skip to content

Commit

Permalink
Added samples for extensible enums
Browse files Browse the repository at this point in the history
  • Loading branch information
PawelGerr committed Feb 7, 2021
1 parent 15d3091 commit cae4040
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public IActionResult RoundTrip(ProductType productType)
return RoundTrip<ProductType, string>(productType);
}

[HttpGet("specialProductType/{specialProductType}")]
public IActionResult RoundTrip(SpecialProductType specialProductType)
{
return RoundTrip<SpecialProductType, string>(specialProductType);
}

[HttpGet("productTypeWithJsonConverter/{productType}")]
public IActionResult RoundTrip(ProductTypeWithJsonConverter productType)
{
Expand Down
6 changes: 6 additions & 0 deletions samples/Newtonsoft.Json.AspNetCore.Samples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public static async Task Main()
// http://localhost:5000/api/productType/invalid
// http://localhost:5000/api/productTypeWithJsonConverter/groceries
// http://localhost:5000/api/productTypeWithJsonConverter/invalid
// http://localhost:5000/api/specialProductType/groceries
// http://localhost:5000/api/specialProductType/special
// http://localhost:5000/api/specialProductType/invalid
// http://localhost:5000/api/productName/bread
// http://localhost:5000/api/productName/a
// http://localhost:5000/api/productNameWithModelBinder/bread
Expand All @@ -56,6 +59,9 @@ private static async Task DoHttpRequestsAsync(ILogger logger)
await DoRequestAsync(logger, client, "productType/invalid"); // invalid
await DoRequestAsync(logger, client, "productTypeWithJsonConverter/groceries");
await DoRequestAsync(logger, client, "productTypeWithJsonConverter/invalid"); // invalid
await DoRequestAsync(logger, client, "specialProductType/groceries");
await DoRequestAsync(logger, client, "specialProductType/special");
await DoRequestAsync(logger, client, "specialProductType/invalid"); // invalid
await DoRequestAsync(logger, client, "productName/bread");
await DoRequestAsync(logger, client, "productName/a"); // invalid
await DoRequestAsync(logger, client, "productNameWithModelBinder/bread");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public IActionResult RoundTrip(ProductType productType)
return RoundTrip<ProductType, string>(productType);
}

[HttpGet("specialProductType/{specialProductType}")]
public IActionResult RoundTrip(SpecialProductType specialProductType)
{
return RoundTrip<SpecialProductType, string>(specialProductType);
}

[HttpGet("productTypeWithJsonConverter/{productType}")]
public IActionResult RoundTrip(ProductTypeWithJsonConverter productType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public static async Task Main()
// http://localhost:5000/api/productType/invalid
// http://localhost:5000/api/productTypeWithJsonConverter/groceries
// http://localhost:5000/api/productTypeWithJsonConverter/invalid
// http://localhost:5000/api/specialProductType/groceries
// http://localhost:5000/api/specialProductType/special
// http://localhost:5000/api/specialProductType/invalid
// http://localhost:5000/api/productName/bread
// http://localhost:5000/api/productName/a
// http://localhost:5000/api/productNameWithModelBinder/bread
Expand All @@ -59,6 +62,9 @@ private static async Task DoHttpRequestsAsync(ILogger logger)
await DoRequestAsync(logger, client, "productType/invalid"); // invalid
await DoRequestAsync(logger, client, "productTypeWithJsonConverter/groceries");
await DoRequestAsync(logger, client, "productTypeWithJsonConverter/invalid"); // invalid
await DoRequestAsync(logger, client, "specialProductType/groceries");
await DoRequestAsync(logger, client, "specialProductType/special");
await DoRequestAsync(logger, client, "specialProductType/invalid"); // invalid
await DoRequestAsync(logger, client, "productName/bread");
await DoRequestAsync(logger, client, "productName/a"); // invalid
await DoRequestAsync(logger, client, "productNameWithModelBinder/bread");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ public class Product
public Guid Id { get; private set; }
public ProductName Name { get; private set; }
public ProductCategory Category { get; private set; }
public SpecialProductType ProductType { get; private set; }
public Boundary Boundary { get; private set; }

// For EF (see also https://github.com/dotnet/efcore/issues/12078)
#pragma warning disable 8618
private Product(Guid id, ProductName name, ProductCategory category)
private Product(Guid id, ProductName name, ProductCategory category, SpecialProductType productType)
{
Id = id;
Name = name;
Category = category;
ProductType = productType;
}
#pragma warning restore 8618

public Product(Guid id, ProductName name, ProductCategory category, Boundary boundary)
: this(id, name, category)
public Product(Guid id, ProductName name, ProductCategory category, SpecialProductType productType, Boundary boundary)
: this(id, name, category, productType)
{
Boundary = boundary;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Core;
using Serilog.Events;
using Thinktecture.EnumLikeClasses;
using Thinktecture.ValueTypes;

Expand All @@ -12,16 +14,18 @@ public class Program
{
public static void Main()
{
var loggerFactory = GetLoggerFactory();
var loggerFactory = GetLoggerFactory(out var loggingLevelSwitch);
var logger = loggerFactory.CreateLogger<Program>();

using var ctx = CreateContext(loggerFactory);

InsertProduct(ctx, new Product(Guid.NewGuid(), ProductName.Create("Apple"), ProductCategory.Fruits, Boundary.Create(1, 2)));
InsertProduct(ctx, new Product(Guid.NewGuid(), ProductName.Create("Apple"), ProductCategory.Fruits, SpecialProductType.Special, Boundary.Create(1, 2)));

try
{
InsertProduct(ctx, new Product(Guid.NewGuid(), ProductName.Create("Pear"), ProductCategory.Get("Invalid Category"), Boundary.Create(1, 2)));
loggingLevelSwitch.MinimumLevel = LogEventLevel.Fatal;
InsertProduct(ctx, new Product(Guid.NewGuid(), ProductName.Create("Pear"), ProductCategory.Get("Invalid Category"), SpecialProductType.Special, Boundary.Create(1, 2)));
loggingLevelSwitch.MinimumLevel = LogEventLevel.Information;
}
catch (DbUpdateException)
{
Expand Down Expand Up @@ -59,12 +63,15 @@ private static ProductsDbContext CreateContext(ILoggerFactory loggerFactory)
return ctx;
}

private static ILoggerFactory GetLoggerFactory()
private static ILoggerFactory GetLoggerFactory(out LoggingLevelSwitch loggingLevelSwitch)
{
loggingLevelSwitch = new LoggingLevelSwitch();

var serilog = new LoggerConfiguration()
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] [{SourceContext}] {Message:lj}{NewLine}{Exception}")
.Destructure.AsScalar<ProductCategory>()
.Destructure.AsScalar<ProductName>()
.MinimumLevel.ControlledBy(loggingLevelSwitch)
.CreateLogger();

return new LoggerFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ private static void DemoForNonValidatableEnum(ILogger logger)
{
logger.Information("==== Demo for IEnum<T> ====");

var productTypes = ProductType.Items;
logger.Information("Product types: {Types}", productTypes);
logger.Information("Product types: {Types}", ProductType.Items);
logger.Information("Special product types: {Types}", SpecialProductType.Items);

var productType = ProductType.Get("Groceries");
logger.Information("Product type: {Type}", productType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Thinktecture.EnumLikeClasses
{
public sealed partial class ProductType : IEnum<string>
[EnumGeneration(IsExtensible = true)]
public partial class ProductType : IEnum<string>
{
public static readonly ProductType Groceries = new("Groceries");
public static readonly ProductType Housewares = new("Housewares");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Thinktecture.EnumLikeClasses
{
[EnumGeneration]
public sealed partial class SpecialProductType : ProductType
{
public static readonly SpecialProductType Special = new("Special");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public static void Demo(ILogger logger)
ProductName.Create(" ");
logger.Warning("This line won't be reached.");
}
catch (ArgumentException)
catch (ValidationException)
{
logger.Information("ArgumentException is thrown because a product name cannot be an empty string.");
logger.Information("ValidationException is thrown because a product name cannot be an empty string.");
}

var validationResult = ProductName.TryCreate("Milk", out var milk);
Expand Down

0 comments on commit cae4040

Please sign in to comment.