Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable From method generation #671

Open
AthenaAzuraeaX opened this issue Sep 24, 2024 · 2 comments
Open

Disable From method generation #671

AthenaAzuraeaX opened this issue Sep 24, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@AthenaAzuraeaX
Copy link

AthenaAzuraeaX commented Sep 24, 2024

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 AthenaAzuraeaX added the enhancement New feature or request label Sep 24, 2024
@SteveDunn
Copy link
Owner

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.

@AthenaAzuraeaX
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants