ritratt
ritratt

Reputation: 1858

How to remove circular reference resulting from dependency injection in my MVC Application?

My solution contains 3 projects -

Project A - which is the startup project

Project B - which containers interfaces, providers, helpers and implementations that are injected in Project A

Project C - which is responsible for resolving IoC using Autofac.

Project A contains a reference to Project B since it requires the interfaces in there. Project C contains a reference to Project A.

I am now trying to implement a new interface which needs to use the models defined in Project A. If I create the interface in Project B this results in circular reference since Project A already references Project B so Project B cannot reference Project A for using the model.

If I create the interface in Project A then I cannot register it without referencing Project A from Project C because circular again.

Where do I create and implement this interface without causing a circular reference. The interface is a kind of an authentication manager provider. I could just create a function for doing the stuff within my Controller but that is very untidy.

Upvotes: 0

Views: 973

Answers (2)

Boneless
Boneless

Reputation: 104

There seems to be a flow in your architecture.

Are you able to extract the Interfaces in another project (who will be referenced in all project, but wont reference the others) and to have nothing else in it (so we won't be tempted to reference something else)?

When doing injection, I always seperate the interfaces projects from the implementations ones... To many ways to mixe them up.

Upvotes: 0

Mrchief
Mrchief

Reputation: 76208

You can move the models from Project A to Project B and from your description, it looks like that is a common layer anyway.

So then your new interface can be created in Project B and it won't have to access Project A to access those models.

Upvotes: 1

Related Questions