In my game, people connect to my server. The server creates a GamePeer per connection. GamePeer creates one HelpersContainer that contains all helpers. Any helpers must see any other helpers.
//1 network peer = 1 GamePeer object
class GamePeer
{
HelpersContainer helpersContainer = new HelpersContainer(dbWorker)
...
}
//Contain all helpers
class HelpersContainer
{
public IDBWorker DBWorker {get; private set;}
public UserHelper UserHelper {get; private set;}
public BuildingHelper BuildingHelper {get; private set;}
public StarsHelper StarsHelper {get; private set;}
//WeakReference for GC can collect objects
private WeakReference ThisWeakReference = new WeakReference(this);
public HelpersContainer(IDBWorker dbWorker)
{
this.DBWorker = dbWorker;
this.UserHelper = new UserHelper(ThisWeakReference.Target);
this.BuildingHelper = new BuildingHelper(ThisWeakReference.Target);
this.StarsHelper = new StarsHelper(ThisWeakReference.Target);
}
}
class UserHelper
{
private HelpersContainer HContainer;
public UserHelper(HelpersContainer hContainer)
{
this.HContainer = hContainer;
}
public GetUsersInGalaxy(int galaxyId, IEnumerable<User> allUsers = null, IEnumerable<Star> allStars = null)
{
if(allUsers == null)
allUsers = HContainer.DBWorker.GetAll<User>();
if(allStars == null)
allStars = HContainer.StarsHelper.GetStarsForPeoples();
var starsInGalaxy = allStars.Where(x => x.galaxyId == galaxyId);
return allUsers.Where(x => starsInGalaxy.Any(y => y.Id == x.starId));
}
}