Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

.net - Unity configuration file class mapping with interfaces with generics

I'm having this issue and after googled and readed almost all the documentation about it and search for the same question here, I'm not able to solve it, I really need help.

So I've just made a short piece of code so any of you can reproduce easily:

using Microsoft.Practices.Unity.Configuration;
using Unity;

namespace ConsoleApp1
{
    public class Dog { public Dog() { } }
    public class Cat { public Cat() { } }
    public interface IVet<T1, T2>{}
    public interface IHospital<T> { }

    public class Veterinarian : IVet<Dog, Cat> { public Veterinarian() { } }

    public class VeterinaryHospital : IHospital<IVet<Dog, Cat>> { public VeterinaryHospital() { } }

    class Program
    {
        static void Main(string[] args)
        {
            UnityContainer Container;
            //New container
            Container = new UnityContainer();
            Container.AddExtension(new Diagnostic());
            //Loading configuration
            Container.LoadConfiguration();

            var myVet = Container.Resolve<IVet<Dog, Cat>>(); //Ok
            var myHospital = Container.Resolve<IHospital<IVet<Dog, Cat>>>(); //Throws resolve exception
        }
    }
}

And the app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration" />
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">    
    <alias alias="DogEntity" type="ConsoleApp1.Dog, ConsoleApp1" />
    <alias alias="CatEntity" type="ConsoleApp1.Cat, ConsoleApp1" />
    <alias alias="IHospital" type="ConsoleApp1.IHospital`1, ConsoleApp1" />
    <alias alias="IVet" type="ConsoleApp1.IVet`2, ConsoleApp1" />
    <alias alias="VeterinaryHospital" type="ConsoleApp1.VeterinaryHospital, ConsoleApp1" />
    <alias alias="Veterinarian" type="ConsoleApp1.Veterinarian, ConsoleApp1" />
    <containers>
      <container>
        
        <register type="IVet[DogEntity,CatEntity]" mapTo="Veterinarian">
          <lifetime type="ContainerControlledLifetimeManager"/>
        </register>
        <register type="IHospital[Veterinarian]" mapTo="VeterinaryHospital">
          <lifetime type="ContainerControlledLifetimeManager"/>
        </register>
        <!-- This option doesn't work either -->
        <!--<register type="IHospital[IVet[DogEntity,CatEntity]]" mapTo="VeterinaryHospital">
          <lifetime type="ContainerControlledLifetimeManager"/>
        </register>-->

      </container>
    </containers>
  </unity>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
</configuration>

So, the problem is that I'm not able to get my "Hospital" resolved, I'm getting always a

Unity.ResolutionFailedException: 'The current type, ConsoleApp1.IHospital`1[ConsoleApp1.IVet`2[ConsoleApp1.Dog,ConsoleApp1.Cat]], is an interface and cannot be constructed. Are you missing a type mapping?

Any suggestions please?

Thank you in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...