Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewModel object needed in 2 projects Core and Web - Circular Dependancies

Tags:

c#

asp.net-mvc

I have an MVC application that consists of 3 projects Core, Repository & Web.

References are as follows:

  • Core references Repository
  • Web references Core

My ViewModels sit in the Web project, which are being used by controllers.

In the Core project I have an email class that I want to recieve the bound ViewModel object as a parameter (to send an HTML email of the record).

The problem is that I can't use that object as a parameter in the email method as it will create a circular dependency.

Any ideas how I can have that object in 2 projects at once??

like image 823
JimmyB Avatar asked Mar 17 '23 10:03

JimmyB


1 Answers

It sounds like the view model is holding domain information, which it shouldn't be doing. Specifically, it represents a structure of data which belongs in Core because Core has functionality that needs it.

Two main approaches to this are:

  1. Create a model in Core and the view model can have that model as a property.
  2. Create a model in Core and the view model can replicate its structure. (Which would likely involve conversion methods between the two at some point.)

In either case, if you have a model that belongs in core then build it there.

What is that model? I really can't know for sure based on the description. For now I'm going to call it an EmailTemplate. In that case it sounds like you have this:

Core Assembly
    Email Object
        Send Method (View Model parameter)

Web Assembly
    View Model Object
        properties

Instead, you'd want this:

Core Assembly
    Email Object
        Send Method (Email Template parameter)
    Email Template Object

Web Assembly
    View Model Object
        Email Template property

In fact, you might not even need a view model at all and just bind the view to the domain object. But that's really tough to say without knowing more.

As you're discovering, dependencies should only ever point inward to the domain core. (In fact, the Repository references are also backwards, but that's outside the scope of the question.) Either the functionality and the structures to support it belong in Core, or the functionality itself belongs in Web. The structure of the domain in the question is a little vague so sorry if this is light on details, but the principle is the same.

like image 173
David Avatar answered Apr 06 '23 07:04

David