Skip to content

Instantly share code, notes, and snippets.

@Kahbazi
Last active September 4, 2023 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kahbazi/dc44ec2de17675945c6a4cee79d80a82 to your computer and use it in GitHub Desktop.
Save Kahbazi/dc44ec2de17675945c6a4cee79d80a82 to your computer and use it in GitHub Desktop.
Value Object In Asp.Net Core
public sealed class AppDbContext : DbContext
{
public DbSet<Entity> Entities { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Entity>()
.Property(x => x.VeryVeryCool)
.HasConversion(CoolStringValueConverter.Instance);
}
}
public readonly struct CoolString
{
private static readonly Dictionary<char, char> _chars = new Dictionary<char, char>
{
['0'] = '5',
['1'] = '6',
['2'] = '7',
['3'] = '8',
['4'] = '9',
};
public string? Value { get; }
public CoolString(string? value)
{
Value = ReplaceSpecialChars(value);
}
public static implicit operator CoolString(string? value)
{
return new CoolString(value);
}
public static explicit operator string?(CoolString value)
{
return value.Value;
}
public static string? ReplaceSpecialChars(string? value)
{
if (value is null)
{
return null;
}
char[]? result = null;
var flag = false;
for (var i = 0; i < value.Length; i++)
{
char c = value[i];
if (_chars.TryGetValue(c, out var toBeReplaced))
{
result ??= new char[value.Length];
result[i] = toBeReplaced;
if (!flag)
{
for (var j = 0; j < i; j++)
{
result[i] = value[j];
}
}
flag = true;
}
else if (flag)
{
result![i] = c;
}
}
if (result is null)
{
return value;
}
else
{
return new string(result);
}
}
}
public sealed class CoolStringJsonConverter : JsonConverter<CoolString>
{
public override CoolString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new CoolString(reader.GetString());
}
public override void Write(Utf8JsonWriter writer, CoolString coolString, JsonSerializerOptions options)
{
writer.WriteStringValue(coolString.Value);
}
}
public sealed class CoolStringValueConverter : ValueConverter<CoolString, string?>
{
public readonly static CoolStringValueConverter Instance = new CoolStringValueConverter();
private CoolStringValueConverter()
: base(_convertTo, _convertFrom, null)
{
}
private static readonly Expression<Func<string?, CoolString>> _convertFrom = (string? value) => new CoolString(value);
private static readonly Expression<Func<CoolString, string?>> _convertTo = (CoolString coolString) => coolString.Value;
}
public sealed class Entity
{
public CoolString VeryVeryCool { get; set; }
}
var builder = WebApplication.CreateBuilder(args);
builder.Services.ConfigureHttpJsonOptions(jsonOptions =>
{
jsonOptions.SerializerOptions.Converters.Add(new CoolStringJsonConverter());
});
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(a => a.MapType<CoolString>(() =>
new OpenApiSchema
{
Type = "string"
}
));
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI();
app.MapPost("/", (Entity dto) =>
{
return dto.VeryVeryCool;
});
app.Run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment