Thursday, August 4, 2011

Spring.NET IoC

Web.config:

<spring>

   <parsers>

      <parser type="Spring.Data.Config.DatabaseNamespaceParser, Spring.Data" />

      <parser type="Spring.Transaction.Config.TxNamespaceParser, Spring.Data" />

      <parser type="Spring.Aop.Config.AopNamespaceParser, Spring.Aop" />

   </parsers>

   <context>

      <resource uri="~/Config/application.xml" />

   </context>

</spring>

 
application.xml:

<?xml version="1.0" encoding="utf-8" ?>

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

xmlns:db="http://www.springframework.net/database"

xmlns:tx="http://www.springframework.net/tx"

xmlns:aop="http://www.springframework.net/aop"

xmlns:v="http://www.springframework.net/validation">

   <!-- data -->

   <import resource="assembly://App.DataStorage/dao.xml" />

   <!-- controllers -->

   <import resource="~/Config/controllers.xml" />

   <!-- filters -->

   <import resource="~/Config/filters.xml" />

   <!-- aspects -->

   <import resource="~/Config/aspects.xml" />

   <!-- services -->

   <import resource="~/Config/services.xml" />

</objects>

 
Spring Beans are referred to as objects in Spring.NET. This one sits in dao.xml.

<object id="fooRepository"

type="App.DataStorage.FooRepository, App.DataStorage"

autowire="autodetect"

singleton="true">

   <property name="SessionFactory" ref="appSessionFactory" />

   </object>

 
It is referenced in controllers.xml like so.

<object id="fooController"

type="App.UI.FooController"

singleton="false" >


   <property name="FooRepository" ref="fooRepository" />


   <property name="BarRepository" ref="barRepository" />


   <property name="BazRepository" ref="bazRepository" />


</object>

 
One may "autopopulate" an object's parameters by a Service Locator means via Spring.NET.

<object id="foo" type="MyApp.Project.Bar, MyApp.Project" autowire="autodetect" singleton="true" >

   <property name="Baz" value="abc"/>

   <property name="Qux" value="123"/>

</object>

Bar() will have a parameterless constructor, yet use the values above for Baz and Qux. I'm not sure as of this writing now Service Locator makes the association. Is the call to the class above enough?

No comments:

Post a Comment