JonnyKnottsvill
JonnyKnottsvill

Reputation: 1153

Circular Dependency in an inherited code base

I'm very much still learning how to structure larger code bases (a year ago I had only dealt in solutions of 3/4 small projects whilst learning), and I've been trying to organise an inherited code base to get rid of a Circular Dependency issue.

Basically I have an MVC Portal project backed by an SQL database which is code first using Entity Framework. I also have a repository project which handles communication to some Azure tables. The data in these tables overlaps somewhat but the Azure table does not contain much of the information from the Portal db.

The problem arises as when I create certain entities, the solution as it is demands they be created in the Portal Database (so that users can see their information) and also in these Azure tables which the rest of the solution uses for other purposes.

So the Portal persists data to both, meaning I need to use the Repository project within my MVC project, but I also need my Repository project to be able to access the MVC project for some peripheral information which is not persisted to the Azure tables.

This is obviously bad design, but structurally is there a way around it which doesn't require major refactoring or changes to the Azure table structure to include every little detail of the Portal db?

Every "simple fix" I come up with isn't a fix at all and merely makes the Circle bigger haha. These two tables in the design have confused me since I inherited this.

EDIT for more clarity

My repository references the MVC project in order to use the ApplicationDbContext to communicate with Portal db. This is not easily separated into another project as I understand. My MVC project references the Repository to store information in Azure tables. this needs to stay as it is really.

Tha problem I guess is the Repository does too much. I'm starting to think I should just separate those out despite it being quite the rework.

JK

Upvotes: 1

Views: 91

Answers (2)

Wosh
Wosh

Reputation: 1625

Correct me if I am wrong, but from what I understood, you currently want to exchange information between the Azure Storage and the SQL Storage. If that is the case, then you should create a BaseRepository which is inherited by both the SQL and Azure storage. Then the "peripheral information" can be found in properties from the BaseRepository, which are accessible by its children.

Upvotes: 1

Polyfun
Polyfun

Reputation: 9639

Extract the "peripheral information" in the MVC project to a another, new project that both the MVC and Repository project can reference, and remove the reference to the MVC project from the Repository project.

Edit: to clarify: currently you have this circle:

MVC -> Repository -> MVC

You state that Repository needs to extract some "peripheral information" in MVC. So put that "peripheral information" into another project, let's call it PI, and remove the reference to MVC from Repository (as everything external that Repository needs is now in PI and not MVC). This breaks the circle. This will give you:

MVC -> Repository
MVC -> PI
Repository -> PI

This is the classic way to break circles, by creating a linking "entity".

Upvotes: 0

Related Questions