PDA

View Full Version : Some assistance with .net web app spring integration



Amateur
01-03-2011, 06:18 PM
I am new to Spring.Net and currently implementing it into my .net web application. I have some questions that I hope someone can help me out with.

Firstly I have several .aspx pages that all inherit from a BasePage. Within the BasePage I have some properties that I want to inject values in to via spring. Now I am able to wire up the configuration as follows to achieve this:




<object id="identityManager"
type="XXX.IdentityCookieManager, XXXX.Cookies"
singleton="false">
<constructor-arg name="identity" ref="identity" />
</object>

<object name="basePage">
<property name="IdentityManager" ref="identityManager" />
</object>

<object type="Default.aspx" parent="basePage" />



My question is, do I have to specify every page in my application or is there a quicker way of doing such?

My next question is a little more complex. For each .aspx page, it has a controller object that carries out the operations for the page with the result passed to the page to display on screen. All page controllers inherit from a BaseController which again has some properties that I want to populate with spring.

Config for populating base controller is:




<object id="baseController">
<property name="ResourceManager" ref="controllerResources" />
<property name="IdentityManager" ref="identityManager" />
<property name="Logger" ref="logger" />
<property name="Configuration" ref="configuration" />
</object>



The controller for the page is set in the code behind of the page like this:




var views = new List<IView>() { this, this.MasterPage, this.ucControl };
this.Controller = new HomeController(views);



When the HomeController object is instansiated, the base controller properties are not set. Is there a way I can wire it up via spring that once an object that inherits from BaseController is instansiated, wire up the base controller properties?

I did look at assigning the Controller via spring config but it was a bit messy.

Any my last question has got to do with the global.asax. Is it possible to inject properties in to this that are available in Application_Start. At the moment I am injecting in to the global.asax but the properties are not populated for the Application_start method but are for subsequent methods, eg. Application_error




<object name="HttpApplicationConfigurer" type="Spring.Context.Support.HttpApplicationConfigurer, Spring.
<!-- Global.asax -->
<property name="ApplicationTemplate">
<object>
<property name="Logger" ref="logger" />
</object>
</objects>

Mark Pollack
01-03-2011, 07:47 PM
Hi,


My question is, do I have to specify every page in my application or is there a quicker way of doing such?

You will have to list an object definition in the Spring configuration file for all of your .aspx pages if you want them to be dependency injected.


When the HomeController object is instansiated, the base controller properties are not set. Is there a way I can wire it up via spring that once an object that inherits from BaseController is instansiated, wire up the base controller properties?

I did look at assigning the Controller via spring config but it was a bit messy.

Have you looked into 'object definition inheritance' (http://www.springframework.net/doc-latest/reference/html/objects.html#objects-childobjects)? It would look like this in your case



<object id="baseController" abstract=true>
<property name="ResourceManager" ref="controllerResources" />
<property name="IdentityManager" ref="identityManager" />
<property name="Logger" ref="logger" />
<property name="Configuration" ref="configuration" />
</object>

<object id="homeController" parent="baseController"
type="XXX.HomeController, XXX.Application>

<!-- configure additional properties here -->
</object>


Aside from the somewhat higher than normal "XML tax" for configuring a generic list in XML (described here (http://www.springframework.net/doc-latest/reference/html/objects.html#objects-generic-collections-values)), this isn't too messy.

If you want to take a dependency on the Spring container APIs you can configure an object after it has been instantiated.

To do this you need to get a reference to Spring's IApplicationContext. Implementing IApplicationContextAware is one way to get reference to the Spring application context if you page does not inherit from Spring's base page class or you can use the static utility method WebApplicationContext.Current. Spring's base page class inherits from IApplicationContext aware so you will have access to the ApplicationContext property.

The method to call (http://www.springframework.net/docs/1.3.0/api/net-2.0/html/topic5322.html)then is



object ConfigureObject( object target, string name)

where the first argument is the object you just 'newed' and the second is the name of the object definition you want to use to configure the rest of the properties, e.g. "baseController"


Any my last question has got to do with the global.asax. Is it possible to inject properties in to this that are available in Application_Start. At the moment I am injecting in to the global.asax but the properties are not populated for the Application_start method but are for subsequent methods, eg. Application_error

Off to the top of my head I don't remember exactly, but what you observe sounds reasonable given how I think about the container boostrapping in the web environment. I suggest trying WebApplicationContext.Current in the global.asax file and see if you can resolve that way. Let us know how that goes.

Mark

Amateur
01-03-2011, 11:15 PM
Thanks very much for your help Mark, question 1 & 2 were resolved with your suggestions.

No 3 regarding the availability of Spring populated properties in the Application start of global.asax wasnt successfull as the WebApplicationContext.Current was null. Any other ideas around this?