Skip to content

feeleen/StringEnum.Net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build status NuGet Badge

StringEnum.NET - Enumeration-like string values

C# .Net

.NET 5.0, .NET 6.0, .NET 7.0

Define your StringEnum clean and simple:

public class MyPet : StringEnum<MyPet>
{
	public static MyPet Monkey = New(); // = "Monkey" 
	public static MyPet Cat = New(); 
	public static MyPet Rabbit = New();
}

More initialization options:

public class MyPet : StringEnum<MyPet>
{
	public static MyPet Rabbit = New("Rabbits");
	public static MyPet Dog = New(EnumCase.Upper, "Dog"); // MyPet.Dog.ToString() -> "DOG"
	public static MyPet Ghost = New(null);  // handy when values in dataobject may have null values
	public static MyPet Empty = New(String.Empty);
}

Add additional properties for yout StringEnum:

public class MyFlower : StringEnum<MyFlower>
{
	public static MyFlower Rose = New().HasPropertyValue(x => x.AdditionalInfo, "Big");
	
	public static MyFlower Camomile = New()
		.HasPropertyValue(x => x.AdditionalInfo, "Small")
		.HasPropertyValue(x => x.DefaultQuantity, 15);
	
	//your custom properties here:
	public string AdditionalInfo { get; protected set; }
	public int? DefaultQuantity { get; protected set; }
}

Usage example:

var mouse = MyPet.Mouse;

if (mouse == "Mouse") // compare with string without .ToString()
{
	return true;
}

if (mouse == MyPet.Parse("mouse")) // parsing is case insensitive
{
	return true;
}


// implicit conversions example
string cat = MyPet.Cat;
Assert.IsTrue(cat == "Cat");

string dog = (MyPet)"Dog";
Assert.IsTrue(dog == "Dog");

JSON conversion example:

[JsonConverter(typeof(JsonStringEnumConverter<MyFlower>))]
public class MyFlower : StringEnum<MyFlower>
{
    public static MyFlower Rose = New();
    public static MyFlower Hibiscus = New();
}

public class GardenFlower
{
    public MyFlower FlowerType { get; set; }
    public int Quantity { get; set; }
}

string jsonData = "{'FlowerType' : 'Rose', 'Quantity' : 2 }";
var obj = JsonConvert.DeserializeObject<GardenFlower>(jsonData);

Assert.IsTrue(obj.FlowerType == "Rose");
Assert.IsTrue(obj.FlowerType == MyFlower.Rose);

TypeConverter and IConvertible support to work with strings:

[TypeConverter(typeof(StringEnumConverter<MyFlower>))] // default type converter
public class MyFlower : StringEnum<MyFlower>
{
    public static MyFlower Rose = New();
    public static MyFlower Hibiscus = New();
}

TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(MyFlower));
var rose = (MyFlower) typeConverter.ConvertFrom(null, Thread.CurrentThread.CurrentCulture, "Rose");

var fl = MyFlower.Hibiscus;
var res = Convert.ChangeType(fl, typeof(string));

Use with data entities

  1. Define your model:

    public class PersonType : StringEnum<PersonType>
    {
    	public static PersonType EM = New();
    	public static PersonType SP = New();
    	public static PersonType SC = New();
    	public static PersonType VC = New();
    	public static PersonType IN = New();
    	public static PersonType GC = New();
    }
    
    [Table(Name = "[Person].[Person]")]
    public class Person
    {
    	[PrimaryKey, Identity]
    	public int BusinessEntityID { get; set; }
    	[Column]
    	public PersonType PersonType { get; set; } // string values "EM", "SP", "SC" ... etc.
    }
  2. Setup type conversion via MappingSchema:

    var ms = new MappingSchema();
    _ = dbContext.AddMappingSchema(ms);
    
    var builder = ms.GetFluentMappingBuilder();

    and type conversion setup:

    _ = builder.Entity<Person>().Property(e => e.PersonType).HasConversion(v => v.Value, s => PersonType.Parse(s));
    ms.SetDataType(typeof(PersonType), DataType.VarChar);

    or

    ms.SetConverter<string, PersonType>(s => PersonType.Parse(s));
    ms.SetConverter<PersonType, DataParameter>(val => new DataParameter { Value = val, DataType = DataType.VarChar });
  3. Usage:

    // select records
    var persons = await db.GetTable<Person>().ToListAsync();
    
    // update record
    var pers = new Person() { PersonType = PersonType.VC, BusinessEntityID = 1675 };
    var res = await db.UpdateAsync(pers)

EF Core:

[Keyless]
public class PersonTitle : StringEnum<PersonTitle>
{
	public static PersonTitle Mr = New("Mr.");
	public static PersonTitle Ms = New("Ms.");
	public static PersonTitle Mss = New("Ms");
	public static PersonTitle Mrs = New("Mrs.");
	public static PersonTitle Sr = New("Sr.");
	public static PersonTitle Sra = New("Sra.");
	public static PersonTitle Undefined = New(null);
}

// setup converter:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
	modelBuilder
		.Entity<Person>()
		.Property(e => e.Title)
		.HasConversion(
			v => v.ToString(),
			v => PersonTitle.Parse(v));

	base.OnModelCreating(modelBuilder);
}