In "ASP.net mvc Razor Engine: Razorview" introduced Buildmanagercompiledview, We talked about the default use of Viewpageactivator using the currently registered Dependencyresolver to complete the activation of the target view. This means that we can implement an IOC view activation by registering a custom dependencyresolver. In this article we will demonstrate how to implement integration with the IOC framework Ninject by customizing view.
We define a ninjectdependencyresolver with the following definition, which has a ikernel type of read-only property kernel, which is initialized to a Standardkernel object in the constructor. For the implementation of the GetService and GetServices methods, we directly call kernel's tryget and getall to return a list of instances and instances of the specified type. In order to facilitate the type mapping, we define the Register<tfrom,tto> method of the generic type.
1:public class Ninjectdependencyresolver:idependencyresolver
2: {
3: Public ikernel Kernel {get; private set;}
4: Public ninjectdependencyresolver ()
5: {
6: This . Kernel = new Standardkernel ();
7: }
8: Public void Register<tfrom, tto> () where Tto:tfrom
9: {
A: this . Kernel.bind<tfrom> (). To<tto> ();
One: }
: Public Object GetService (Type servicetype)
: {
"Return" this . Kernel.tryget (servicetype);
: }
: Public ienumerable<object> getservices (Type servicetype)
: {
: Return this . Kernel.getall (servicetype);
: }
20:}
We are demonstrating a scenario for multilanguage support, so that some of the content that is output from the view changes dynamically as the current thread uiculture, we define an asp.net MVC application that reads the resource content abstract class ResourceReader. Resources Here are a broad concept that does not enforce constraints on storage, and we can use resource files to store resource content. For simplicity, ResourceReader only defines a unique GetString method to get a string of the specified name.
1:public Abstract class ResourceReader
2: {
3: Public Abstract String GetString (string name);
4:}
We used the resource file to define the data source by default, so we added two resource files Resoures.resx (language culture neutral) and Resources.zh.resx (Chinese) to the project, and added the resource items (HelloWorld) as shown in the resource file.
Then we create a default defaultresourcereader that reads the resource file we added to get the string that the GetString method returns (static type resources is the type that the Add resource file is created automatically).
1:public class Defaultresourcereader:resourcereader
2: {
3: Public Override string GetString (string name)
4: {
5: Return Resources.ResourceManager.GetString (name);
6: }
7:}
To allow ResourceManager to be applied to all of the view, we created the following base class localizableviewpage<tmodel> for the entire view of the application. The type is a subclass of webviewpage<tmodel> with a property ResourceManager of type ResourceManager. Because the Ninject.injectattribute attribute is applied on this property, it means that the property is automatically initialized in the form of "property injection."
1:public abstract class Localizableviewpage<tmodel>: webviewpage<tmodel>
2: {
3: [inject]
4: Public resourcereader ResourceReader {get; Set }
5:}