Registration of IWhatever<T> -> Implementation where Whatever<T> base class exists

Apr 16, 2014 at 10:25 AM

I'm just some existing container code to use Unity 3 convention based mapping, but I can't figure out how to map a specific use case.

My current (working) mapping code is:
            // TODO: Work out how to map TTemplateDataTransformer -> DataTransformer<T> -> IDataTransformer<T>
            container.RegisterType<IDataTransformer<IndexTemplateViewData>, IndexTemplateDataTransformer>();
            container.RegisterType<IDataTransformer<IndexViewData>, IndexDataTransformer>();
            container.RegisterType<IDataTransformer<ActiveTaskViewData>, ActiveTaskDataTransformer>();
            container.RegisterType<IDataTransformer<CompletedTaskViewData>, CompletedTaskDataTransformer>();
            container.RegisterType<IDataTransformer<ExtractionRuleViewData>, ExtractionRuleDataTransformer>();
            container.RegisterType<IDataTransformer<FieldDefinitionViewData>, FieldDefinitionDataTransformer>();
All of the concrete implementations use a base class DataTransformer<T> which implements IDataTransformer<T>.

I have tried things like:
                AllClasses.FromAssembliesInBasePath().Where(t => typeof(IDataTransformer<>).IsAssignableFrom(t)),
                t => t.FullName);

                AllClasses.FromAssembliesInBasePath().Where(t => typeof(DataTransformer<>).IsAssignableFrom(t)),
                t => t.FullName);
but I keep getting an exception regarding not have a constructor on IDataTransformer'. Everything else works fine. Any ideas?


Mike Taylor
Apr 22, 2014 at 3:06 PM
Anything? Is this a clear enough example? Also, how would I resolve all interfaces with interface is: IWhatever<T, U> ?
Apr 23, 2014 at 2:13 PM
So you want to use Unity 3 registration by convention to map an IDataTransformer<T> to a DataTransformer using the rule for T:

[Prefix]ViewData => [Prefix]DataTransformer

So IDataTransformer<[Prefix]ViewData> will be resolved to [Prefix]DataTransformer.

If that is the case then you will need to create custom logic to perform that mapping. You already have the type selector you will have to replace the call to WithMappings.FromAllInterfaces with custom logic to perform the above mapping.

Randy Levy
Enterprise Library support engineer
Support How-to