Resolving circular dependencies in C#

Started by
2 comments, last by Telastyn 12 years, 4 months ago
Hi guys

I'm writing a multi-player server for web based flash games. Each game instance has 2-4 players, and each instance is completely independent of other instances. Different game types (ie chess and poker) can be run on the same server. Server uses a worker thread pool that uses application instances for locking; ie only one worker thread may access an instance at a time.

In one project I have the server code that handles networking, scheduling, all the common low level stuff a game instance needs. It provides an abstract Application class that provides the interface* to the low level stuff that I want each different game type to inherent. *declared abstract application, not interface application as I wanted to put in some utility functions in there.

This works fine if each game lives in the same project as the server, but when I tried to refactor a game out into its own separate project I run into circular dependencies, since:
Server needs to be able to construct instances of a particular game type.
Each game type needs to be able to inherent Application.

I've been doing some googling, and it seems the 'standard' solution involves creating a third project that has an interface that both projects use...but that seems kinda hacky to me...is that really the best way to do what I'm trying to do ? Like if this was C++ I'd just make each game instance a static lib and link it in. Inversion of Control also comes up alot, but it seems very overcomplicated for what I'm trying to do. I don't need to be able to swap out server implementations, I just want each different game to live in its own project.

Thanks for any help, Hope I gave enough detail.
Advertisement

I've been doing some googling, and it seems the 'standard' solution involves creating a third project that has an interface that both projects use...but that seems kinda hacky to me...is that really the best way to do what I'm trying to do ?
[/quote]

If I understand you correctly, you have a server project that has some networking stuff that games use. Each game project references (and uses) that networking stuff. And then you want the server project to be your entrypoint? Have it create the game instance?

Personally, I would have a small glue project that ties the game to the networking and spins things up. But it also seems really weird to me that the networking library is doing anything but providing networking capability.
Thanks for your reply

The server needs to spinup each game instance as required. When players first connect they are given a list of games to pick from, they select one and if there isn't a game already waiting for players the server creates a new instance of that game and adds that player to it. When a second player comes along that also wants to play that game he is added the existing game instance, and the game begins.

The game instances don't know or care they are on a server, all they need to do is respond to events such piece X to position Y, ensuring the rules of the game are followed. If each game instance was its own server, each instance would need its own port and have much more overhead.

Hope that makes more sense.
Then what from the server project are the games using if all they're doing is responding to events?

This topic is closed to new replies.

Advertisement