New "dependency" keyword #3886
-
Instead of this:
We could write this:
Constructor would be created by compiler with dependencie with the same order as declared. Additionaly we could create method:
Which would be called in auto-generated constructor. :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
With records you will be able to do this: public record MyService(IServiceA serviceA, IServiceB serviceB, IServiceC serviceC) {
// members go here
} It's expected that primary constructors (the feature that allows for a parameter list to be specified after the type name) will be supported for normal classes by C# 10.0 complete with a constructor block where you can perform validation: public class MyService(IServiceA serviceA, IServiceB serviceB, IServiceC serviceC) {
public MyService {
if (serviceA is null) throw new ArgumentNullException(...);
if (serviceB is null) throw new ArgumentNullException(...);
}
// members go here
} With #2145 you could possibly also write it as: public class MyService(IServiceA serviceA!!, IServiceB serviceB!!, IServiceC serviceC) {
// members go here
} |
Beta Was this translation helpful? Give feedback.
-
This feels like a very domain-specific problem that would be better solved with a domain-specific source generator. |
Beta Was this translation helpful? Give feedback.
With records you will be able to do this:
It's expected that primary constructors (the feature that allows for a parameter list to be specified after the type name) will be supported for normal classes by C# 10.0 complete with a constructor block where you can perform validation:
With #2145 you could possibly…