Replacement for Spring?

Jan 20, 2009 at 6:34 PM
Hello,

I'm looking into replacements for Spring.NET.  I've come across the unity application block, and it looks like it will do what I need, however I'm having trouble configuring it as desired. 

I am working on a multi-tier application, with a database tier, a service tier, and an application tier.  I've been using Spring to link up these tiers, i.e., inject a reference to the database tier into the service tier, and inject a reference to the service tier into the application tier.  The configuration to do this is entirely within XML config files.  I understand that this is not the case for unity, but that's ok. 

Here's the unity config I've been working on:

<

 

unity>
    <
containers>
        <
container>
            <types>
                <
type name="MyService" type="ServiceTier.MyService,ServiceTier">
                    <
typeConfig>
                        <
property name="RefToDatabase" propertyType="DatabaseTier.MyDatabase,DatabaseTier">
                    
</typeConfig>
                </
type>
            </
types>
            <instances>
                <
add name="MyServiceInstance" 
                    
type="ServiceTier.MyService,ServiceTier"
                    
value="new"
                    
typeConverter="ServiceTier.MyService,ServiceTier"/>
            </
instances>
        
</container>
    </
containers>
</
unity>

The type specified is a service tier object - it has a property within it that is a reference to a database tier object.  When I create a service tier object, I want the reference to be populated with a new instance of the database tier object.

I've added a service tier instance as well - not sure if I need this, and made it a type converter that returns a new instance of itself (the "new" value is ignored - it always returns a new instance). 

The code I'm running to test all of this is as follows:

 

UnityConfigurationSection

 

section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

 

section.Containers.Default.Configure(container);

 

var myService= container.Resolve<MyService>("MyServiceInstance");

 

When I execute this code, I get my service tier object, but the RefToDatabase property within it is null.  No errors are reported.

I'm hoping that my complete unfamiliarity with unity means the solution is simple!

Thanks in advance.

Jan 20, 2009 at 6:51 PM

As expected, the solution was incredibly simple.  I didn't realize that I could Resolve types, as opposed to instances.  So, the solution is to replace

var myService= container.Resolve<MyService>("MyServiceInstance");

with

var myService= container.Resolve<MyService>("MyService");
 
The RefToDatabase variable is now populated.

I will now continue to look into replacing Spring!

Jan 21, 2009 at 2:50 AM
Yes, you could both resolve (and register) types and instances.  Your typeconverter might be just instantiating a new MyService object and not populating the RefToDatabase property.  Anyway, resolving the type would be simpler rather than using instance with the typeconverter if your typeconverter simply instantiates the object.


Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com