Saturday, October 25, 2014

Three Tier Architecture Sample in C#


The short version:

Uploaded a simple C# sample solution (Visual Studio 2012), featuring:


  • 3 tier architecture (web application, web server, database)
  • AJAX calls from a web application
  • XMLSerialization
  • Accessing a Database with SqlDataReader
  • layer architecture of the server tier (business connector, data connector and core)


The long version:

Lately I made some changes to the client server architecture. The original design consisted of a big client (javascript running in the browser), a very small web server and a lot of SQL.
I put most of the business logic into sql because I thought that transaction handling, concurrency and scaling would be easy to implement with sql. The use of spatial data types seemed also to simple some things which would otherwise be programmed by hand.
Additionally, I wanted to find out if it is possible to write all of the server sided code in sql.
And finally, I just like sql programming and wanted to write more code in it.

Until recently, the program flow was as follows: 
The web server remembers user id per user in a session cookie. The server receivers ajax requests, calls a sql stored procedures with the client parameters and the user id and returns the result of the stored procedure.

Mostly, this implementation worked just fine, but as reported in the last blog, I ran upon a performance problem regarding spatial indices. Subsequently, I added a programmed game server to the game architecture.
The web server now receives user requests, but does not call the stored procedure but a method of the game server.

The game server consists of the following namespaces:
BC (business connector)
Core
DataConnectors

The following requirements should be fulfilled by the server tier :
- The business connector should be the only one accessible from the outside. It reveals a bunch of methods.
- Classes in the core contain the game data and do all the processing.
- The data connector is called when the core needs data or to writes updates 
- The game server has to be designed in a way that it may grow to many thousands lines of code in 100+ classes, without getting a big unstructured mess (source code tends to rot if you are not careful).

The last point is the one for which I didn't find good advice or examples. That is the reason why I put a tidied up version of the game server on github. Keep in mind that I have until now programmed only few features in the game server, so I can't guarantee that its design is as good as hoped.

This is a typical workflow for a web server request: 

  1. calling the bc method in BusinessConectort.cs file 
  2. creating an instance of the corresponding bc class which3
  3. creates an instance of a core class doing the work
  4. the core class 
    1. fetches data into sql table corresponding objects
    2. does the calculations
    3. writes back to sql
  5. the bc class then generates the output
I do not momentarily need a sophisticated way of communication between the web server and the game server. Thus the current solution is a dll which lets the web server instantiate the business connector. A better solution in the long term would be a stand alone server. That server could then even communicate with multiple web servers.

That only the business connector is accessible from the outside is not yet fully implemented. Some classes have "internal" as access modifier, preventing outside access.Others had to be set as public, since I wanted to use them with the XMLSerializer.

The web application project is kept very simple. The ajax return is imply inserted into the dom. Either adapt the result of the business connector, or add some javascript logic to parse the result, if you are implementing a web application yourself. Or do both, returning a json string from the server and creating a javascript object from it would be a good beginning.

I would be happy to hear suggestions or get even improvements to the sample.

If you like it, and in case that you also like multiplayer 4X games, check out Empires in Space, the reason for writing the game server...










No comments:

Post a Comment