Chapter 28. Visual Studio.NET Integration

28.1. XML Editing and Validation

(Available in 1.0)

Most of this section is well traveled 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 XML editor can use 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. In order to get this functionality in VS.NET 2002/2003 you 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, 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. For VS.NET 2003 the schema directory will be either

C:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Packages\schemas\xml for VS.NET 2003

or

C:\Program Files\Microsoft Visual Studio .NET\Common7\Packages\schemas\xml for VS.NET 2002

The VS.NET 2005 directory for XML schemas is

C:\Program Files\Microsoft Visual Studio 8\Xml\Schemas

As simple aid in this file copy task, you can use the NAnt build file located in the doc/schema directory that comes with Spring and execute

nant

The default nant target will copy the file spring-object.xsd from the doc/schema directory to the appropriate VS.NET 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 xsd file to use by setting the targetSchema property in the Property Sheet for the configuration file.

As shown in the section Section 5.7, “Interacting with the IObjectFactory” Spring.NET supports using .NET's application configuration file as the location to store the object defintions 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 2002/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 and addition 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 it is validating all the time while you edit, you will see any errors that it finds in the Error List window.

Keep these tradeoffs 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.

28.2. Versions of XML Schema

The schema was updated from Spring 1.0.1 to 1.0.2 in order to support generics. The schema for version 1.0.1 is located under http://www.springframework.net/xsd/1.0.1/ The schema for the latest version will always be located under http://www.springframework.net/xsd/

28.3. Integrated API help

As part of the installation process the API documentation for Spring.NET is registered with Visual Studio. There are two versions of the documentation, one for VS.NET 2002/2003 and the other for VS.NET 2005. They differ only in the format applied, VS.NET 2005 using the sexy new format. Enjoy!