SteveDunn / Vogen

A semi-opinionated library which is a source generator and a code analyser. It Source generates Value Objects
Apache License 2.0
869 stars 45 forks source link

Allow multiple user pre-configured `Configurations` for ValueObjects #331

Open jeffward01 opened 1 year ago

jeffward01 commented 1 year ago

Describe the feature

Hello all,

Feature Title: Multiple pre-defined configurations

Summary:

By default, Vogen is configured with int type. (Details here)

Current

Proposed

Details

This feature request is an expansion on the existing feature 'Configuration' of Vogen.

Existing Feature:

In the current implementation, we can utilize an AssemblyInfo.cs class for pre-configuration of Vogen Value Objects, such as:

// This is my AssemblyInfo.cs file
//--------------------
// Code starts below
//--------------------

// Set the defaults for the project

using Vogen;

[assembly:
    VogenDefaults(
        typeof(Guid),
        Conversions.EfCoreValueConverter | Conversions.NewtonsoftJson | Conversions.TypeConverter |
        Conversions.SystemTextJson,
        typeof(ValueObjectValidationException))]

Proposed Feature (Pseudo code)

Please take note of these classes:

// This is my AssemblyInfo.cs file
//--------------------
// Code starts below
//--------------------

// Set the defaults for the project

using Vogen;

// [ValueObject]    <--- When this attribute is used, the below config is applied
[assembly:
    VogenDefaults(
        typeof(Guid),
        Conversions.EfCoreValueConverter | Conversions.NewtonsoftJson | Conversions.TypeConverter |
        Conversions.SystemTextJson,
        typeof(ValueObjectValidationException))]

// This example here might be confusing,.... or maybe its handy. 
// [ValueObject<Guid>]     <--- When this attribute is used, the below config is applied
[assembly:
    VogenConfigs(
        typeof(Guid),
    Conversions.TypeConverter | Conversions.NewtonsoftJson ,
        typeof(SomeOtherErrorException))]

// [ValueObject<string>]   <--- When this attribute is used, the below config is applied
[assembly:
    VogenConfigs(
        typeof(string),
        Conversions.SystemTextJson,
        typeof(RandomException))]

//  [ValueObject<int>]       <--- When this attribute is used, the below config is applied
[assembly:
    VogenConfigs(
        typeof(int),
        Conversions.EfCoreValueConverter | Conversions.NewtonsoftJson 
        Conversions.SystemTextJson,
        typeof(OtherException))]

// I don't really like using string literals.  Perhaps we can extract these to a type like what is suggested ==>
// https://github.com/SteveDunn/Vogen/issues/327
//----
// The example implementation would be:
// [ValueObject<int, MyStronglyTypedNameConfig>]  <-- Jeff votes for this

// / [ValueObject<int, MyStronglyTypedNameConfig>]     <--- When this attribute is used, the below config is applied
// or     
//  [ValueObject<int>("MyStronglyTypedName")]     <--- When this attribute is used, the below config is applied
[assembly:
    VogenConfigs(
     "MyStronglyTypedName",  typeof(int),
        Conversions.EfCoreValueConverter | Conversions.NewtonsoftJson | Conversions.TypeConverter |
        Conversions.SystemTextJson,
        typeof(XyzException))]

// / [ValueObject<int, MyOtherStronglyTypedNameConfig>]  <--- When this attribute is used, the below config is applied
// or 
//  [ValueObject<int>("MyOtherStronglyTypedName")]   <--- When this attribute is used, the below config is applied
[assembly:
    VogenConfigs(
     "MyOtherStronglyTypedName",  typeof(int),
        Conversions.EfCoreValueConverter
        Conversions.SystemTextJson,
        typeof(AbcException))]

Please let me know what you guys think, and if there are any questions or suggestions, please feel free to be chatty about it.

Thanks so much!

SteveDunn commented 1 year ago

Thanks @jeffward01 - certainly something to think about. I'm a bit snowed under at the moment, but this could certainly be a useful addition! Thanks again.