Based on. NET platform's layered architecture combat (vi.)--Dependency injection mechanism and IOC design and implementation __.net

Source: Internet
Author: User
Tags reflection

We design a layered architecture where layers should be loosely coupled to each other. Because it is a one-way single call, the "loose coupling" here actually means that the upper class cannot be specifically dependent on the underlying class, but rather relies on an interface provided by the lower layer. In this way, the upper class cannot instantiate the class directly in the lower layer, but only holds the interface, and the dependent injection mechanism determines the type of variable that the interface refers to in the end.

This is done in order to achieve a "replaceable" design between layer and layer, for example, it is now necessary to implement the data access layer in a different way, as long as the implementation follows the data Access layer interface defined previously, the business logic layer and presentation layer need not be changed, only the configuration file system needs to be changed to function correctly. In addition, the system based on this structure can also realize parallel development. That is, different developers can focus on their own level, only the interface is defined, the development of things can be seamless connection.

On the Java EE platform, the spring framework is used primarily to implement dependency injection. Here, we will make ourselves a dependency injection container.

The theoretical basis of dependency injection is the abstract factory design pattern, which is briefly introduced in combination with concrete examples.

The example of the data access layer shows the application of the abstract factory model. As pictured, there is a data access layer for access and SQL Server two databases, all of which implement the data Access layer interface. Each data access layer has its own factory, and all factories are implemented from Idalfactory interfaces. The customer class, which is the business logic layer class, is only coupled to the factory interface, the data Access layer interface, and not to the specific class, so that different data access layers are available as long as the configuration file is used to determine which factory is instantiated.

However, while this design works, the code is redundant because it requires a factory to be written for each implementation of the data access layer, as does the business logic layer. In the past, we were helpless, but. NET platform provides us with a solution to the reflection mechanism introduced in this paper. With reflection, each layer requires only one factory, and then dynamically loads the corresponding class by reading out the name of the assembly from the configuration file. In addition, in order to improve the efficiency of the dependency injection mechanism, the caching mechanism is introduced here. Here's a concrete implementation.

Configuration
First, you need to add the following two entries under the Web.config files node of the Web project:


These two configuration options store the assembly names of the data access and business logic tiers to be applied, respectively. Value is currently empty because there is no concrete implementation at all levels.

Implementing cache Operations Auxiliary classes
To implement caching operations, we encapsulate the caching operation into a helper class, placed under utility engineering, with the following specific code:

CacheAccess.cs:

Encapsulating Dependency Injection Code
Because many dependency injection codes are very similar, in order to reduce repetitive code, we encapsulate reusable code in a class first. The specific code is as follows (this class is placed under the Factory project):

DependencyInjector.cs:
Cacheaccess

Code highlighting produced by Actipro Codehighlighter (freeware)
http://www.CodeHighlighter.com/

--> 1using System;
2using system.web;
3using System.Web.Caching;
4
5namespace nguestbook.utility
6{
7/**////
8///Auxiliary class for caching operations
9///
Ten public sealed class cacheaccess
11 {
12/**////
13///To add an object to the cache
14///
15///Cache Key
16///Cache Objects
17///Cache Dependencies
The public static void Savetocache (String CacheKey, Object CacheObject, CacheDependency dependency)
19 {
Cache cache = Httpruntime.cache;
Cache. Insert (CacheKey, CacheObject, dependency);
22}
23
24/**////
25///Gets the object from the cache and returns null if it does not exist
26///
27///Cache Key
28///acquired Cache object
The public static object Getfromcache (String cachekey)
30 {
Cache cache = Httpruntime.cache;
32
Cache[cachekey];
34}
35}
36}

  Dependencyinjector

Code highlighting produced by Actipro Codehighlighter (freeware)
http://www. codehighlighter.com/

--> 1using system;
 2using System.Configuration;
 3using System.Reflection;
 4using System.Web;
 5using System.Web.Caching;
 6using NGuestBook.Utility;
 7
 8namespace nguestbook.factory
 9{
10    /**//// 
11    ///  Dependency Injection provider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.