C
C#•8mo ago
Hambones

Internal classes

I've created a back end/"engine" project in a solution. I followed the default and set all my classes to internal without thinking much of it. Now when i want to use a front end in a different project, I'm finding that i can't access the internal classes (obviously) and thus can't do things like pass instances of such classes to my UI. Is this c#'s not so subtle way of encouraging me to define an explicit outward facing "interface"?
13 Replies
Angius
Angius•8mo ago
No, it's C#'s way of telling you that it probably should be public
Hambones
HambonesOP•8mo ago
Hmm... alright. that would certainly make everything easier...
Jimmacle
Jimmacle•8mo ago
in terms of library development it's best to restrict visibility as much as possible because it's a lot easier to make something public than to take it away once people are using it
Hambones
HambonesOP•8mo ago
alright...
Omnissiah
Omnissiah•8mo ago
theoretically in your project you should have a "section" (the public api) dedicated to external access which is undoubtedly public
michalgrzyska
michalgrzyska•8mo ago
personally I think this may be a good case to use interfaces, especially if you use a lot of DI there
Hambones
HambonesOP•8mo ago
So would I use public interfaces to access the internal classes? Seems like an interesting idea.
michalgrzyska
michalgrzyska•8mo ago
this is generally what interfaces are for 😄 you make a public interface and the implementation is internal. you will notice one big convenience - you can inject whatever you want into implementation's contructor and all dependencies would still be internal, because interface does not expose a constructor. (of course this is only one of many purposes of interfaces)
Angius
Angius•8mo ago
You can have all the interfaces in the world, if the implementation is private it is private
michalgrzyska
michalgrzyska•8mo ago
yes - when you explicitly register stuff in DI container, this is perfectly valid case to use interfaces with an implementation
Angius
Angius•8mo ago
I don't think anybody mentioned DI here
michalgrzyska
michalgrzyska•8mo ago
I said " especially if you use a lot of DI there", but yes fair enough, there was no mentioning DI from the author. I wanted to point it out as a probably good idea of designing an app (unless its not some game for example)
Hambones
HambonesOP•8mo ago
Thanks guys.
Want results from more Discord servers?
Add your server