Design time configuration for an abstract type

May 2, 2011 at 4:07 PM

Hello,

I am migrating from Spring.Net to Unity 2.0 and I am having trouble with some of my configurations.

In the Spring configuration I was able to specify abstract="true" for my parent object and then parent="MyAbstractObject" for my child object.
When I would resolve the child object all properties would be injected for both parent and child. 

I am trying to duplicate this scenario in Unity without success.

public abstract class Base
{
    public ISomeService SomeService { get; set; }
}
public class Derived : Base
{     
}

<register name="Base" type="Web.UI.Base, Web.UI" mapTo="Web.UI.Base, Web.UI">
<property name="SomeService" dependencyName="someService"/>
</register>

<register name="Derived" type="Web.UI.Base, Web.UI" mapTo="Web.UI.Derived, Web.UI"> </register>

When I do:

Container.BuildUp(typeof(Derived), d, "Derived");

I would like to get SomeService injected, but I can not get this to work.
Am I doing anything wrong here or is it not possible?

Any help is greatly appreciated!

Regards
John

May 5, 2011 at 7:02 PM

I believe what are you trying to achieve is the same with this post - http://unity.codeplex.com/discussions/241494.

HTH.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 15, 2011 at 6:18 PM

Hello Gino,

Thank you very much for your answer!

I have tried the technique from the post you suggested but I Can't get SomeService to be resolved.
I have tried several approaches but I can't seem to get the Container to understand that SomeService is configured in Base type and that Derived inherits Base an therefore SomeService should be injected in Derived when I BuildUp an instance of that type.

I think that this should work so I am thinking that I am doing something wrong here...

Any help is greatly appreciated!

Regards
John 

May 19, 2011 at 4:06 AM

Hello John,

I'm just wondering if you're doing the same approach as the sample below and if this is the same scenario you're trying to achieve. Hope the sample would help too.

class Program
    {
        static void Main(string[] args)
        {

            IUnityContainer container = new UnityContainer();
            //Make sure that the following registration is registered in the container
            container.RegisterType<ISomeService, ImplementService>();

            // Used Property Injection to resolve the ISomeService 
            container.RegisterType<Base, Derived>(new InjectionProperty("SomeService"));
            var service = container.Resolve<Base>();
            
        }
    }
    
    public abstract class Base
    {
        public ISomeService SomeService { get; set; }
    }
    public class Derived : Base
    {
    }

    public interface ISomeService
    {
        void DoService();
    }

    public class ImplementService : ISomeService
    {

        #region ISomeService Members

        public void DoService()
        {
            Console.WriteLine("Implement Service Test...");
        }

        #endregion
    }

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 30, 2011 at 8:54 AM

Hello Gino,

I will try the code you posted and get back to you as soon as possible.

Thank you very much for your help!

 

Regards

John

May 30, 2011 at 2:01 PM

Hello Gino,

 

I have tried your example and it works.

The thing is that I would like to use declarative configuration, and wouldn't 

container.RegisterType<Base, Derived>(new InjectionProperty("SomeService"));

be the same as 

<register name="Derived" type="Web.UI.Base, Web.UI" mapTo="Web.UI.Derived, Web.UI">
    <property name="SomeService" dependencyName="someService"/>
</register>

In e.g. Spring.Net you can inherit configuration and that is what I would like to do, in order to avoid specifying the same properties in the declaration for each inherited type.

Regards

John

May 31, 2011 at 5:10 AM

Yes, you can also do this in the design time configuation. Converting the previous sample I've sent, it should be as follows;

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">    
    <container>
      <register type="PropertyInj.ConsoleApp.ISomeService,PropertyInj.ConsoleApp" mapTo="PropertyInj.ConsoleApp.ImplementService, PropertyInj.ConsoleApp" />
      <register type="PropertyInj.ConsoleApp.Base, PropertyInj.ConsoleApp" mapTo="PropertyInj.ConsoleApp.Derived, PropertyInj.ConsoleApp">
        <property name="SomeService" dependencyType="PropertyInj.ConsoleApp.ISomeService,PropertyInj.ConsoleApp" />
      </register>
    </container>
  </unity>
</configuration>

Application Code

        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();       
            container.LoadConfiguration();
                      
            var service = container.Resolve<Base>();
            service.SomeService.DoService();
            Console.ReadLine();
        }

Is this exactly what are you pertaining? Hope this helps.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

May 31, 2011 at 1:04 PM

Hello Gino,

 

The thing is that this would mean that if I had two classes that would derive from base I would end up with the following configuration:

<register name="Derived1" type="UnityTest.Base, UnityTest" mapTo="UnityTest.Derived1, UnityTest">
	<property name="SomeService">
		<dependency name="SomeService"/>
	</property>
</register>

<register name="Derived2" type="UnityTest.Base, UnityTest" mapTo="UnityTest.Derived2, UnityTest">
	<property name="SomeService">
		<dependency name="SomeService"/>
	</property>
</register>

But I would like to have it like this:

<register type="UnityTest.Base, UnityTest.Inheritance">
	<property name="SomeService">
		<dependency name="SomeService"/>
	</property>
</register>
			
<register name="Derived1" type="UnityTest.Base, UnityTest" mapTo="UnityTest.Derived1, UnityTest" />
<register name="Derived2" type="UnityTest.Base, UnityTest" mapTo="UnityTest.Derived2, UnityTest" />

This is what I would like to do:

http://www.springframework.net/doc-latest/reference/html/objects.html#objects-childobjects

Thank you for your patience

Regards

John

May 31, 2011 at 1:36 PM

Hello John,

Let me get back to you on this one. As far as I know the injection happens in the derived class which inherits from the base and not in the actual base class. I just need to check if your scenario is feasible and find any approaches how to make this work.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

 

Jun 2, 2011 at 12:20 PM

Hello John,

I'm afraid I still haven't got any luck on this yet. With this I would think the only approach you can take for now is to configure your property injection one by one in the config. Also, one thing I can suggest is to log this in the Issue Tracker as feature request and vote for it. HTH.

Gino Terrado
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com