Composition/Aggregation law of demeter

Mar 7 2010 3:42 PM

Hi,
I have question regarding architecture when dealing with composition/aggregation hierarchy. The three classes below "House", "Room" and "Door" describes a simple nested architecture used for this question. This architecture means that when I use a House object and want to refer to a door nested in a Room I will brake the "Law of demeter" rule that states that I should only use direct connection like House.ToString() not indirect connection like House.Room.Door.ToString(), is this okay or bad practise, is there any better way of organising the architecture?
 
Would it be better to do helper functions for the door in the Room Class, and not expose a Door object public, like:
DoorOpen();
DoorClose();
 
If this arhitecture is prefered, what should be done with the events inside the Door class, should they also be defined once again in the Room class and be "chained" back thru it's parent classes some way ?
 
 
Any thoughts, tips and ideas would be helpful.

public class House
    {
        public House()
        {
            Room = new Room();
        }
        public Room Room { get; private set; }
    }

    public class Room
    {
        public Room()
        {
            Door = new Door();
        }
        public Door Door { get; private set; }
    }

    public class Door
    {
        public event EventHandler<EventArgs> OnOpen;
        public event EventHandler<EventArgs> OnClose;
        private bool _isOpen;
        public Door()
        {
        }
        public void Open()
        {
            _isOpen = true;
            OnOpen(this, new EventArgs());
        }
        public void Close()
        {
            _isOpen = false;
            OnClose(this, new EventArgs());
        }
    }