tags:

views:

369

answers:

2

I'm a Java developer being forced to learn C#. Since I couldn't stand NOT using a Dependency Injection container, I'm trying to configure Spring.NET in my project.

My project is divided into two solutions in Visual Studio: a ClassLibrary solution for all my business logic, services, DAOs, etc, and a Webapp section (ASP.NET pages, etc).

I found it interesting that I could effectively inject a dependency into an actual page via its code-behind, which I couldn't really do with a JSP. However, what I'm wanting is to inject a service (UserService) from the ClassLibrary solution into the Login.aspx.cs, which is in the Webapp section.

Do I have to define the UserService object in both the App.Config on the ClassLibrary side AND on the Web.Config side?

A: 

No. You can import a Spring.Net configuration from the DLL project (defining the UserService) into the web project by using the 'assembly' prefix (works like 'classpath' prefix in Java). In your web project's spring config you can then reference all objects defined in the DLL.

Don't forget to mark your XML file containing the configuration (in the DLL project) as a resource to be included in the DLL.

Fried Hoeben
Okay, so let me see if I have this straight.In web.config, in the <compilatation> section, adding the reference to "MyApplication.ClassLibrary" using <add assembly=""> should pick up the config.xml resource file from the DLL project (as long as I marked it as a resource)?
Jason
See http://www.springframework.net/doc-latest/reference/html/web.html and 22.3.1. You can put your configuration inside assemblies. I always prefer self contained module configurations inside assemblies as they rarely (if ever) really change.
Marko Lahma
A: 

The app.config for the class library is not being used when you run an ASP.NET application. Only the web.config of the web application is taken into account. So in your web.config you could declare the dependency:

<configSections>
    <sectionGroup name="spring">
        <section name="context" 
                 type="Spring.Context.Support.ContextHandler, Spring.Core"/>
    </sectionGroup>
</configSections>
<spring>
    <context>
        <resource uri="~/Config/context.xml"/>
    </context>
</spring>

and in the context.xml:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"&gt;
    <!-- Define the UserService object -->
    <object id="userService" type="Namespace.UserService, ClassLibrary" />
</objects>

and finally in your code behind:

var userService = (UserService)ContextRegistry
    .GetContext()
    .GetObject("userService");
Darin Dimitrov
Calling GetObject is an anti-pattern for dependency injection. They pages should be declared in Web.config with their dependencies (properties with ref attributes). Spring.NET will handle the injection without the unwanted plumbing code.
Marko Lahma
See http://www.springframework.net/docs/1.3.0/reference/html/web.html#web-di for more information of using dependency injection in your ASP.Net pages
Fried Hoeben