Пример #1
0
    private static void DemoForNonValidatableEnum(ILogger logger)
    {
        logger.Information("==== Demo for IEnum<T> ====");

        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);

        productType = (ProductType)"Groceries";
        logger.Information("Explicitly casted product type: {Type}", productType);

        if (ProductType.TryGet("Housewares", out var housewares))
        {
            logger.Information("Product type {Type} with TryGet found", housewares);
        }

        string keyOfTheProductType = productType;

        logger.Information("Implicit conversion of ProductType -> string: {Key}", keyOfTheProductType);

        try
        {
            ProductType.Get("Unknown");
            logger.Warning("This line won't be reached.");
        }
        catch (UnknownEnumIdentifierException)
        {
            logger.Information("UnknownEnumIdentifierException is thrown because there is no product type with the key 'Unknown'.");
        }
    }
Пример #2
0
        private void ConfigureProduct(EntityTypeBuilder <Product> entity)
        {
            entity.Property(x => x.Name).IsRequired();

            entity.Property(x => x.Type)
            .IsRequired()
            .HasConversion(
                g => g.Value,
                y => ProductType.Get(y));
        }