Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Author Advanced .NET Applications in Perl : Page 5

In ''PerlNET: An Introduction,'' we introduced you to the new PerlNET technology that allows you to create .NET-compliant applications and components with Perl. In this article we'll show you how to author advanced .NET applications in Perl. We'll use ADO.NET classes to incorporate database access into PerlNET programs, then we'll explain and demonstrate working in connected and disconnected modes. PerlASPX adds Perl to the family of ASP.NET languages and makes it possible to develop ASP.NET Web applications in Perl. We'll explain how to create ASP.NET Web Forms and ASP.NET Web services.


advertisement
Code-Behind File for Shippers Web Form
The corresponding code-behind file (Listings 8-10) defines the ShippersPage class that is the base class for our Web Form. This class defines the business logic for the application. It accesses the data layer and queries or updates the Shippers table from the Northwind database. Additionally, this class includes the data binding code. Another role that we assigned to the ShippersPage class is events handling. Our sample demonstrates working with the Command event.

We divided the code of the code-behind file into three listings to help you navigate through it.

Listing 8 presents the main declarations of the ShippersPage class. We define the interface of the class (lines 11-21) specifying that we inherit from the Page class (line 12). Additionally, we implement the overridden OnLoad method where we invoke the BindControls method (line 27) that binds our Repeater control to the data from the Shippers table. Listing 9 shows the data binding code.

The BindControls method sets the DataSource property of the Repeater control to the DataTable object that is returned by CreateDataSource (lines 5-6). In the next line we call the DataBind method of the Repeater to bring the data into the container (line 7).

The CreateDataSource method performs simple data access in disconnected mode, fills the DataSet, and returns the DataTable object with the shippers' data.

Finally, Listing 10 shows the Command event handler—the RemoveShipper method.

To remove the shipper from the database we construct DELETE SQL statement bases on the CommandArgument property of the CommandEventArgs class that holds an id of a shipper to delete. We work in the connected mode and invoke the ExecuteNonQuery method (line 13). Some shippers may be referenced by the other tables (like Orders) and they cannot be deleted. We handle the exception with the eval block (lines 12-14). In case removing the shipper wasn't successful, we catch an exception and display the corresponding message in the lblMsg label (lines 15-19).

Now that we've demonstrated the power of PerlASPX in creating ASP.NET Web Forms, we'll show you how this wonderful product may help you in mastering ASP.NET Web services.

ASP.NET Web Services
Web services enable two different applications to integrate with each other using XML and SOAP (Simple Object Access Protocol). This integration becomes language and platform agnostic by nature. .NET enables you to write Web services just adding a WebMethod CLR Attribute that exposes that method as a Web service. In a quick example, we'll see how to write a Web services using PerlNET.

The above program exposes the Spell assembly that we've created in the last article. As in any other .NET language, it becomes easy to write Web services in PerlNET just by adding the [System.web.Services.WebMethod] tag above the method that we choose to expose as a Web service.

When a browser hits the PerlSpell.asmx page (see Listing 11), ASP.NET generates an information page on the Service (see Figure 2). When a user gives an integer value and presses the Invoke Button, the application calls the Web service Method spellNumber and the result is returned in an XML format.

The XML formatted output for the number 2242, the PerlSpell.asmx returns,

<?xml version="1.0" encoding="utf-8" ?> <string xmlns="http://tempuri.org/">two thousand two hundred forty two</string>

Web Service Client
Consuming a Web service uses the .NET wsdl.exe tool that, given the URL of the Web service, creates a proxy class in a specified language. The current version of WSDL generates the proxy classes only in VB, Jscript, or C#. In order to consume the Spell Web service we have to generate the proxy class, say in C#, then compile it to produce a library assembly. First, we'll generate the proxy class as

wsdl.exe /language:cs /out:SpellService.cs http://localhost/codemag/webservices/perlspell.asmx?wsdl

Next we'll compile the resulting SpellService.CS into a DLL and create SpellService.dll



csc /target:library SpellService.cs

The following code shows a PerlNET Console program that consumes the Spell Web service.

Compiling the code in Listing 12 along with reference option value for SpellService.Dll produces the CspellClient.exe. The following code snippet shows a sample result.

cspellclient.exe 234 ========== 234 spelled as two hundred thirty four

Once we have the proxy class in a .NET assembly it becomes pretty easy to develop any type of program to consume the Web service. The code in Listing 13 shows a Web page consuming the Spell Web service.


Yevgeny Menaker is a developer, author, and Linux consultant with more than 5 years of experience. For the past three years, Yevgeny has focused on developing new and advanced Internet technologies and Internet/COM/ATL applications. He is working on migrating to .NET technology. He is the lead author of "Programming PERL in the .NET Environment" (Prentice-Hall). Additionally, he worked with Object Innovations as a Linux consultant. .
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.