circular dependency between database project and web project

I have a web application that needs access to database values. Currently the web project creates an instance of DB Project class and uses that to pull values via the various functions of the class. However, whenever the DB tables change we have to update our code to adjust to the new table / structures. SO I’m trying to re-organize the dependency so that the DB class in the DB project implements a web project interface.

This should be fine and would require that the DB project simply reference the Web project. However as far as I can conceive, I still need to build a concrete implementation of the DB Project class w/i web application regardless of whether I assign it to a interface variable or not. Thus I believe I need to also reference the DB project in the Web project, leading to the circular dependency I’m facing.

I work in asp.net 4.0.

How can my web project gain access to an instance of the DB project class w/o ever referencing the project directly?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You would use only interfaces in your web project. The DB project class implements these interfaces. So your DB project would need to reference whichever project defines the interfaces.

Your startup project would use dependency injection to wire up concrete DB classes to the interface when and provide them when creating the web classes.

So, one way to do this is have these projects

  • IInterfaces – interfaces (not concrete classes) used by the Web project
  • Web – uses IInterfaces
  • DB – uses IInterfaces. Defines concrete implementations
  • Startup – use IInterfaces, Web and DB. Instantiates Web classes and DB classes, injecting concrete dependencies for interfaces

You could combine some of the above depending on how much you want to reduce coupling. For instance you could combine Web and Startup.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x