Onion Architecture – Implementing in Client-Server Applications

architectural-patternsArchitecturecclient-servernet

I'm staring new project, cross-platform. I want to use onion architecture there. It will be a simple game (tic tac toe), just for education purposes. This app will be client-server type.

Now, I read a lot about onion architecture, but I'm still not exactly sure how to deal with it in client-server.

Should I create two different repositories(1) for server and for client? Or should I keep them both in single solution(2) (since many class/interfaces in Core/Domain are the same for both)?

  • (1) – source code repositories (on bitbucket or sth like that)
  • (2) – Visual Studio Solution

EDIT:

It will be cross-platform app (C# via xamarin) with single server also in C#. Communication via WCF. Server will just store user accounts, watch every move, save history of games – not important, it's just for education.

Answering @Kasper, how do I see OA in client-server app:

I thought, I could share as much code as possible in Core of Onion – like Entities, Helpers, Logger(?), Interfaces… Then, I will have another layer – project for Client API (Application and Domain services) and Server API (Application services – consuming and sending messages from/to clients), then Interface layers. Separated for Clients and for Server.

So, single VS solution, two apps, single Core. I know it's technically possible – but is that a good practice?

Best Answer

It is not clear what you mean by "repositories". Do you mean Source Code Repositories ? In this case, definitely use only one repository.

Also, it is not clear what you mean by "solution". You are using C#, so presumably Visual Studio, and in visual studio "Solution" is a container of all your projects. So, definitely use one solution. This solution would contain multiple projects.

So, your "Tic Tac Toe" solution would contain:

  • One "Client" project for your client (perhaps of type WinForms application ?)
  • One "Server" project for your server (perhaps of type Console application ?)
  • One "TicTacToe" project with all code that is common for both (of type Class Library.)

The "Client" project will depend on (make use of) your "TicTacToe" project, and your "Server" project will also depend on (make use of) your "TicTacToe" project.

Furthermore, even though your client project does not directly invoke (cannot make use of, at the source code level) your server project, you should still define the client as depending on the server, because every time you want to run the client, you want Visual Studio to make sure that the server is up to date. (Has been compiled.) Then, you could have a mechanism in place which automatically restarts the server each time it gets recompiled, but that's beyond the scope of this question.

So, the TicTacToe Class Library project can be thought of as the center of your onion, the Server Application project can be thought of as a middle layer of your onion, and the Client Application project can be thought of as the outer layer of your onion.

Related Topic