dadhi / DryIoc

DryIoc is fast, small, full-featured IoC Container for .NET
MIT License
988 stars 122 forks source link

Required Properties support by default #563

Open dadhi opened 1 year ago

dadhi commented 1 year ago

Discussed in https://github.com/dadhi/DryIoc/discussions/562

Originally posted by **TonyValenti** March 16, 2023 AutoFac recently implemented required properties injection and it is awesome! https://autofac.readthedocs.io/en/latest/register/prop-method-injection.html#required-properties This eliminates a ton of boilerplate constructor code, especially where inheritance is involved. Dryloc should follow suit.
TonyValenti commented 1 year ago

Also, just so you know, AutoFac looks at the attributes that C# decorates classes and properties with so that no extra properties or attributes are necessary. It doesn't set all properties. Just the required ones which is really nice.

dadhi commented 1 year ago

@TonyValenti got it

TonyValenti commented 1 year ago

Any idea when this will be released?

dadhi commented 1 year ago

Here is the example how to configure the required properties support for DryIoc v5.4.0 (currently in preview)

TonyValenti commented 1 year ago

Hi @dadhi , Would you consider making required properties on by default? It would be really great if Dryloc was compatible with Autofac in this way so that folks could simply adopt it and it just work. Also, if it was on by default, it would allow you to promote additional lines of code saved because all the overhead of constructor chaining then goes away automatically.

dadhi commented 1 year ago

@TonyValenti For the next version v5.4.0 you need to specify the support for the required properties via:

var c = new Container(rules => rules.With(propertiesAndFields: PropertiesAndFields.RequiredProperties()));

I still need to work out the performance considerations to make it the default rule. So it will go either into v5.5.0 or to v6.0.0

dadhi commented 1 year ago

The issue for the custom rule support #571