kiota-abstractions-dotnet
kiota-abstractions-dotnet copied to clipboard
Models should be partial classes
Models should be partial classes, so one can extend them if necessary.
We've received that suggestion a couple of times and rejected it for the following reasons:
- only works for dotnet (and maybe TypeScript) from a design perspective.
- might make support more difficult as people would say "X isn't working in my model" but the root cause could be located in the code they added.
When digging into why they'd like to extend the models, the main reason was "OpenAPI description is inaccurate/incomplete". Can you provide specific scenarios this would enable outside of that main reason?
One reason would be to implement #232. Other reason could be to implement a combined property getter - such as IsValid => SomeProperty == Something && OtherProperty == SomehtingElse
or ToString() etc.
I was thinking more in augmentation direction than fixing generated code.
Couldn't you do that through an extension method?
I can't implement interface nor create properties through extension methods. True, I could create IsValid() extension method in such case. Anyway, partial classes give more flexibility. At least you might consider generator flag that would toggle partial keyword inclusion.
We are not strongly against partial classes, we just need clear use cases to justify the work and the additional conceptual complexity.
as for additional flags, not having any is a design philosophy for kiota. because it leads to additional complexity on the CLI experience, and additional maintenance burden. We've already been asked for a dozen different flags...
as for additional flags, not having any is a design philosophy for kiota. because it leads to additional complexity on the CLI experience, and additional maintenance burden. We've already been asked for a dozen different flags...
Fair enough.
We are not strongly against partial classes, we just need clear use cases to justify the work and the additional conceptual complexity.
IMO there is almost no work adding keyword partial to all models. As per complexity people will make out of it - even now is possible to manually add partial keyword where required - albeit its tedious, or modify generated code. My main use case today is implementing #232 which would prettify and reduce my code. Basically applying an interface. Then (from my past experience) imagine models that share some traits, i.e. CreatedDate property. One could slap another interface to these and uniformly work on them instead of using plenty of _if_s. I'm sure there are other possibilities with generated partial classes.
I believe that change is simple enough and the use case of "I want my models to implement an extra interface" can only be enabled this way that we should consider it. This would be limited to dotnet though. Thoughts @darrelmiller ?
ping @darrelmiller for confirmation before we proceed forward with a pull request here
Closing this for now as this was completed in https://github.com/microsoft/kiota/pull/4895