Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 982
Abigail Baar Avatar asked Sep 07 '15 20:09

Abigail Baar


2 Answers

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)
like image 200
janhartmann Avatar answered Nov 13 '22 15:11

janhartmann


From Microsoft's "Common web application architectures".

enter image description here

enter image description here

The Application Core Project

The Application Core holds the business model, which includes entities, services, and interfaces. These interfaces include abstractions for operations that will be performed using Infrastructure, such as data access, file system access, network calls, etc. Sometimes services or interfaces defined at this layer will need to work with non-entity types that have no dependencies on UI or Infrastructure. These can be defined as simple Data Transfer Objects (DTOs).

The Infrastructure Project

The Infrastructure project typically includes data access implementations. In a typical ASP.NET Core web application, these implementations include the Entity Framework (EF) DbContext, any EF Core Migration objects that have been defined, and data access implementation classes. The most common way to abstract data access implementation code is through the use of the Repository design pattern.

In addition to data access implementations, the Infrastructure project should contain implementations of services that must interact with infrastructure concerns. These services should implement interfaces defined in the Application Core, and so Infrastructure should have a reference to the Application Core project.

The ASP.NET Core Web App Project

The user interface layer in an ASP.NET Core MVC application is the entry point for the application. This project should reference the Application Core project, and its types should interact with infrastructure strictly through interfaces defined in Application Core. No direct instantiation of or static calls to the Infrastructure layer types should be allowed in the UI layer.

A starting point repo for Clean Architecture with ASP.NET Core https://github.com/ardalis/CleanArchitecture

like image 27
Joel Wiklund Avatar answered Nov 13 '22 14:11

Joel Wiklund