Sign in to follow this  
Captain P

Controlling accessibility levels in C#

Recommended Posts

I've been working on a school project recently, and one of the things involved is a renderer module. Since I want to build a solid interface for it, keeping as much details hidden from the rest of the program, I came up with the following design. The Renderer class allows outside code to add, update and remove shapes. The Renderer.Add() function returns a reference object, that contains a reference (and possibly some additional information) to the shape held by Renderer. Shapes can be updated by calling the Renderer.Update() function, passing it the reference object and the new data. The reason I'm going for this design, is that I found passing ID integers around to be fragile: what if some other code called the Renderer.Update() function with a totally random ID, and it actually had effect? Also, it takes the Renderer a search through it's data structure to look up the ID - using a reference object allows me to store more data to eliminate these lookups, and by keeping the internals of this object private I can change that without affecting the interface and usage of this module. The issue here is, that only Renderer should be able to access this shape reference within the reference object. It should also be the only one that can construct these objects, preferrably. It seems however, that C#'s internal keyword doesn't offer me enough control, since it only restricts access to the current assembly. So I wonder, does anyone know of a workaround? And while we're at it, are there any particular issues that I overlooked with this design? Thanks beforehand. :)

Share this post


Link to post
Share on other sites
Ignoring the questionable design where the user cannot manipulate a shape they create...

I'd look into nested classes. A nested shape within the renderer which implements a public interface should yield the accessibility layout you're looking for. I'd also toy around with a few since there's a number of nuances to those sort of constructs.

Share this post


Link to post
Share on other sites
Quote:
Original post by Captain P
The issue here is, that only Renderer should be able to access this shape reference within the reference object. It should also be the only one that can construct these objects, preferrably. It seems however, that C#'s internal keyword doesn't offer me enough control, since it only restricts access to the current assembly.
So don't do that stuff. Since it's your assembly, you control all the code involved. The type of relationship you're looking for -- C++ style friendship -- exists to prevent other code from accessing certain functionality that is present in an interface.

Of course, it's recommended that you break things out into other assemblies.

Share this post


Link to post
Share on other sites
Quote:
Original post by Telastyn
I'd look into nested classes. A nested shape within the renderer which implements a public interface should yield the accessibility layout you're looking for. I'd also toy around with a few since there's a number of nuances to those sort of constructs.


Sounds like what I'm using now with Panda 3D. When you load a model, you get a NodePath object, that provides an interface to do whatever you need to do with that model, like adding it to the scenegraph, changing position, etc.
I think the main difference with my idea is that I wanted to merge the shape update (manipulate, as you call it) interface into my Renderer class itself. I think keeping those apart would be cleaner indeed. Thanks. :)


Quote:
Original post by Promit
Of course, it's recommended that you break things out into other assemblies.


I guess it's time for me to look into using .dll's yeah. ;)

Share this post


Link to post
Share on other sites
If you make the constructor of the Shape "internal" that should prevent people outside the assembly from creating new shapes.

As for "internal" not giving you the control you want, what control do you want? C++ had the "friend" classes, but as far as I know C# doesn't have that. (I know I recently looked it up, and I remember something about it not being supported in C#). Also nested classes will not hide your shape. If your renderer can access it, so can other classes in your assembly.

If you just want to protect the code from your own mistakes, I wouldn't go there. I think you're clever enough not to fall in your own pit.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this