andrewlock / StronglyTypedId

A Rosyln-powered generator for strongly-typed IDs
MIT License
1.52k stars 80 forks source link

Setting to disable struct default Constructor generation. #75

Closed panoukos41 closed 9 months ago

panoukos41 commented 2 years ago

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 commented 2 years ago

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

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 commented 9 months ago

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 🙂