开发者

Organizing multi level aggregate object or composite

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 开发者_JS百科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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜