Vogen icon indicating copy to clipboard operation
Vogen copied to clipboard

Make project non-dependent on Vogen

Open johnwc opened this issue 6 months ago • 5 comments

Describe the feature

Is there a way to have this package create the generated code but not be dependent on the Vogen package. So other projects using our package doesn't also require referencing Vogen?

johnwc avatar Jun 09 '25 01:06 johnwc

Hi @johnwc - other projects using your package wouldn't necessarily need Vogen, but if they didn't reference it, then they wouldn't get code analysis goodness.

SteveDunn avatar Jun 19 '25 12:06 SteveDunn

@SteveDunn what code analysis goodness?

johnwc avatar Jun 19 '25 15:06 johnwc

There's various analysers that offer compiler warnings for things like newing up a value object, or defaulting a value object.

SteveDunn avatar Jun 19 '25 17:06 SteveDunn

I have not seen these show up during my development, can you give short examples of some that I can paste into my code to see? Currently we only have the following one to test how well it works with EF. How would I use this incorrectly in code to have it show the analysis warnings?

[ValueObject<int>(conversions: Conversions.Default | Conversions.EfCoreValueConverter)]
public partial struct AccountId;

johnwc avatar Jun 19 '25 21:06 johnwc

Here's some examples

// catches object creation expressions
var c = new CustomerId(); // error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited
CustomerId c = default; // error VOG009: Type 'CustomerId' cannot be constructed with default as it is prohibited.

var c = default(CustomerId); // error VOG009: Type 'CustomerId' cannot be constructed with default as it is prohibited.
var c = GetCustomerId(); // error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited

var c = Activator.CreateInstance<CustomerId>(); // error VOG025: Type 'CustomerId' cannot be constructed via Reflection as it is prohibited.
var c = Activator.CreateInstance(<CustomerId>); // error VOG025: Type 'MyVo' cannot be constructed via Reflection as 
it is prohibited

// catches lambda expressions
Func<CustomerId> f = () => default; // error VOG009: Type 'CustomerId' cannot be constructed with default as it is prohibited.

// catches method / local function return expressions
CustomerId GetCustomerId() => default; // error VOG009: Type 'CustomerId' cannot be constructed with default as it is prohibited.
CustomerId GetCustomerId() => new CustomerId(); // error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited
CustomerId GetCustomerId() => new(); // error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited

// catches argument / parameter expressions
Task<CustomerId> t = Task.FromResult<CustomerId>(new()); // error VOG010: Type 'CustomerId' cannot be constructed with 'new' as it is prohibited

void Process(CustomerId customerId = default) { } // error VOG009: Type 'CustomerId' cannot be constructed with default as it is prohibited.```

SteveDunn avatar Jun 20 '25 11:06 SteveDunn