How to Use the coldspring framework to manage ColdFusion

Source: Internet
Author: User
Tags java spring framework

Adobe ColdFusion is already popular in the development community. Its rapid development is due to a feedback loop: more powerful tools and frameworks enable application developers to become better, more powerful tools and frameworks can be developed by better developers. A core component of this kind of continuous success is the coldspring framework. I will introduce coldspring to you in this article, explain what it is, why you should use it, and provide some examples of its functions.

What is coldspring?

Coldspring is a dependency injection framework developed for ColdFusion by Chris Scott and Dave Ross (with the help of leaders from other communities. Coldspring is basically based on the Java Spring framework and is used to manage dependencies in the application domain model. This kind of capability is also called inversion of control, but it does not seem intuitive to me, so I insist on using dependency injection.

In ColdFusion, the object model is implemented using the ColdFusion component (CFC. Therefore, in actual use, coldspring provides a way to avoid manual management of the relationship between CFC. Of course, coldspring does not only manage dependencies, but it is its main responsibility.

Why use coldspring?

Good object-oriented design practices require you to use synthesis more than inheritance. Development in this way usually means that your CFC usually carries one (or more) other CFC pointers as instance variables. This provides great flexibility for your design, but one drawback is that you have to manage all these dependencies.

Let's imagine a domain model with two layers of functionality: a service layer and a database abstraction layer. For example,UserserviceMay depend on oneUserdaoAnd oneUsergatewayTo interact with the database. In turn, yourUserdaoAndUsergatewayThe two CFC can rely on configuration CFC to provide information about the name and secret of the data source, while the utility CFC (utility CFC) can provide some general functions, for example, adjust all values in the structure.

In this article, you can not only manage dependencies between these components, they must also be created according to a specific structure. Both the CFC utility and the CFC configuration must be created first and then providedUserdaoAndUsergateway. Finally,UserdaoAndUsergatewayMust be providedUserservicePreviously created.

Dependency Injection capability

Now let's take a look at how coldspring can help manage CFC dependencies. You can define dependencies in multiple ways, but the most common method is to use an XML file. It is very easy to initialize coldspring with code. (ListA)

List

<Cfsetservicedefinitionlocation = expandpath ('services. xml')/>
<Cfsetcoldspring = Createobject ('component', 'coldspring. Beans. defaultxmlbeanfactory '). INIT ()/>
<Cfsetcoldspring. loadbeansfromxmlfile (servicedefinitionlocation)/>

Once this step is completed, coldspring can be used. To create a CFC for coldspring, you only need to callGetbean ()Method, and pass the name of the required component as follows :(ListB)

List B

<Cfset test = coldspring. getbean ('test')/>

I am going to show you an example of coldspring XML, And Then browse it for a while to discuss what coldspring will do (ListC).

<? Xmlversion = "1.0" encoding = "UTF-8"?>

<Beans>

<Beanid = "user">
<Propertyname = "utility">
<Refbean = "utility"/>
</Property>
</Bean>

<Beanid = "utility"/>

<Beanid = "testfactory"/>

<Beanid = "test" factory-bean = "testfactory" factory-method = "getinstance">
<Constructor-argname = "Parameters">
<Map>
<Entrykey = "user">
<Refbean = "user"/>
</Entry>
</Map>
</Constructor-Arg>
</Bean>

</Beans>

The above XML uses a variety of different features. First, note that coldspring recognizes multiple components as "beans", which is mainly derived from the legacy of spring. Sometimes I want this name to be more relevant, because they may confuse users with what "Bean" is. However, if you only treat them as CFC, there will be no problem.

The first bean is calledUserCFC. It hasUtility. If you look back at this definition, you will see the second definition is the utility CFC. So we immediately concluded that coldspring will:

  • StartUtilityInstance
  • StartUserInstance
  • Since we told coldspring thatUserCFC depends onUtilitySo coldspring will automatically find a setter method that matches the name of the injected Bean (here isSetutility ()).
  • Finally, coldspringUserCallSetutility ()MethodUtilityInjectUserAndUtilityThe instance is passed as an independent variable. This is calledSetterInjection.

Therefore, coldspring not only solves the dependency problem for you, but also automatically solves their Sorting Problem. However, there are other ways to get the new CFC from coldspring. For details, see the definitions of the third and fourth beans below.

AboveListCThe third definition definesTestfactoryCFC. As you have guessed, This is a factory, that is, it is responsible for creating other CFC. You may wonder, "Why do we need a factory? Isn't coldspring already a factory ?"

Yes, coldspring is a factory, but it is just a simple factory. By allowing us to call a custom factory from coldspring, we can have greater control over the created CFC and make it more powerful. Just imagine a simple example: we wantContactmanagerCFC to allow the server to communicate with the system administrator. During working hours, it can create a CFC for email processing, but during off-duty hours, it will create a CFC for sending call messages or text messages. It is easy to add some additional logic to our factory so that different CFC can be instantiated based on different situations.

As you can see from the definition of the last bean, I am usingTestfactoryCFC, And let coldspring call itGetinstance ()Method. I also passed some data directly to the factory method:UserThe structure name parameter of the key and a previously createdUserCFC value.

You can use this method to call constructor injection (Constructor Injection), Instead of using the setter injection method. Both methods have their own advantages and disadvantages, but one advantage of setter injection is that you can avoid the issue of circular dependency. If cfc1 needs to use cfc2 as the constructor independent variable, but cfc2 also needs cfc1 as the constructor independent variable, then the issue of circular dependence will occur.

Back to this example, we now know what kind of response coldspring will make based on the bean we define. Now we can start using it. (ListD)

List D

<Cfset test = coldspring. getbean ('test')/>
<Cfoutput> date: # test. getdate () # </cfoutput>

All in allGetbeanResponse, coldspring will:

  • StartUtilityInstance
  • StartUserInstanceUtilityInjectUser. setutility ()Li
  • StartTestfactoryInstance
  • CallTestfactory. getinstance ()And pass a data structure as the independent variable, the latter will returnTestCFC.

Well, what should I do now?

All of the above are artificial examples to illustrate how to use some features of coldspring. However, you may say, "I can do this myself by manually creating objects !" Of course you can. If you do not deal with a large number of CFC, the benefits of frameworks such as coldspring will not be fully apparent.

In the above examples, it is not very complicated to manage several CFC tasks. Managing dependencies between 200 CFC is totally different. If you create a CFC in the wrong order, it will crash. If you forget to pass a CFC correctly, it will crash. Coldspring can do this for you. Finally, coldspring XML creates an Excellent graph for CFC dependencies. You can see all the dependencies in the XML file, rather than browsing many components to try to figure out their dependencies.

Most popular MVC frameworks for ColdFusion support coldspring through plug-ins or adapters. Many even support "autowiring", so when you define bean in XML, the plug-in will try to provide the framework component with the bean generated by coldspring by finding the setter that matches the bean name. Model-glue, Mach-II, and fusebox all provide this function.

Profile-oriented capability

In addition to the ability to rely on injection, coldspring also introduced the concept of cross-section programming (Aspect-Oriented Programming, AOP) to ColdFusion users. AOP is a powerful method to solve specific types of problems. A common use of AOP is logging. You may want to write a log file when some methods are executed. In the standard object-oriented programming (OO) method, you must add code to the method used to process log records.

AOP has changed this approach. You do not need to add the logging code in all places. Instead, you only need to define the logging code once, and then AOP intercepts the method calls in the entire model, and run the logging code when calling the method.

In practice, coldspring will create a wrapper CFC, expand the original CFC, and return the package component to you when calling this CFC. Your code does not even know that it is processing the package rather than the original components; coldspring manages these transparently. It creates a proxy for all the original CFC methods, but allows you to add additional code (such as logging code) before and after code execution ).

Barney boisvert has a good example of AOP code, which will automaticallyCftransactionBlock. This allows you to automatically add or delete Database Transaction Management for method calls! You can find this code in his blog.

The last example of the AOP capability isRemotefactorybean. You can use AOP to accept any model component and generate a remote fa C Ade component for it. This allows you to open some or all methods to remote sources, such as web services, Ajax, or flash remoting calls. You can even use coldspringFlashutilityserviceAutomatically convert CFC dataActionScriptObject.

Try it

I can only explain some of the main features of coldspring to you, but I hope you can know that it brings many powerful features to you. It is used for ColdFusion dependency injection and AOP implementation. It has been tested and has stable performance. You can download the coldspring sample code and read the complete documentation. There are also many links to the CVS resource library and a list of dedicated emails on this topic. You can try it on your own and then try it!

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.