0

I'm trying to implement constructor injection for a generic type repository interface. I do this by applying http://msdn.microsoft.com/en-us/library/ff660933(v=pandp.20).aspx documentation. The xml-editor however does not accept the square brackets in for instance:

    <alias alias="IGenericRepository"
       type="Design.DataModel.Interfaces.IGenericRepository'1    [[Design.DataModel.Entities.GaugeModel, Design.DataModel]], Design.DataModel"/>    

The xml-editor gives the following error: "Unexpected text". The full xml-section is:

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<sectionExtension prefix="" type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension, Microsoft.Practices.Unity.Interception.Configuration" />

<alias alias="IGenericRepository" type="Design.DataModel.Interfaces.IGenericRepository'1[[Design.DataModel.Entities.GaugeModel, Design.DataModel]], Design.DataModel"/>
<alias alias="GenericSqlRepository" type="Design.DataAccess.GenericSqlRepository'1[[Design.DataModel.Entities.GaugeModel, Design.DataModel]], Design.DataAccess"/>
<alias alias="IGaugeModelbaseService" type="Design.Contracts.IGaugeModelbaseService, Design.Contracts"/>
<alias alias="GaugeModelbaseService" type="Design.ContractImplememtations.GaugeModelbaseService, Design.ContractImplementations"/>
<alias alias="IContractMapper" type="Design.ContractImplementations.IContractMapper, Design.ContractImplementations"/>
<alias alias="ContractMapper" type="Design.ContractImplementations.ContractMapper"/>
 <container>
  <register type="IGaugeModelbaseService" mapTo="GaugeModelbaseService"> 
    <interceptor type="InterfaceInterceptor" />
  </register>
  <register type="IContractMapper" mapTo="ContractMapper"> 
    <lifetime type="hierarchical" />
  </register>
  <register type="IGenericRepository" mapTo="GenericSqlRepository">
    <lifetime type="hierarchical" />
  </register>
</container>  

Browsing the svc result however results in an error: "The type name or alias GaugeModelbaseService could not be resolved"

The xml must be equivalent to the folowing fluent API configuration (that works fine):

public class WcfServiceFactory : UnityServiceHostFactory
{
    #region Methods

    protected override void ConfigureContainer(IUnityContainer container)
    {
        //container.LoadConfiguration();
        container.AddNewExtension<EnterpriseLibraryCoreExtension>();
        container.AddNewExtension<Interception>();
        container.RegisterType<IGaugeModelbaseService, GaugeModelbaseService>(
            new InterceptionBehavior<PolicyInjectionBehavior>(), new Interceptor<TransparentProxyInterceptor>());
        container.RegisterType<IContractMapper, ContractMapper>(new HierarchicalLifetimeManager());
        container.RegisterType<IGenericRepository<GaugeModel>, GenericSqlRepository<GaugeModel>>(
            new HierarchicalLifetimeManager());
    }

    #endregion
}

Do I miss something in the xml-configuration? Please help.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • Duplicate of [another question](http://stackoverflow.com/questions/3486837/unity-2-0-registering-generic-types-via-xml). – Sebastian Weber Sep 30 '12 at 12:31
  • Partially yes. But this question gives no answer to me. Why does xml editor not accept the square brackets? Why doesn't it work? Do I miss something? – Frans Verhoeven Sep 30 '12 at 12:41
  • Read Chris Tavares' answer carefully. He creates an alias for the open generic type first and uses that alias to define an alias for the closed generic type. And ignore the xml editor's warning. It's a false negative. – Sebastian Weber Sep 30 '12 at 13:25

1 Answers1

0

Is this a direct copy/paste from your config file? If so, the problem is a typo:

<alias alias="GaugeModelbaseService" type="Design.ContractImplememtations.GaugeModelbaseService, Design.ContractImplementations"/>

Note: 'Implememtations'

Your syntax looks correct otherwise.

Chris Tavares
  • 29,165
  • 4
  • 46
  • 63