ASP.NET MVC4 – N-Tier Web Application with Console Application

asp.net-mvc-4c

I've built an MVC web application and I need to run several tasks at different time intervals. So I've also made a console application to schedule these tasks through task manager in windows. For example, the application needs to send out an email every week summarizing the program's data. I've chosen to use the Nuget RazorEngine package to format the emails because I already have to show the same data on a webpage too, so by using the RazorEngine package, I can re-use my viewmodel for both the email and the UI of the web project (just use it in a template formatted for email instead of web). But therein lies the problem, the ViewModel is in the UI project. I don't think the right thing to do here is for my console application to take a dependency on my UI layer. The logical decision to me at first was "just make another layer, App.ViewModelBuilders (maybe also help me with a better name?), and take a dependency in both App.UI and App.ConTasks" But I've never seen that done before, so I'm not sure if that is a wise choice or not…

Here's my current layers (projects) for the solution:

  • App.DAL (No Dependencies – Unit of Work and Repository pattern)
  • App.Service (Depends on DAL – uses DAL for business operations, such as getDataWithCondition() which returns the relevant objects (it's actually an IQueryable)
  • App.UI (Depends on Service – gets the data back from service and runs it thru a ViewModelBuilder to map the business objects to the appropriate view model, and then renders a view using that view model)
  • App.ConTasks (SHOULD depend only on service, but still needs ViewModels – uses data returned from service to render view formatted for email and send the email)

What Layer should my View Models and View Model Builders go in?

Best Answer

View Models (and their builders) can go in App.Service or App.UI.

Your controllers will have to take a dependency on the Service Layer anyway; the controller methods will obtain application data from the Service Layer, marry it with the View Models it obtains from the Service Layer or from App.UI, and return a suitable view.