A ViewModel is required in two Core and Web projects - Circular Dependencies

I have an MVC application consisting of 3 Core, Repository, and Web projects.

The links are as follows:

  • Basic Link Repository
  • Core Web Links

My ViewModels are hosted in a web project that is used by controllers.

In a Core project, I have an email class that I want to receive as a linked ViewModel as a parameter (for sending HTML email).

The problem is that I cannot use this object as a parameter in the email method, as it will create a circular dependency.

Any ideas how I can have this object in two projects at the same time

+4
source share
4 answers

It appears that the view model contains domain information that it should not do. In particular, it is a data structure that belongs to Core, because Core has the functionality it needs.

Two main approaches to this:

  • Create a model in Core, and a view model can have this model as a property.
  • Create a model in Core, and a view model can replicate its structure. (Which is likely to include conversion methods between them at some point.)

In any case, if you have a model that belongs to the kernel, then create it there.

What is this model? I really can't know for sure based on the description. Until I name it EmailTemplate. In this case, it sounds like this:

Core Assembly
    Email Object
        Send Method (View Model parameter)

Web Assembly
    View Model Object
        properties

Instead, you want:

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

Web Assembly
    View Model Object
        Email Template property

. , .

, . ( , .) , Core, Web. , , , .

+2

. Core - ... ( ), , , .

- Core, , Core. - , .

+2

- Email ViewModel . mapper (, AutoMapper) ViewModel. , , .

+2

Your email class should not link to ViewModel. A ViewModelis for use View.

I would map the values ​​of your viewmodel to the model and work with the model (and your model should be placed in a data store that stores information). To do this, you can use Automapper or write your own display layer.

+1
source

Source: https://habr.com/ru/post/1570487/


All Articles