太卜『符玄』
太卜『符玄』
CC#
Created by Victor H on 3/2/2025 in #help
Generic static factories
QwQ,You're welcome
13 replies
CC#
Created by Victor H on 3/2/2025 in #help
Generic static factories
avoid reflection, you can use factory method.
public abstract record AggregateId<TId>(Guid Value) : IStronglyTypedId<Guid, TId>
where TId : IStronglyTypedId<Guid, TId>
{
private static Func<Guid, TId> _factory;

public static TId From(Guid value) => _factory(value);

protected static void SetFactory(Func<Guid, TId> factory)
{
_factory = factory;
}
}
public abstract record AggregateId<TId>(Guid Value) : IStronglyTypedId<Guid, TId>
where TId : IStronglyTypedId<Guid, TId>
{
private static Func<Guid, TId> _factory;

public static TId From(Guid value) => _factory(value);

protected static void SetFactory(Func<Guid, TId> factory)
{
_factory = factory;
}
}
derived it:
public record ConcreteAggregateId(Guid Value) : AggregateId<ConcreteAggregateId>(Value)
{
static ConcreteAggregateId()
{
SetFactory(value => new ConcreteAggregateId(value));
}
}
public record ConcreteAggregateId(Guid Value) : AggregateId<ConcreteAggregateId>(Value)
{
static ConcreteAggregateId()
{
SetFactory(value => new ConcreteAggregateId(value));
}
}
13 replies
CC#
Created by Victor H on 3/2/2025 in #help
Generic static factories
if it must be static, you can derived it.
public abstract record AggregateId<TId>(Guid Value) : IStronglyTypedId<Guid, TId>
where TId : IStronglyTypedId<Guid, TId>
{
public static TId From(Guid value)
{
return Activator.CreateInstance<TId>(value);
}
}

public record ConcreteAggregateId(Guid Value) : AggregateId<ConcreteAggregateId>(Value)
{
public static new ConcreteAggregateId From(Guid value) => new ConcreteAggregateId(value);
}
public abstract record AggregateId<TId>(Guid Value) : IStronglyTypedId<Guid, TId>
where TId : IStronglyTypedId<Guid, TId>
{
public static TId From(Guid value)
{
return Activator.CreateInstance<TId>(value);
}
}

public record ConcreteAggregateId(Guid Value) : AggregateId<ConcreteAggregateId>(Value)
{
public static new ConcreteAggregateId From(Guid value) => new ConcreteAggregateId(value);
}
13 replies
CC#
Created by Victor H on 3/2/2025 in #help
Generic static factories
oh, I see. sorry
13 replies
CC#
Created by Victor H on 3/2/2025 in #help
Generic static factories
or , if you want use reflection.
public abstract record AggregateId<TId>(Guid Value) : IStronglyTypedId<Guid, TId>
where TId : IStronglyTypedId<Guid, TId>
{
public static TId From(Guid value)
{
var constructor = typeof(TId).GetConstructors()
.FirstOrDefault(c => c.GetParameters().Length == 1 && c.GetParameters()[0].ParameterType == typeof(Guid));
if (constructor == null)
{
throw new InvalidOperationException($"No suitable constructor found for type {typeof(TId).Name}");
}

return (TId)constructor.Invoke(new object[] { value });
}
}
public abstract record AggregateId<TId>(Guid Value) : IStronglyTypedId<Guid, TId>
where TId : IStronglyTypedId<Guid, TId>
{
public static TId From(Guid value)
{
var constructor = typeof(TId).GetConstructors()
.FirstOrDefault(c => c.GetParameters().Length == 1 && c.GetParameters()[0].ParameterType == typeof(Guid));
if (constructor == null)
{
throw new InvalidOperationException($"No suitable constructor found for type {typeof(TId).Name}");
}

return (TId)constructor.Invoke(new object[] { value });
}
}
13 replies
CC#
Created by Victor H on 3/2/2025 in #help
Generic static factories
so avoid to invoke From, you can:
public abstract record AggregateId<TId>(Guid Value) : IStronglyTypedId<Guid, TId>
where TId : IStronglyTypedId<Guid, TId>
{
public static TId From(Guid value) => Create(value);

protected abstract static TId Create(Guid value);
}
public abstract record AggregateId<TId>(Guid Value) : IStronglyTypedId<Guid, TId>
where TId : IStronglyTypedId<Guid, TId>
{
public static TId From(Guid value) => Create(value);

protected abstract static TId Create(Guid value);
}
public record MyAggregateId(Guid Value) : AggregateId<MyAggregateId>(Value)
{
protected override static MyAggregateId Create(Guid value) => new MyAggregateId(value);
}
public record MyAggregateId(Guid Value) : AggregateId<MyAggregateId>(Value)
{
protected override static MyAggregateId Create(Guid value) => new MyAggregateId(value);
}
13 replies
CC#
Created by Victor H on 3/2/2025 in #help
Generic static factories
Hello! The problem you have is that in the abstract record AggregateId<TId>, the implementation of the From method calls itself, resulting in infinite recursion. Specifically, TSelf.From(value) actually calls the From method of the current class instead of the concrete subclass implementation, which results in recursive calls and eventually throws a stack overflow.
13 replies
CC#
Created by ayjay on 3/2/2025 in #help
Designing a Scalable Booking .Net API – Best Practices & Input Needed
umm
14 replies