I have reviewed the relevant WinForms/XNA samples here and here.

However, my requirements are slightly different.

Given the following:

  • I am developing a multiplayer (Client - Server) game
  • There will be three projects in the solution, one for Client and Server respectively, plus an additional one containing the core game/engine
  • The instances will share common code from this engine project (core Game Engine functionality)
  • I require the Server instance to also allow management of the server (e.g. connection list etc.) via a Windows Form
  • Finally, and importantly, I am proposing to use the Dependency Injection pattern on initialisation of the client/server instances to inject the relevant network management class as required - similar to the approach here

What is the best way to achieve this? Specifically, are there any flaws with organising the projects in the solution the way I have proposed above? For the Server project, do I start it as an XNA project and then call the Winform, or vice versa?

Thank you for any advice you can spare.

Edit - a brief example of the "dependency injection" style pattern I plan to use. Forgive me if this is not an example of "pure" / full / traditional DI!

// Create the server
using (var game = new ExampleGame(new ServerNetworkManager()))

// Create the client
using (var game = new ExampleGame(new ClientNetworkManager()))
  • 4
    \$\begingroup\$ Try to make a prototype and you will see if it works. I dont think it will, but it depends on what you mean by Game engine functionality, only things that i was able to share between Client and Server was some constant stuff, message structs helper methods. And i doubt that you will ever make any use of dependency injection. \$\endgroup\$
    – Kikaimaru
    Commented Mar 7, 2013 at 15:15
  • \$\begingroup\$ The dependency injection will certainly be used. I will append an example to the original question for you. And in my case, most of the functionality will be shared, as the Server will be authoritative for all object positions etc. \$\endgroup\$
    – George
    Commented Mar 7, 2013 at 15:27
  • 3
    \$\begingroup\$ There's nothing "non-tradiitonal" about your approach of DI. This is legit; frameworks are useful when you get to the point that you need them. Try prototyping this and let us know what specifically you get stuck on. Your approach seems fine (coming from someone who never tried sharing client/server code like this). \$\endgroup\$
    – ashes999
    Commented Mar 7, 2013 at 15:40
  • \$\begingroup\$ Thank you, if there is nothing wrong with my plan so far, I will try implementing the project and let you know how I go. \$\endgroup\$
    – George
    Commented Mar 7, 2013 at 16:03

1 Answer 1


OK, so after the positive reception of my design idea, I have gone ahead and implemented a prototype. For completeness, I'll describe the structure of my project below. Most importantly, it builds and runs, and the DI works great.

Solution Structure (in VS2010)

  • Solution Root:
    • Client. Console Application. Startup Project.
    • Server. Console Application. Startup Project.
    • XNAGame. Windows XNA Project. No entry point (Program.cs removed). Output Type: Class Library. No Startup Object.
    • XNA Game Content. Standard XNA content project.

Client/Server Projects
Reference the XNAGame project and Microsoft.Xna namespace. Both contain a Program.cs with a static void main() method as expected. This method invokes the XNA game, considering Dependency Injection, as follows:

// Network Manager type injected as appropriate.
using (XNAGame game = new XNAGame(new ClientNetworkManager()))

XNAGame Project
Contains all the usual XNA structure (class with Update/Draw methods, etc.) but with one fundamental difference: there is a Windows Form in this project.

The RTTI features in C# allow this class instance to ascertain whether it has been instantiated as a Client or a Server. Obviously, the relevant Network Manager classes should have been created that implement a common interface for the polymorphism to work. For example:

// Constructor accepts the "injected" type as below.
public XnaGame(INetworkManager networkManager)
    graphics = new GraphicsDeviceManager(this);
    Content.RootDirectory = "Content";

    this.networkManager = networkManager;

// RTTI / Polymorphism. Determine if this instance is a Server or a Client.
public bool IsServer()
    return this.networkManager is ServerNetworkManager ? true : false;

// The Windows form within this project can then be run as follows.
protected override void Initialize()
    // TODO: Add your initialization logic here

    // If this is the server, we need to create and show the management form.
    if (this.IsServer() == true)
        Form = new MainServerForm();

  • \$\begingroup\$ So your question is...solved? It works? \$\endgroup\$
    – ashes999
    Commented Mar 7, 2013 at 23:24
  • \$\begingroup\$ So far, so good, yes. Thank you for your input originally. I'm genuinely puzzled as to why my question was voted down... my question was regarding the design and whether there would be any glaring problems with my idea. I chose to implement it, and it's turned out OK, yet I am left feeling that my question was not a good fit for this type of community? Strange. And frustrating. \$\endgroup\$
    – George
    Commented Mar 7, 2013 at 23:43
  • 1
    \$\begingroup\$ this is a very unique and fickle community. They expect very specific questions with "correct" answers, generally. Anyway, I'm glad your problem was solved, and I hope you'll stick around -- it's a good site, once you get the hang of the culture. \$\endgroup\$
    – ashes999
    Commented Mar 8, 2013 at 2:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .