Chapter 40. Visual Studio.NET Integration

40.1. XML Editing and Validation

Most of this section is well travelled territory for those familiar with editing XML files in their favorite XML editor. The XML configuration data that defines the objects that Spring will manage for you are validated against the Spring.NET XML Schema at runtime. The location of the XML configuration data to create an IApplicationContext can be any of the resource locations supported by Spring's IResource abstraction. (See Section 7.1, “Introduction” for more information.) To create an IApplicationContext using a "standalone" XML configuration file the custom configuration section in the standard .NET application configuration would read:

<spring>

  <context>
    <resource uri="file://objects.xml"/>
  </context>

</spring>

The VS.NET 2005 or later, the XML editor uses the attribute xsi:schemaLocation as a hint to associate the physical location of a schema file with the XML document being edited. VS.NET 2002/2003 do not recognize the xsi:schemaLocation element. If you reference the Spring.NET XML schema as shown below, you can get intellisense and validation support while editing a Spring configuration file in VS.NET 2005/2008/2010. In order to get this functionality in VS.NET 2002/2003 you will need to register the schema with VS.NET or include the schema as part of your application project.

<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.net http://www.springframework.net/xsd/spring-objects.xsd">
  <object id="..." type="...">
  ...
  </object>
  <object id="..." type="...">
  ...
  </object>
  ...
</objects>

It is typically more convenient to install the schema in VS.NET, even for VS.NET 2005/2008/2010, as it makes the xml a little less verbose and you don't need to keep copying the XSD file for each project you create. The following table lists the schema directories for each version of VS.NET:

Table 40.1. 
Visual Studio Version Directory in which to place Spring .XSD files 
VS.NET 2003C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml
VS.NET 2005C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas
VS.NET 2008C:\Program Files\Microsoft Visual Studio 9.0\Xml\Schemas
VS.NET 2010C:\Program Files\Microsoft Visual Studio 10.0\Xml\Schemas

Spring's .xsd schemas are located in the directory doc/schema. In that directory is also a NAnt build file to help copy over the .xsd files to the appropriate VS.NET locations. To execute this script simply type 'nant' in the doc/schema directory.

Once you have registered the schema with VS.NET you can adding only the namespace declaration to the objects element,

<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.net">
  <object id="..." type="...">
  ...
  </object>
  <object id="..." type="...">
  ...
  </object>
  ...
</objects>

Once registered, the namespace declaration alone is sufficient to get intellisense and validation of the configuration file from within VS.NET. Alternatively, you can select the .xsd file to use by setting the targetSchema property in the Property Sheet for the configuration file.

As shown in the section Section 5.2.3, “Using the container” Spring.NET supports using .NET's application configuration file as the location to store the object definitions that will be managed by the object factory.

<configuration>

  <configSections>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
  </configSections>

  <spring>

    <context>
      <resource uri="config://spring/objects"/>
    </context>

    <objects xmlns="http://www.springframework.net">
        ...
    </objects>

  </spring>

</configuration>
    

In this case VS.NET 2003 will still provide you with intellisense help but you will not be able to fully validate the document as the entire schema for App.config is not known. To be able to validate this document one would need to install the .NET Configuration File schema and an additional schema that incorporates the <spring> and <context> section in addition to the <objects> would need to be created.

Validating schema is a new feature in VS 2005 or later. It is validating all the time while you edit, you will see any errors that it finds in the Error List window.

Keep these trade offs in mind as you decide where to place the bulk of your configuration information. Conventional wisdom is do quick prototyping with App.config and use another IResource location, file or embedded assembly resource, for serious development.

40.2. Enhancing the XML Editing and Validation Experience using the Spring.NET Visual Studio 2010 Extension

If you are using VS.NET 2010, you are encouraged to install the Spring.NET Visual Studio 2010 Extension. For more information and to download the latest version of this 100% free tool, visit http://springframework.net/vsaddin/.

The latest release of the Spring.NET Visual Studio 2010 Extension provides Intellisensetm support in VS.NET 2010 for the following areas of editing Spring XML configuration files:

  • Type completion

  • Property name completion

  • Constructor argument name completion

  • Property value completion for property of type 'Type', 'Enum' and 'Boolean'

In addition, this tool also provides for the following enhancements to the Visual Studio 2010 XML Editor experience:

  • Snippets integration (inline or by menu)

  • Quickinfo tooltip for properties and types

A brief screencast demonstrating the use of this tool can be viewed here: http://maruxelo.free.fr/spring/index2.html

40.3. Solution Templates

Solution templates for VS.NET 2008 are provided to get you up and running quickly with a Spring.NET based application or library. Four templates are provided and there are plans for more. All the templates aside from the web template have been created using SolutionFactory VS.NET Add-in. The source to creating the templates is not included in the distribution now, so please download the source from the subversion repository if you are interested in making modifications.

To install the templates

  1. Add the registry key [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\MyAssemblies] and set the value to be the directory <spring.net-install-directory\bin\net\2.0

  2. In the directory <spring.net-install-directory>\dev-support\vs.net-2008 run the batch file install-templates.bat

In VS.NET 2008 when you create a new project you will see the category Spring.NET and the four solution templates as shown below

All of the templates have the required Spring dependencies set and Spring application configuration files are present and ready for you to add object definitions.

40.3.1. Class Library

The simplest of the solution templates is the Spring Class Library. This creates a solution with two class library projects, one for you application classes that will be managed by Spring and another testing project. The projects have starter files to write XML based object definitions and also refer to Spring.NET .dlls as needed. The testing project refers to Spring.Testing.NUnit which provides integration testing support. A screen shot of the generated Class Library solution is shown below.

40.3.2. ADO.NET based application library

This solution template provides a service layer project, ADO.NET based data access layer and an unit/integration testing project.

40.3.3. NHibernate based application library

This solution template provides a service layer project, NHibernate based data access layer and an unit/integration testing project.

40.3.4. Spring based web application

This solution template provides a Spring based web layer project, service layer project, ADO.NET based data access layer project and an unit/integration testing project. You will need to set the reference of the App.Web project to refer to the App.Web.References project manually.

40.4. Resharper Type Completion

Resharper supports intellisence completion for the value of the type attribute when editing Spring's XML files. The key combination is Shift+Alt+Space. This is shown below for the case of specifying the type of a DAO object in the NHibernate sample application

You start to type the name of the class and will get a filter list. In this case we are typing HibernateOrderDao.

Hittingn 'enter' will then insert the fully qualfied type name with the namespace but not the assembly reference. To add the assembly reference either hit 'CTRL+ENTER" or select the yellow 'light bulb' to and select 'add module qualification'.

You will need to remove the extraneous 'Verstion' information. This will leave you with the following object definition.

If you use Spring's autowiring functionality, then you can even avoid having to type the property information when referring to collaborating objects. See Section 5.3.6, “Autowiring collaborators”. for more information on autowiring.

40.5. Resharper templates

Resharper offers live templates for assistance while coding as well as file templates. Spring 1.3 provides a few of each type to help you be more efficient when performing common configuration related tasks. To install the templates follow the directions in the 'dev-support' directory. One installed the following templates are available

For example, to set a property reference for the object definition from the previous chapter, type 'odpr' (Object Definition Property Reference) and you will be prompted to hit 'tab' to complete the XML fragment.

Hitting tab will generate the XML to use for an object property values

You will need to type the name of the property and name of the reference. Unfortunately, intellisence for property completion and ref completion is not available. Typing the missing information in then leaves the completed object definition.

There are similar live templates for object property values (odpv), object constructors (odctor) and object definitions (odef)

40.6. Versions of XML Schema

The latest version of the schema will always be located under http://www.springframework.net/xsd/ The filename of the .xsd files contains the first Spring.NET version to which they apply.

40.7. API documentation

Spring provides API documentation that can be integrated within Visual Studio. There are two versions of the documentation, one for .NET 1.1 and one for .NET 2.0 and later. They differ only in the format applied and the versions of VS.NET that supported. There is also standalone HTMLHELP format API documentation. You will need to download the help file seperately from the distribution.