A lightweight IOC object container under Elite Container Delphi

Source: Internet
Author: User

First, Introduction:

Elite Container isDELPHIUnder the light of a lightweightIoCObject Container (IoC: Inverse of control, inversion controls). It refers to the spring framework in Java (mainly the configuration file) and combinesDELPHIFeatures to build. Compared to spring's object container, it provides more streamlined features (such as Object delay creation, object attribute auto-injection, etc.), reduces the learning difficulty, and provides many extensibility points, you simply write a plug-in implementation class, and in the configuration file simple configuration, you can let elite Container has your own custom features!

Elite Container has some of the following features:

1, regardless of use or do not use BPL, you can use elite container to build your program (but do not recommend the use of pure DLLs, because each DLL, the same class type information is different, not familiar with the relevant knowledge, there may be some problems, and this is not the elite Container's problem)
2. Support multiple attribute injection types, such as direct value, enumeration, collection, Stringlist, ObjectList, etc. Which stringlist this injection method, in resolving key value pairs configuration is very useful, see the relevant examples in detail
3, support constructor injection. But becauseDelphiMeta-information is not complete enough, sometimes you need to write a constructor call class (very simple), you can let the container correctly call the class and its subclasses of the constructor, in detail, see the relevant example in addition, because the constructor injection has the potential to cause cyclic injection, it is recommended to use attribute injection as far as possible to express the object's dependency, Use constructor injection only when you have to.
4, support the inheritance of object configuration. When you omit the configuration sub-object, it is cumbersome and difficult to maintain when you re-copy the property configuration of the parent object again.
5. Support automatic injection of object attributes. If your attribute name satisfies certain rules, you can use automatic injection to save a lot of configuration writing
6, to support the introduction of configuration files, improve the reusability of the configuration file
7. Support life cycle configuration of multiple objects (for stateless management classes, such as logical layers, the single life cycle is the most common)
8. Object-Supported alias configuration
9, enhanced object registration mechanism, can register non-tpersistent as the base class (common such as tinterfacedobject), and the container can distinguish between different units of the same name class
10. Compatible useDelphiRegisterClass to register the legacy modules of the class
11, a large number of custom plug-in extension points. You can customize your own property injection configuration, constructor injection configuration, object configuration Introduction, Object Lifecycle management, object attribute auto-injection, etc.
12. Deferred creation of supported objects. In case the container initializes, it automatically instantiates some classes that will take a lot of time to create and seldom use.
13, a large number of programmable interfaces. You can even write yourself a configuration using INI, or a database.IoCContainer!

With Elite container andIocIdeas, you can more easily build applications that are loosely coupled and highly reusable. Its core idea is to split the function interface and implementation, the upper layer only relies on the lower interface, and then through the elite container configuration, the different implementation classes injected into the interface, to achieve the replacement function, that is, the purpose of reusing the existing code. Designers can really use their own object-oriented ideas and related design patterns to architect enterprise-class applications withoutDelphiWith the object-oriented always a little catch see the feeling of the sleeve.

demo:http://download.csdn.net/source/1891434

Ii. Typical development steps using the Elite container:

1. Create a new project, select "Build with Runtime packages" in the project options, remove all default packages, and select EliteContainerD7
2. Building application domains (e.g. object entities, logic processing, interface services, etc.)
3. Configure these objects with an XML file
4. Referencing the Elxmlconfiguratecontainer unit, declaring an interface variable of type Ielobjectcontainer
5, to this interface to create a Telxmlconfiguratecontainer instance, the path of the XML file (relative or absolute path can be, the constructor has parameters specified), as a parameter passed into the constructor
6. When Telxmlconfiguratecontainer is created, the automatic initialization lifecycle is configured as "singleton" and is not a deferred object creation
7. You can now get the configured object or interface from the container based on the object ID or alias configured by the configuration file.
8, call the acquired object or interface, to complete the function of the program

Three, the demo directory Description:

Executable program for each bin example
Bin\config the configuration files used by each example
The DCP files used by the Lib examples need to be introduced in the project Setup "Build with runtime packages"
Source code for each instance of source. A total of 26 examples for details on the features of the elite container. Be sure to combine the source code, configuration files, and executables to understand this in order to achieve the purpose of the presentation. If you want to compile the example source code, please add the Lib directory to the Delphi Library Path, and modify the output path of the next project.

Iv. Brief description of the demo:

Example 1: The most basic functions of the container are demonstrated. How to get a well-configured object, including the object form (getobj), and get it in the form of an interface (getintf).

Example 2: The most basic functions of the container are demonstrated. Unlike example 1, in this case, the class is encapsulated in the BPL. This approach is the most common way in which a project is actually developed (in order to reuse business logic). Note that when configuring an object, the object's class is configured, plus the BPL file where the class resides, and then the class is registered in the program.

Example 3: Demonstrates how to inject a direct value (string, integer, etc.) into a property. This approach is often used to change some initial state of an object using configuration, such as the ability to inject a database link string into an object in such a way that it can change the database link of the program without recompiling.

Example 4: Demonstrates how to inject an enumeration or collection into a property. The powerful is that, in the configuration, you can write both the enumerated string name and the integer value of the enumeration.

Example 5: Demonstrates how to inject other configured objects into a property. A property can be an object type, or it can be an interface type (if the object is injected, the interface is implemented). This is the most commonly used injection method, such as injecting a data access object into a business logic object to allow the business logic object to access the database.

Example 6: Demonstrates how to configure a Stringlist object and inject it into a property. As you can see from the configuration file, there are many ways to configure it. The configuration of the two properties, Keyvaluelist and Objectreflist, takes the form of a class key-value pair, which is commonly used when resolving mapping relationships.

Example 7: Demonstrates how to configure a ObjectList object and inject it into a property. Note that the Ownsobject parameter of ObjectList is set to False by default, that is, releasing the ObjectList will not release the object inside. Because the single-instance declaration cycle of elite container is the most common, the object's life cycle should be managed by the container and should not be released when the ObjectList is released. However, the ObjectList can also be configured directly to manage the life cycle, for example, the life cycle of objects within it is configured as "prototype". You need to know clearly how the life cycle of an object is managed in order to properly use this injection method.

Example 8: Demonstrates how to configure a Interfacelist object and inject it into a property.

Example 9: The function of automatic injection of object properties is demonstrated. You can set an attempt to get an object from a container based on the property name ("T" plus the property name as the object ID), the property type, or the automatic mode. Auto-injection is used for those properties that are not manually configured, are not read-only, and are types that are objects or interfaces. Auto-injection is turned off by default (the value is "false"), and by property name is the most commonly used auto-injection mode.

Example 10: A property configuration that inherits the parent object is demonstrated. To be able to configure the file to see that the values of the count, Text, day three properties of TMYOBJECTB inherit the settings from the tmyobjecta. The powerful is that you can also override the property configuration, and you can see that the value of day in TMYOBJECTB is Wdtuesday, not the wdmonday configured in Tmyobjecta.

Example 11: Constructor injection is demonstrated. Note To configure the type of constructor arguments, the common values are string, Integer, Float, Boolean, String, enum, set, object, interface, and so on.

Example 12: The introduction of object configuration is demonstrated. Usually we can write separate configuration files for each component, and then the main program will introduce the configuration files of the several components. This reuses the configuration file for the component. Note that if the configuration of the object is very large, you can delay loading the configuration file by setting lazy= "true".

Example 13: The life cycle configuration of an object is demonstrated. The default three life cycle is: Singleton "Singleton", the object's instance is managed by the container (do not release it itself), and each fetch is the same instance, this life cycle is the default life cycle, the prototype "prototype", the container is only responsible for the creation of objects, Instead of being responsible for the release of the instance, you need to manage its life cycle, and the thread singleton "Per-thread" is essentially a singleton pattern, but each line thread has a different singleton.

Example 14: An alias declaration for an object is demonstrated. This feature is often used to consolidate resources, such as several components declared the database link object ID, I can declare the alias, the database linked to the object ID all point to the same object, then implemented a few components with the same database link.

Example 15: After the injection is completed, the object is initialized for processing. It is often possible to check that the properties of an object are injected here, because the phenomenon of write-leaking configuration is sometimes unavoidable.

Example 16: Demonstrates how to use RegisterClass to register a class's old module. In fact, the use is exactly the same, there is no special place.

Example 17 (Advanced function): Demonstrates how to injectDELPHIThe class that comes with it. It's important to note that there's nothing special about having to register these classes manually.

Example 18 (Advanced function): Demonstrates how to obtain a class with the same name in a different cell. In the configuration, class takes care to add the cell name where the class resides.

Example 19 (Advanced function): Demonstrates the processing of custom attribute injection. You need to write a property value recognizer and a property value injector separately, and then configure it in the configuration. As you can see, in the example configuration, the properties appear with a custom type of "path".

Example 20 (Advanced function): Demonstrates the processing of custom constructor injections. You need to write a constructor parameter value recognizer and constructor parameter value injector separately, and then configure it in the configuration. As you can see, in the example configuration, the constructor parameter appears with a custom type of "Owner-selector".

Example 21 (Advanced function): Demonstrates how to customize the introduction mechanism of object configuration. In the example, the configuration of the object is encapsulated in the DLL, which has a simple protection function. BecauseDelphiis heavily used in the client program, which is different from Java. So this can be said to haveDelphifeature a very useful extensibility point.

Example 22 (Advanced function): Demonstrates the life cycle management of a custom object.

Example 23 (Advanced function): Demonstrated custom object property auto-injector writing.

Example 24 (Advanced function): Demonstrates how to write a constructor function caller. You need to write a constructor for the class when the following two cases occur: the constructor of the base class is not a virtual function, and you declare the constructor in a subclass, although the constructor of the base class is a virtual function, but in the subclass you have either re-declared it or provided an overloaded version. General caller logic, need to check the number of parameters, signatures, according to the overloaded version, determine whether the parameter exists, the parameter type is correct and so on.

Example 25 (Advanced function): Demonstrates deferred acquisition of object properties. When getting texample in the example, three properties are not valued, only the configured objects are retrieved in the container when they are accessed. This greatly increases the speed at which the Texample is acquired (the classes configured for these three properties, which have been artificially sleep for a period of time as a delay in order to demonstrate the need)

Example 26 (Advanced function): Demonstrates how to register a class that is not tpersistent as a base class.

V. Other NOTES:

Elite container can support any version of Delphi 7, and currently only offers D7 version for trial use. In addition to release this demo, the main purpose is to be able to collect feedback in a timely manner, the container itself can not avoid some problems, such as memory leaks, lack of thread protection, etc., temporarily do not use in the actual project, otherwise the consequences at own risk.

A lightweight IOC object container under Elite Container Delphi

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.