I have two objects, RoomManager
and Room
, there will be several Room
s and one RoomManager
. I want the RoomManager
to be the only one allowed to create a Room
object. So I'm wondering if there is a way to make the Room
constructor (and the rest of the Room
methods/properties) only accessible to the RoomManager
. I was thinking maybe moving them to their own namespace and making Room
private or internal or something. From Accessibility Levels (C# Reference) I see that internal is for the entire assembly though, not just the namespace.

- 9,921
- 12
- 54
- 90
-
5Whenever I see XManager I remember this article: http://www.codinghorror.com/blog/2006/03/i-shall-call-it-somethingmanager.html – Nicholas Mancuso Apr 13 '11 at 07:09
-
You could make `Room` a private class inside `RoomManager`? – Blorgbeard Apr 13 '11 at 07:09
-
@Nicholas, nice article.. must read. :D – KaeL Apr 13 '11 at 07:11
-
@Nicholas, good article, I hadn't read that one. I also like the first comment "Well I feel smart. I just renamed every *Manager class to *Helper." :) – jb. Apr 13 '11 at 07:19
4 Answers
No, C# (and .NET in general) has no access modifiers which are specific to namespaces.
One fairly hack solution would be to make Room just have a private constructor, and make RoomManager a nested class (possibly just called Manager):
public class Room
{
private Room() {}
public class Manager
{
public Room CreateRoom()
{
return new Room(); // And do other stuff, presumably
}
}
}
Use it like this:
Room.Manager manager = new Room.Manager();
Room room = manager.CreateRoom();
As I say, that's a bit hacky though. You could put Room and RoomManager in their own assembly, of course.

- 1,421,763
- 867
- 9,128
- 9,194
-
1Room and RoomManager will only ever be needed for one program, so putting them in their own assembly seems like overkill. Is your "Manager class inside Room class" approach too hacky to actually be used? – jb. Apr 13 '11 at 07:21
-
@jon: i would say its not hacky at all, it is a great way how to make these kind of controllers, managers ect ;) – Zavael Sep 23 '11 at 13:57
-
http://stackoverflow.com/questions/585859/what-is-the-difference-between-protected-and-protected-internal – Pharap May 20 '14 at 20:16
You can do something like this:
var room = Room.Factory.Create();
If the constructor of Room is private, it will still be accessible from Room.Factory if you declare the factory class inside the Room class.

- 21,995
- 15
- 85
- 141
Defining the Room inside of RoomManager itself and making it's constructor private could be helpful.
EDIT : But the best solution I think is that to extract an abstract class of the Room, and expose that class to clients.
No one can create an instance of the abstract class.

- 14,616
- 5
- 46
- 70
-
-
I'm sorry, I'm not following with the "extract an abstract class of the Room" part. I do need to create instances of the Room class. Could you clarify a bit? – jb. Apr 13 '11 at 07:23
You should implement 'Singleton pattern'. It creates object only once and any subsequent attemps to create object of that type will actually returns already created object.
You should create class factory which would implement singleton for RoomManager
. Room
, in turn, should be private type of RoomNamager
. RoomNamager
manager should have method for Room
creation. But in this case you cannot access Room
properties outside of RoomManager
class. To solve this problem I recomend you to create IRoom
public interface which would grant access to room functionality. So your create room
method would return IRoom
interface.

- 6,227
- 5
- 41
- 69