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

Setting to disable struct default Constructor generation. #75

Closed
panoukos41 opened this issue Jul 23, 2022 · 2 comments · Fixed by #117
Closed

Setting to disable struct default Constructor generation. #75

panoukos41 opened this issue Jul 23, 2022 · 2 comments · Fixed by #117

Comments

@panoukos41
Copy link

My proposal is to add a boolean switch on the [StronglyTypedId] attribute that would indicate if the generator should generate the default constructor that accepts the BackingType value.

That way someone could provide his own custom constructor that accepts the BackingType and allow for further customisation of the final Value property.

I found myself in need of this actually only on the string type so far since I wanted to do some extra work to some ids.

If this sounds okay with you I would love to sit and try to implement it myself when I have some time 😸

@NxSoftware
Copy link
Contributor

+1 for this but I'd propose an enum that allows for:

  • the current behaviour of generating a public constructor (probably the default)
  • generate a private constructor (with the implementation otherwise as-is)
  • completely disable constructor generation

My idea behind a private constructor would allow for static factory method(s) to be used for creating instances using a pattern similar to Guid.TryParse

Example

[StronglyTypedId(constructor: StronglyTypedIdConstructor.Private)]
partial struct MyStrongString
{
    public static bool TryParse(string value, out MyStrongString? result)
    {
        if (CanParse) // Pseudo-code
        {
            result = new MyStrongString(value);
            return true;
        }

        result = null;
        return false;
    }
}

@andrewlock
Copy link
Owner

Sorry for the delay with this 😳. To be honest I got overwhelmed with how to handle the big range of feature requests and options. I think I've found an answer in the big redesign of the library in this PR:

The main idea is to make the library much more maintainable while also giving people a mechanism to customise the generated IDs as much as they like. This will make it easy to make changes like this 🙂

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

Successfully merging a pull request may close this issue.

3 participants