Should I inject services into my MVC views?

We are working on a cloud-based CMS using ASP.NET MVC technology and have found some obstacles. There are a number of parameters that the user can change through the control panel, which we need to get into Views. For example, the Facebook application identifier for initializing the Facebook JS API. Or additional text to be displayed on the page. Or background image. While we do not use DI to pass these parameters, we add them to the ViewModel instead, but this disrupts the ASP.NET MVC way of working with models (e.g. form validation, bindings, etc.).

It seems that using DI to implement services to provide options, texts and images can make my views less dependent on specific controllers, and even there is some kind of Microsoft method http://www.asp.net/mvc/tutorials/hands-on -labs / aspnet-mvc-4-dependency-injection # Exercise2 . However, there are many answers on forums against injecting services in submissions using DI.

So the question is: what is the right way to implement some services in Views? Or should I not do this at all, and is something wrong with the design of the application?

UPDATE : some real code examples (now we use Model to enter services)

Entering text from the database (they must be editable, since this is a CMS):

<div class="steps">@Html.Raw(Model.Texts["Main", "Step2"]</div>

Introductory translations from the database (this is actually localization):

<div class="gonfalon">@Model.Translations["Title_Winners"]</div>

( , , , facebook ):

Facebook.Initialize(Model.Parameters["FbApplicationId"], Model.Parameters["FbApplicationSecret"]);

, . , , facebook. , -, , View (, ).

2: :

public static class WebViewPageExtensions 
{ 
    public static I ResolveService<I>(this WebViewPage page) 
    { 
         return DependencyResolver.Current.GetService<I>(); 
    } 
} 
+5
3

, , ...

, , , . , , ? ?

asp.net mvc , . . , .

, - - . , , .

 public static IMyViewServices MyServices(this WebViewPage view)
     {
         return DependencyResolver.Current.GetService<IMyViewServices>();
     }

IMyViewServices, DI, http ()

+6

, . ? :

, , . , . , . , .

, , "".

, , . - , ( ).

+3

, .

"" MVC , , - , .

GET , , . POST , , .. .

, .

+2
source

All Articles