Skip to content
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

[Question] Inheritance Attribute API is even real? #549

Open
1SHAGGYR1 opened this issue Sep 24, 2024 · 0 comments
Open

[Question] Inheritance Attribute API is even real? #549

1SHAGGYR1 opened this issue Sep 24, 2024 · 0 comments
Labels

Comments

@1SHAGGYR1
Copy link

I was trying to create a tool with had the following structure at the beginning and would be easy to expand

-- parent command (has some 'global' options)
---- child command 1 (has some more 'global' options available for this and it's children + from parent command)
------ grandChild command 1 (has it's own options and arguments + from child command 1 + from parent command)
------ grandChild command 2 (has it's own options and arguments + from child command 1 + from parent command)
---- child command 2 (has some more 'global' options available for this and it's children + from parent command)
------ grandChild command 3 (has it's own options and arguments + from child command 2 + from parent command)

So i considered that inheritance with attribute api is the right call for me. I was consulting this sample.

I was expecting that something like this should work for me
image

In terms of code I had the following attributes on my ParentCommand:
[Subcommand(typeof(ChildCommand))]
following on ChildCommand:
[Command(Name = "childCommand"), Subcommand(typeof(GrandChildCommand1), typeof(GrandChildCommand2))])
and following on GrangChildCommands:
[Command(Name = "grandChildCommand1/2")]

But after I tried to run it I got an error saying Subcommand cycle detected: trying to add command of model GrandChildCommand1 as its own direct or indirect subcommand, cause of the Attributes set like shown above.

When I made a deeper dive into the sample I undestood that inheritance is achieved via private field, so I was able to rewrite my case and started it correctly.

But now I'm curious what's the sense of having native C# inheritance in the first place, cause now my GrandChildCommands doesn't inherit on ChildCommand and can only inherit from ParentCommand (for some reason it doesn't fail that way), so I lose all benefits or OOP - I have to make all ChildCommand public to use them via private field, I can't use encapsulation and share most of the implementation in base class.
Moreover I don't understand why is it even an option to inherit from ParentCommand if I have ChildCommand (which inherits from ParentCommand) as well as a private field, and if I do the same manipulation with private field in ChildCommand I a can access ParentCommand's properties via it.

And also this type of 'inheritance' doesn't work with [required] attribute normally, cause it simply fails on child entities before it's been set with the help of reflection (I guess)

All in all, I didn't find an example of using native inheritance properly in samples and what I was able to create myself looks terrible, please tell me that I understood something wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant