views:

38

answers:

1

Hye!

I have Aggregate object that have more than one level. Aggregate root is Network object and it have collection of Nodes and Links. Node and Link have collection of Segments, and Segment have collection of Lanes. My domain is traffic network and this is only part of this aggregate:

public class Network
{
 private List<Node> nodes;
    private List<Link> links;
}    
public class Node
{
 private Segment segment;

}    
public class Link
{
 private List<Segment> segments;
}    
public class Segment
{
 private List<Lane> lanes;
}
public class Lane
{
 private List<Cell> cells;
}    
public class Cell
{
 private bool IsOccupied;
}

First of all, can somebody tell me is this good or it's better to breakdown this aggregate on maybe three levels (Network, Segment, Cell) with repository pattern implemented on top of that roots.

My problem is with additional functionality that i want to add to network. If this network is going to be used for simulation then simulator need vehicle sources and sinks that will generate and destroy vehicles on the edge of network. My idea is to have that functionality out of network aggregate. This is the code:

public class Simulation
{
 public void Iteration()
 {
  //do something with vehicles
  //foreach sink in sinkRepository call o.Update()

        }

}

public class SinkRepository
{
 private List<Sink> sinks;

 //Singleton pattern

 //+ CRUD operations and GetEnumerator()
}

public class Sink
{
 private int nodeId;
 private List<int> laneIds;

 public void Update()
 {
  //need reference to Network object
  //foreach laneId
  ////network.GetLane(id)
  ////find is cell in lane occupied
         ////if it is then network.SetCellStatus(false);
        }
}

Is this good way to do it? And what are your suggestions :) Also i have some persistance code that create network from XML, and it can also create sinks from XML but the problem is how to elegante pass reference of Network object to sink object because sink object needs reference to network because it need to fetch lane based on laneId.

I was thinking about adding repository of network and repository would be singleton so sink would see it witout keeping reference on network throw whole lifetime.

A: 

One thing you could do to make it a bit simpler is to find the correct lane objects whenever you are creating a sink and put them into Sink object. This will replace List<int> laneIds with List<Lanes>. You won't need to hold a reference to Network in Sink and search the list of lanes each time you are doing an update.

You will still need a reference to Network object either in SinkRepository or better yet in a SinkFactory class where you could create your new Sink objects and set them up with correct lanes.

derdo
Ok, but wouldn't it be against aggregate pattern to hold reference to Lane objects in Sink?
Vedran
well, it might be. But the real question is does this solution work better for you or not. Patterns are patterns not the law:)
derdo