Search code examples
c#.netdependency-injectionarchitectureinversion-of-control

What's the recommended folder structure of catalogs in project using IoC


I've read few articles and watched many lectures/tutorials on YT about DI and IoC, but I didn't find any recommended layout of catalogs in VS solution.

I'm talking about the project (a game for example) where you have few classes/interfaces, logger, database provider, wcf services, wpf presentation layer (that's actually different project)...

Is there any pattern project, that shows how should I organize my project, so next, experienced programmer will not waste time figuring out what's going on? Like we're talking about "self commented code", I'm talking about "self commented project structure".

For example, should I put all interfaces into "Interfaces" catalog? Or should I (in case of logger) create "Logger" catalog and put there interface, classes, class with extension methods (all, focused on logging). Code focused on Board, in "Board" catalog. Separate catalog for "Field" etc etc..

Right now the structure looks like that. I'm not sure about "Business" there and Logger. I have interface in different catalog then other logger classes. Should I call Log4Net provider? or adapter? or decorator? It's just a logger class implementing ILogger interface. Here is the screen: link

Here is the sample code (there is no IoC yet, but everybody will notice there will be 3 interfaces there mapped. Very simple):

public class Game
{
    public IBoard Board { get; set; }

    public Game(IBoard board)
    {
        Board = board;
    }
}

public interface IBoard {}

public class Board : IBoard
{
    public IField[,] Fields { get; set; }
    public Board(IField field, int boardWidth, int boardHeight)
    {
        Fields = new IField[boardHeight, boardWidth];
        Fields.Initialize();
    }
}

public interface IField {}

public class Field : IField {}

public interface ILogger
{
    void Log(LogEntry entry);
}

Solution

  • What I usually do is that I have a MyApplication.Core (Class library) layer, which contains all the applications interfaces with as little (read: none) third-party dependencies, e.g. ILogger, ICommand or IQuery<TResult>.

    Next I have a MyApplication.Domain (Class library) layer which contains all the application domain specific knowledge - this is the business layer. This is implementations of the core interfaces ICommand, IQuery<TResult>. These implementations then have an dependency on e.g. ILogger. Never concrete implementations.

    Then I have the MyApplication.Infrastructure (Class library) which is where all the service interfaces from MyApplication.Core is implemented, e.g. ILogger. Here you can have dependencies on third-party libraries such as Log4Net.

    Then last I have the presentation layer, which is in my case usually an MVC applications so I would name this MyApplication.Web.Mvc. All controllers have only dependencies on the interfaces. Never concrete implementations. This layer is also responsible of bootstrapping all the interfaces to the concrete implementations using a Composition Root.

    TL;DR:

    • MyApplication.Core (Application Interface Layer)
    • MyApplication.Domain (Business Logic)
    • MyApplication.Infrastructure (Implementations of Application Interface Layer)
    • MyApplication.Web.Mvc (Presentation and Composition Root Layer)