Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can MVC Views access all projects even though they aren't referenced by the project where the views are?

Ok, so I'm a bit confused as to what is going on with the following data.

We have the following Structure in our application:

  • Portal.Web - An MVC 3 Web App which basically contains all the views, scripts, css and HTML helper extension methods
  • Portal.Core - A Class Library which is basically our Business Objects, we have all of our models contained within this project.
  • Portal.Data - Another Class Library which contains our NHibernate config and our DTO classes.

Here's our usage: In the controller we call the model located in Portal.Core, which populates by calling Portal.Data, so basically Web can never see data.

Here's the catch: In the controller, say for example I try and instantiate a new DTO object called Client like so:

var client = new Client();

It won't work, which is expected it has no idea what Client is and even specifying a using won't cut it. That's fine.

BUT if I try and do that exact same line in the View, Resharper adds the using to the view and then no complaints, the project runs and we can use DTO classes in our views.

So the question is, why is this? I'm trying to stop our juniors from using DTO classes in Views, so I've purposely removed the reference to the Data project in Web, but they can still use the classes. Can someone shed some light?

like image 806
mattytommo Avatar asked Apr 05 '12 10:04

mattytommo


1 Answers

I ran the same test with ASPX and Razor views. Referencing Client in ASPX views fails however in Razor views they work. The views are compiled on the fly when you request the application, so I had a look at some folders in "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\portal.web" and from cmdline files references to the assembly is explicitly added when the view is compiled.

It seems the process that compiles razor views adds references to all the assemblies in the bin folder. However, looking at the source of ASP.NET MVC, I cannot confirm this.

So, the only conclusion I can come to is that it is a side effect of using the Razor View Engine.

That said, you may want to scan the web.config to see if it was included using the assemblies element.

like image 96
bloudraak Avatar answered Sep 29 '22 10:09

bloudraak