Vogen icon indicating copy to clipboard operation
Vogen copied to clipboard

Disable From method generation

Open AthenaAzuraeaX opened this issue 1 year ago • 2 comments

Describe the feature

It is currently possible to disable generation of the TryFrom method. Would it be possible to make it possible to disable generation of the From method, as well? The ctor should remain private. Mostly the reason is because I would like to handle validation myself, while allowing Vogen to generate the code for a Value object.

[System.Diagnostics.DebuggerStepThrough]
internal readonly partial struct TestUnit
{
	public readonly partial struct Factory
	{
		public TestUnit From(ValidationPath Path, LanguageExt.Option<System.Int32> Value) => /* Do some validation and throw on error */ new TestUnit(Value);
		public LanguageExt.Either<IValidationResultCollection_V2, TestUnit> TryFrom(ValidationPath Path, LanguageExt.Option<System.Int32> Value) => /* Do some validation and return an error object on error */ new TestUnit(Value);
	}
}

Currently I have to disable the Validation method by throwing an exception from it to prevent instantiating objects using TestUnit.From.

The reason I really don't want From or TryFrom is because they're static methods, and static methods are difficult to test and use, especially for validation where one might want to inject data into the factories.

AthenaAzuraeaX avatar Sep 24 '24 13:09 AthenaAzuraeaX

Hi @AthenaAzuraeaX - thanks for the suggestion. This would be quite a big change for Vogen.

There are ways to extend the types via static abstracts in interfaces. There's a tutorial here that could be extended and adapted for what you require.

SteveDunn avatar Sep 25 '24 04:09 SteveDunn

Hi @SteveDunn

I'm aware of static interfaces, but purposely opt to not use them due to downsides and limits. But also there is the fact that Vogen's current From method doesn't support the interface we need. Hence, Vogen adding the From method bypasses all our own logic that we need or want.

Hence, the only way for us to use Vogen right now is to add a Validation method to all types that throws an exception. Not a great idea, but since there is no other way to disable the From method, that's what we have to do. That is why I thought it would be nice to disable the From method generation.

AthenaAzuraeaX avatar Sep 27 '24 10:09 AthenaAzuraeaX