Cyclic Dependency between Infrastructure and Model Layers/projects of DDD

1.9k views Asked by At

I am following a book for Domain Driven Model in C#.NEt. I am having Cyclic Dependency between Infrastructure and Domain Layer (both are class library project of my solution namely "ShareManagement"). I want to know how can I get rid of cyclic dependency problem in Visual Studio/C#.NET.

  1. Dependency of Model on Infrastructure Layer: Definitely, Domain Layer uses Infrastructure Layer such that objects in Model Layer depends (Call) Objects in Infrastructure Layer (like Repositories defined in Infrastructure Layer are accessed from Domain Model Layer using ICompanyRepository which implements IRepository<T> defined in Infrastructure Layer).

  2. Dependency Of Infrastructure on Domain Model class: However, in Infrastructure Layer, my Entity Framework (Entity Factory) needs to implement IEntityFactory<T> where T is EntityBase (a Entity class in Domain Model Layer derived from EntityBase in Infrascture Layer; EntityBase is base class for all entities).

Following is the class in infrasture layer (under "Repositories" folder):

using System.Text;
using System.Data;
using ShareManagement.Model.Company; // How to do this ??
ShareManagement.Infrastructure.EntityFactoryFramework;

namespace ShareManagement.Infrastructure.Repositories
{
    internal class CompanyFactory: IEntityFactory<Company> 
//Company is defined in Model Layer and derived from Abstract Base class "EntityBase"
//So, how to use "using ShareManagement.Model.Company" ?
    {

    }
}
1

There are 1 answers

0
Fakhar Anwar On

Image shown in link below has two assemblies/projects (enclosed in two main boundaries) named as Infrastructure Layer Project/Assembly and Model Layer Prject/Assembly.

As obvious from figure that they both are forming a Circular Dependency. http://screencast.com/t/lUGwetETXHF

The Solution to this issue is depicted in link below: http://screencast.com/t/acsLjq7Ubd

If a project/assembly A (Model in our case) depends on (references) a Project/Assembly B (Infrastructure in our case) and if "part-of" Assembly B (like, Infrastructure.Repositories OR EntityFactory) depends on (; needs to reference) classes in Project/Assembly A (Model) forming a Circular Dependency then resolve this dependency as below:

For the sake of understanding, Lets name the depending "Part-Of Code" in Assembly B as B-dep1, then;

  1. Make the B-dep1 a separate assembly/project from B ShareManagement.Infrastructure.Repositories.

  2. The name of the new Project for B-dep1 should be the same namespace name as of layer in Project B such that B-dep1 remain the part of Layer/Namespace (I mean, such that it remains part of ShareManagement.Infrastructure namespace) in Assembly/Project B. (In our case, i name this new project for B-dep1 as ShareManagement.Infrastructure.Repositories.

  3. Now, new project for B-dep1 namely "ShareManagement.Infrastructure.Repositories" can reference A without forming Circular Dependency.

See How:

new project for B-dep1 namely "ShareManagement.Infrastructure.Repositories" depends on A. on B, but B does not depends on (references) New-Part Project.

A depends on B but B does not depends on new project for B-dep1 namely "ShareManagement.Infrastructure.Repositories" whereas ShareManagement.Infrastructure.Repositories project can still continue to use the namespaces (And encapsulated code) of Infrastructure Layer without having to adding reference to Infrastructure Code because they have same namespaces (that is why i named the project as per namespace in B Project). Visual studio creates the namespace automatically based on project name OR Folder Name. Same namespaces in different assmblies freed the assemblies to refer to each other like in this case namespaces related to Infrascture Layer.