The adventures of snails (ii) Web Framework (I) and the adventures of web
As mentioned in the previous article, this article describes how to use Autofac to manage the entire platform's lifecycle (Elementary ).
I. Brief Introduction
Plug-in Web developers should remember the Assembly-Level Attribute PreApplicationStartMethod, which can be started at the beginning of application startup (before Web startup, we usually use it to copy plug-in resources and dynamically load plug-ins.
[assembly: PreApplicationStartMethod(typeof(PreApplicationInit), "InitializePlugins")]
The code it specifies:
Public static void InitializePlugins () {PluginManager. Current. Initialize (); // plug-in Manager initialization}
After all the resources are copied, the plug-in is also completed by Assembly. Load and enters the formal stage. We will initialize IOC in the Application_Start method of Global. asxc.
Ii. Autofac Initialization
Step 1: Use the Autofac modular initialization method to initialize the public modules of the platform
internal static void BuildFrameworkIocEnviroment(){ var builder = new ContainerBuilder(); builder.RegisterModule(new MvcModule()); builder.RegisterModule(new WebApiModule()); builder.RegisterModule(new LoggingModule()); builder.RegisterModule(new EventsModule()); builder.RegisterModule(new CacheModule());}
Step 2: Use automatic initialization to register to IDependency, ISingletonDependency, and ILifeScopeDependency. The registration results are as follows: generate each time, generate a global Singleton, and generate a singleton in each request cycle.
var dependency =assemblyTypes.Where(p => (typeof (IDependency)).IsAssignableFrom(p) && !p.IsAbstract && !p.IsInterface).ToArray();var singleton = dependency.Where((typeof (ISingletonDependency)).IsAssignableFrom).ToArray();var lifescope = dependency.Where((typeof (ILifeScopeDependency)).IsAssignableFrom).ToArray();var perdependency = dependency.Except(singleton).Except(lifescope).ToArray();containBuilder.RegisterTypes(singleton).AsImplementedInterfaces().SingleInstance();containBuilder.RegisterTypes(lifescope).AsImplementedInterfaces().InstancePerLifetimeScope();containBuilder.RegisterTypes(perdependency).AsImplementedInterfaces().InstancePerDependency();
Iii. Use of Autofac
Why do I need to use IOC such as Autofac to initialize containers? In plug-in project development and interface-based programming projects, plug-in functions are integrated, and business logic is concentrated on interface implementation. IOC does not help us manage the object lifecycle in this respect, it is also convenient to implement functions directly to interface-oriented programming (the most common practice is to register the implementation as the corresponding interface ).
During registration, we adopted the "4" registration method. In Web projects, the most common application is the single instance within the request cycle. The so-called single instance within the request cycle refers, A request arrives in the Web application domain environment until it obtains the Response and leaves the application domain in a Singleton, and each request does not interfere with each other. First, it facilitates data transmission. Second, it greatly reduces object generation and saves memory overhead.
How to Implement "interface-oriented" programming?
First, we define an interface and its implementation.
public interface IService:IDependency{ int DoSomeBusiness();}public class IntService:IService{ public int DoSomeBusiness(){ //do something return 0; }}
Then we use constructor injection:
public class HomeController:Controller{ private readonly IService _service; public HomeController(IService service){ _service=service; } public ActionResult Index(){ var val=_service.DoSomeBusiness(); return View(val); }}
Look, this is the rough interface programming. The data we want to display on the page is implemented through the IService interface. If the business logic changes, we can define the implementation of the new IService. Of course, in this example, we can easily transform it into a property structure.
This is a brief introduction to the frameworks such as Autofac. There are also many introductions about IOC and Autofac in the blog park, so I will not elaborate on them, here we will mainly introduce the use of Autofac in my projects. Next, we will introduce the implementation of the log framework.