-
Notifications
You must be signed in to change notification settings - Fork 4
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
Support also get-only properties #14
Comments
Hi, I've released version 2.3.0 with support for injecting get-only properties. I was a little against allowing this case at first, and I am still unsure about it. But I've decided to allow it in an opt-in manner because:
So the injection is done by using the [field: AutoConstructorInject]
public int Property { get; } Using your sample: [AutoConstructor]
public partial class SomeClass
{
private readonly int _someInt;
private readonly string _someString;
[field: AutoConstructorInject]
public int NonImplementedProp { get; }
public int SomeImplementedProp => 2;
public SomeClass(int someInt, string someString, int nonImplementedProp)
{
this._someInt = someInt;
this._someString = someString;
this.NonImplementedProp = nonImplementedProp;
}
} More infos in the README. |
Hi Second,
What about the ability to prevent that "break" by adding to the AutoConstructorAttribute a propoerty like "bool InjectGetOnlyProperties"
Only then, the generator will refer such properties and inject them as well. What do you think about this proposal? |
I'm sorry but I will maintain my position on this. I think it's ok for now if you need some get only properties injected, but if you need a lot, then it will be out the scope of what I wanted initially for this library. I want to have a visual indicator on get only properties, and modifying the base attribute is not enough for me. The breaking change issue was not a major point in my decision, just a additional one, and I think that even if I were to go to a 3.0, I would still not do the injection by default. Hope it is ok for you. |
Actually, the motivation is more about DTOs, which have only properties. BTW Your mechanism brings a fantastic solution to implement it.
Or, in an immutable DTO (records usage is not always efficient):
Hmmm... |
I hadn't see it this way but your point is valid, and I agree that it could be a good feature for this case. I will think about how to do it but I will certainly do it. |
I found a similar project which already includes this capability. |
I have released version I have some more work to do before releasing the 3.0.0 version but this should be as stable as a non pre-release version. |
Hi
I like this project a lot.
It is possible to apply the behavior also in case of get-only properties (which has a backup field behind) ?
See this example, which expected to inject also the NonImplementedProp to the ctor.
The text was updated successfully, but these errors were encountered: