Vogen and Guids #566
Replies: 4 comments 1 reply
-
Hi @drusellers, thanks for the question. I think Vogen 'hoists' up certain methods from the primitive type, e.g. |
Beta Was this translation helpful? Give feedback.
-
I think Thanks for your feedback! |
Beta Was this translation helpful? Give feedback.
-
This has now been implemented. Thanks for your feedback! |
Beta Was this translation helpful? Give feedback.
-
Static abstracts in interfaces allows us to define a factory for value objects of a specific type (or any type). This can be turned on in global config with the [assembly: VogenDefaults(staticAbstractsGeneration: StaticAbstractsGeneration.MostCommon)]
Once on, it will generate an public interface IVogen<TSelf, TPrimitive> where TSelf : IVogen<TSelf, TPrimitive>
{
static abstract explicit operator TSelf(TPrimitive value);
static abstract explicit operator TPrimitive(TSelf value);
static abstract bool operator ==(TSelf left, TSelf right);
static abstract bool operator !=(TSelf left, TSelf right);
static abstract bool operator ==(TSelf left, TPrimitive right);
static abstract bool operator !=(TSelf left, TPrimitive right);
static abstract bool operator ==(TPrimitive left, TSelf right);
static abstract bool operator !=(TPrimitive left, TSelf right);
static abstract TSelf From(TPrimitive value);
static abstract bool TryFrom(TPrimitive value, out TSelf vo);
} We can then implement that interface and use the static methods (such as public static class GuidFactory<TSelf> where TSelf : IVogen<TSelf, Guid>
{
// ReSharper disable once StaticMemberInGenericType
private static long _counter = DateTime.UtcNow.Ticks;
static TSelf NewSequential()
{
var guidBytes = Guid.NewGuid().ToByteArray();
var counterBytes = BitConverter.GetBytes(Interlocked.Increment(ref _counter));
if (!BitConverter.IsLittleEndian)
{
Array.Reverse(counterBytes);
}
guidBytes[08] = counterBytes[1];
guidBytes[09] = counterBytes[0];
guidBytes[10] = counterBytes[7];
guidBytes[11] = counterBytes[6];
guidBytes[12] = counterBytes[5];
guidBytes[13] = counterBytes[4];
guidBytes[14] = counterBytes[3];
guidBytes[15] = counterBytes[2];
return TSelf.From(new Guid(guidBytes));
}
} This can be used as follows: var id1 = GuidFactory<CustomerId>.NewSequential();
var id2 = GuidFactory<CustomerId>.NewSequential();
await Task.Delay(TimeSpan.FromMicroseconds(50));
var id3 = GuidFactory<SupplierId>.NewSequential();
var id4 = GuidFactory<SupplierId>.NewSequential();
Console.WriteLine($"Customer ID 1 = {id1}");
Console.WriteLine($"Customer ID 2 = {id2}");
Console.WriteLine($"Supplied ID 1 = {id3}");
Console.WriteLine($"Supplied ID 2 = {id4}"); Which outputs the following:
|
Beta Was this translation helpful? Give feedback.
-
If I'm using a Guid to back my ValueObjects like
Is there an easy way to add a
New()
type function? Something that would replicateBut ideally, I only have to configure it once using the Assembly Attributes
Conceptually, can Vogen do this as a standard with Guids
Beta Was this translation helpful? Give feedback.
All reactions