kiota-abstractions-dotnet icon indicating copy to clipboard operation
kiota-abstractions-dotnet copied to clipboard

Models should be partial classes

Open MihaMarkic opened this issue 9 months ago • 10 comments

Models should be partial classes, so one can extend them if necessary.

MihaMarkic avatar May 03 '24 11:05 MihaMarkic

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?

baywet avatar May 03 '24 12:05 baywet

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.

MihaMarkic avatar May 03 '24 13:05 MihaMarkic

Couldn't you do that through an extension method?

baywet avatar May 03 '24 13:05 baywet

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.

MihaMarkic avatar May 03 '24 13:05 MihaMarkic

We are not strongly against partial classes, we just need clear use cases to justify the work and the additional conceptual complexity.

darrelmiller avatar May 03 '24 14:05 darrelmiller

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...

baywet avatar May 03 '24 14:05 baywet

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.

MihaMarkic avatar May 03 '24 14:05 MihaMarkic

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.

MihaMarkic avatar May 03 '24 14:05 MihaMarkic

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 ?

baywet avatar May 03 '24 14:05 baywet

ping @darrelmiller for confirmation before we proceed forward with a pull request here

baywet avatar May 22 '24 19:05 baywet

Closing this for now as this was completed in https://github.com/microsoft/kiota/pull/4895

andrueastman avatar Jul 09 '24 09:07 andrueastman