0

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" ?
    {

    }
}
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Fakhar Anwar
  • 295
  • 1
  • 3
  • 20
  • Strictly speaking the domain should not depend on any infrastructure or application-level services. Maybe, the [onion architecture](http://jeffreypalermo.com/blog/the-onion-architecture-part-1/) articles would be a good reference for this. – Davin Tryon Sep 15 '13 at 13:45
  • How can you say that when Domain Model Layer contains Entity Classes all of which are derived from a common base class "EntityBase" which is defined in InfraStructure Layer and as per my knowledge to decalre "using myProjectName.Infrastructure.DomainBase;" in Domain Model Layer we have to add reference/dependency in Domain Model Layer Project to Infrastructure Layer. See the code below in which i need to use a namespace ( using System; using SmartCA.Infrastructure.DomainBase; using SmartCA.Model.Companies; namespace SmartCA.Model.Projects { public class Contract : EntityBase { – Fakhar Anwar Sep 15 '13 at 13:59
  • Again, strictly speaking, entities should not derive from a base class (unless you are modelling a relationship that needs inheritance). Did you check out the articles I linked too? – Davin Tryon Sep 15 '13 at 14:12
  • EntityBase:Evans defines Entity as“object that is distinguished by identity,rather than its attributes”(Evans,Domain-Driven Design:Tackling Complexity in the Heart of Software,92),all of entity classes need some type of data type to distinguish their identities.It wud b gud to use Fowler’s Layered Supertype pattern,which is defined as“type that acts as the supertype for all types in its layer”(Fowler,Patterns of Enterprise Application Architecture,475).Having all entities inherit frm entity base class type will help eliminate some duplicate properties and behavior in the domain entity classes. – Fakhar Anwar Sep 15 '13 at 17:39
  • beside my explaination above: My question is not about whether model should use/depend on infrastructure or not, OR whethere or not Entities should be derived from an EntityBase OR not. My question is really how can i use a namespace from model layer/project in infrastructure layer (i am new to visual studio and .NET so i tried to create dependency/reference in infrastructure layer project to model layer, but as model layer already has reference to infrastructure layer so there is cyclic depdendency issue. i do not know how to resolve this situation. – Fakhar Anwar Sep 15 '13 at 17:44

1 Answers1

0

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.

Fakhar Anwar
  • 295
  • 1
  • 3
  • 20