Issue: Circular Dependency between Model Layer Project and Infrastructure Layer Project ...

Sep 16, 2013 at 1:52 PM
Edited Sep 16, 2013 at 1:56 PM
Hi Tim,

Hope you are doing fine. I am really inspired of your work. I wonder no one raised a basic question that i am going to put up here. May be i am missing something. Here is my Query:

Model Layer uses/extends classes/interfaces defined in Infrastructure, Similarly classes/interfaces in Infrastructure also use namespaces/classes/interface defined in Model Layer Project which is causing Circular Dependency so i cannot use Model in Infrastructure Layer. How can i use "using SmartCA.Model.Projects" in Infrastructure Layer Project while i have already defined Dependency of Model Layer on Infrastructure Layer ? such that MS-VS does not show Circular Dependency constraint.


As classes/interfaces in Model Layer (like Business-Entity Classes and IEntityRepository) use/extend the Classes defined in Infrastructure Layer ( like abstract base class "EntityBase" and IRepository<T>). Therefore, i have added dependencies/references in Model Layer Project for Infrastructure Layer, so as to make Model Layer as dependent on Infrastructure Layer. (So now i can use namespaces and relevant classes/interfaces defined in Infrastructure Layer)

However, Infrastructure Layer seems to use Model Layer namespaces as on page 82 of the book we see the line marked as bold below:

//EntityFactory concrete class
  using System;
  using SmartCA.Model.Projects;
  using SmartCA.Infrastructure.EntityFactoryFramework;
  using System.Data;

  namespace SmartCA.Infrastructure.Repositories
    internal class MarketSegmentFactory : IEntityFactory<MarketSegment>
Similarly, (on page 77 of the book) while creating concrete Respository class, we see the code as below:
  namespace SmartCA.Infrastructure.Repositories
    public class ProjectRepository : SqlCeRepositoryBase<Project> , IProjectRepository
          ///Some Code
where Business Entity Object "Project" and interface "IProjectRepository" both reside in Model Layer and using the namespace of model layer will require to add reference/dependency in Infrastructure Layer Project to Model Layer Project causing a Circular Dependency which Visual Studio (OR, probably .NET Framework) does not allow.

Can you please explain how can i make this code working ?

Note: I have put up the same question there on StackOverFlow, so you can answer my query even there as per your comfort.
Question Link:

Looking forward to your response,

Best Regards,
Fakhar Anwar
Sep 24, 2013 at 8:53 AM
Detailed Answer with explaination is submitted on:

The solution to circular dependency is based on what i observed and learned from the PAtch in SourceCode here. So guys can continue to use patch as a solution and may read my post on link mentioned above if studying and implementing using the Tim McCarthy book on DDD.