ASP. NET-oriented interface extensible framework core container (including test code download)
The container part of the new frame has finally been tuned! Containers are so important that they are useful for a noun called a "core container".
Why are containers so important? It needs to be said.
1, first of all, we start from the framework name-oriented interface programming, what is interface-oriented programming? (This degree Niang answer a bit)
READ: A class is the definition of an individual (modeling), and each aspect of an individual can be an interface
White point, one interface can represent an aspect of an object (class), and again the point object may be generalists (inheriting multiple interfaces) and can work in different scenarios (as instances of different interfaces)
Second, each interface can have a different implementation, as long as the implementation of this interface, basically can replace this location to work properly
2, I think interface-oriented programming is essentially object-oriented programming, but the level of abstraction is higher, more convenient to expand and replace
We need a lot of objects to support the system's functional needs and expansion requirements, but also need to be in each location of the object convenient "at any time" to replace, expand
So how do we manage this huge library of objects, and how is it easy to orchestrate it for management? Here is the container, the container can be very good for the job.
3, the role of the container
I think containers are warehouses, cabinets, boxes, so that we can store our own objects in the way we like, so that we can find them conveniently when we need them.
A good container can also maintain object creation, lifecycle, inversion of Control (IOC), Dependency Injection (DI), interception method Execution (AOP), etc.
There's too much crap in front of you, first example.
I. Creation and management of objects is the responsibility of the container
1. The following is also an example of the previous context, using a container to simplify the following code
The above code is not simple and beautiful much
2, the code is simple, the function is no moisture oh, look at the following implementation results
is not the same as the result of the previous article.
3, the result is nothing, change a configuration (do not change the code), see the results
4, then change the configuration (or not code)
This time vivid, the family each has each individuality, is not very dazzling ah.
5, we look at the configuration
Discerning eye can see that this is the configuration of the unity container; some people say, don't you rely on unity containers? I swear I didn't rely on unity containers, but I didn't say I didn't have unity. I'm just going to make a framework, and other technical solutions can be integrated, as well as containers.
You know unity with unity, you are familiar with the spring.net can also be used with ease, as long as you encapsulate and inherit the framework of the container interface, and then register in it will work properly
Second, using unity containers, but not relying on unity container
1, the place to call the container is not dependent on unity
This test project, in addition to a few references to the system, only refers to the main frame.
2. The main frame does not reference unity container
You're not kidding, are you? Without relying on unity, the main framework has not relied on (referencing) any third-party components except for a few simple system references, but this does not affect my use of a large number of third-party libraries, as this is the "interface-oriented extensible framework"
is not now again on the interface has a further understanding, is not "interface-oriented extensible framework" the name is a bit of a veritable taste.
Three, the following to decrypt how he actually run
1, look at the portal, see the application (here is the console program) reference
Haha, was not found, and finally saw unity quoted by the
But don't be fooled by appearances, in fact the main character here is fang.unity, because this project can not directly refer to unity container, Fang.unity is the package of unity container.
The console program is run-time dependent on the unity-related DLLs, not compilation dependencies, which are directly referenced here for ease of debugging
2, continue to explore the principle
Fang.unity not always run automatically, right? Of course not, on the code
There is a very insignificant, but very important line of code "Fang.Unity.ContainerFactory.Init ()", he is "the culprit"
In other words, the Unity container is completely run as a plug-in in this framework, and if I do not call Unity's init, call Spring.net's init, then all the places currently called are spring.net containers.
Some people say this is too inconvenient, of course, not every time the use of the container to be called, if the Web project can be called in the global Application_Start is OK, can also be called in the HttpModule init.
Most of the time we use containers extensively in development, but don't think about what containers I use specifically.
In other words, I am familiar with the Unity container, I use the Unity container configuration debugging through the components for you to use, but you always use spring.net (or other containers), you call the Spring.net container package, the configuration modified to spring.net configuration, Code to work as expected
Note: As long as component development is supported by the framework's container, there is no direct reference to the Unity container and the code that calls the Unity container directly
3, or take a look at Fang.Unity.ContainerFactory.Init is what Ghost bar (haha, or not sleep)
Unity container does not unfold here, I will open a single article about Unity container and unity package, etc.
Iv. Core Container parsing
1. Container related Implementation class
2. The core class diagram is as follows:
A little analysis of the division of the various classes:
The A:icontainer interface is the basic function needed to define a container
B:icontainerfactory defining the Container factory interface
C:globalcontainer is a static configuration class that provides an API to register external container component functionality and provide a call container
This is the key to implement the container plug-in, this implementation is referenced by the dependencyresolver of ASP.
D:simplecontainer provides a simple and default container and container factory implementation
Appcontext in the text container is his implementation, if you do not configure the extended container function, the use of the container is him, but he is actually a dictionary cache, save something, take a thing, no problem, complex configuration and Di, etc.
E:containerwrapper is a container blocking class that intercepts container operations in order to add features and functionality
After this, there is a very interesting feature.
F:containerfactorycachewrapper is a container factory package and cache
That is, outside the container factory is not to consider the single-mode cache what, he arranged, and the production of each good container object inspection and packaging good
Five, multi-container application
1. We need multiple containers to orchestrate many objects
Like I bought a lot of things from the supermarket, there are eggs, ribs, vegetables, children's toys, clothes and so on. I can not get a big box all put in, also do not disorderly put, can not put clothes and ribs together in the refrigerator (will be scolded by the daughter-in-law death). Ribs into the freezer freezer, eggs and vegetables into the refrigerator freezer. Children's toys in children's beds. Clothes and a wardrobe.
The code is fine-tuned to see the results of the run
Some people say, the more you change the more complex Ah, the code is a little more complex, but the configuration is more organized sometimes it is worthwhile, look at the configuration
This is just a simple way to split the container, but also by the object map to divide the container, it is more reasonable, because there are mutual dependencies between objects, using the container to do control inversion that is the meaning
If it is a container tool that supports sub-containers (unity supports), a system can divide the container by tree, the whole system's public configuration is the root container, each subsystem has its own container configuration, each subsystem has its own container configuration, the child container can inherit the parent container can also overwrite the configuration of the parent container
2, below to do a tree plaint container example
This effect is not a leverage, code is also beautiful mess, is how to configure, also look at it
To summarize, the container name is a graceful chain syntax, the configuration file is a tree-like management structure, perfect!!!
Vi. Container Expansion
1, Simplecontainer container not "simple"
Before the introduction of the IContainer interface, a careful look will be found to add (regist) and read (Resolve) method, but there is no way to delete objects, we want to delete what to do
But Simplecontainer but there is a remove method, the use of the framework of the container function is to give up their more powerful container function is not a bit regrettable, can fish and bear cake both? Of course, the context is implemented with Simplecontainer, and the end of the scope removes the object from the container.
Let's give a simple example here to illustrate
The more versatile technique of using the full framework container support or the ability to use a custom container is that the IContainer interface has a provider property that exposes the provider property of the original container object, as long as the extended function that you want to extend exposes is placed in the provider attribute.
Of course, as with Simplecontainer definition provider, you can return directly to the container itself.
The attentive user may notice that there is something new in this example (Fang.Framework.Factory.Create), which is the same as the previous GlobalContainer.Factory.Create effect, Fang.Framework.Factory is a static class that aggregates the main factory methods of the framework to find and use them.
Note: This example is to use the Simplecontainer to run this test example, the Code "Fang.Unity.ContainerFactory.Init ()" Comment out
However, it is very easy to emphasize that this extension is simple, but it is not recommended to use
Because once you have forced type conversions on provider, you rely on a specific container implementation, so that the code may not adapt to other containers, which means that your code relies heavily on a particular container, which violates the spirit of this framework.
In a word, you need more features to sacrifice some versatility, and if each piece of code is a special case and cannot be extended, then the system should be in trouble.
2, the use of containerwrapper extension
2.1 The container (IContainer) object which is produced by the frame is containerwrapper packed before the containerwrapper is mentioned. and Containerwrapper intercept all IContainer methods.
Simply define a container wrapper class to inherit Containerwrapper, rewrite the functionality you want to extend, define a wrapper container object returned by the Containerfactory class (Inherit icontainerfactory), The framework calls a Checkwrapper method to process the newly generated container object, and if the object can be converted to a Containerwrapper object, it does not need to be wrapped again.
2.2 Another extension technique is to register the container factory in a way that does not extend
The container factory is then extended again to return the Containerwrapper object, which is its own definition of the container encapsulation function instead of the framework default encapsulation logic
It is possible to develop a general purpose di markup extension in the future, and it is intended to use this method to check whether the current object or type has a corresponding callout (Attribute), and if there is a call to special logic processing, without a unified di callout, using di will have to rely on a particular container. This can affect code reusability and mobility.
Seven, the container "black technology"
1. Quick Search
Examples of using black technology to transform a tree structure
You can configure many containers to manage objects, but instead of dealing directly with each container, you can use any one of the container objects to tune any object configuration, as long as you are named by a certain rule.
If the above black technology with Di, then more perfect!!!
Note: There is an episode in which an engineer uses the container configuration Di, pointing to a node inside a configuration file to say to me, I want to put this node di on my controller. I explained to him, "Di can't do that, you have to put this node in the root container, or define a partition and point to a container, and then copy that node into the new container." The engineer listened to the fog, confused and said, "I just want to map this node to my controller,..."; With this black technology, I can safely tell him that you write a di callout on it.
2. Fast retrieval of context containers
The context is implemented through a container, and is it possible to retrieve it uniformly? The answer is yes, the above example is
Context container retrieval provides a special name "$" (the name is a little vulgar, I did not think better, who is better to suggest), using "$.name" to retrieve from context, theoretically, the value of the context container should also be able to support Di, of course, the time of the DI should be after the context container value is generated
Write so much, there are a lot of things to write, there are many features to be developed, and then add it.
Provide test source code download.
In particular, the framework is still in development, and has not been fully tested, do not exclude the possibility of major bugs, do not want to temporarily get their own reality project, the consequences of the problem is self-contained, and this framework has not formally open source, no open source authorization.
Tags: interface-oriented, ASP., Framework, container, Unity
interface-oriented extensible framework