Cant' map class/class instead of interface/class?

Sep 16, 2011 at 9:24 AM

As I've found I can't map same class to same class. For example if I have a type product and I want to map like Contaner.RegisterType<Product, Product>(), I get exception. Investigating the code I've found there's a checking TypeMapping class's constructor like shown below:

            if (typeTo.IsAbstract)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.NonAbstractType, typeTo.Name));
            }
            else if (!typeTo.IsSubclassOf(typeFrom) && typeTo.GetInterface(typeFrom.Name) == null)
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.ImplicitConversionNotDefined, typeTo.Name, typeFrom.Name));
            }

However, if I remove the checking, then register works and I can use class to class mapping. Since I'll use SPG in my production project I just want to make sure if I comment out the checking will it make any problem? Is there any specific reason to prohibit class to class mapping?

Thanks

Sohel

Coordinator
Sep 16, 2011 at 7:12 PM

just curious, what is the use case for doing this? 

I don't see an issue with doing this, but I'd say that rather than remove the checking, you'll want to add a check to see if it is the type to the else-if statement - that check is intended to make sure that the type that is constructed is valid to return for the interface/type requested.

Sep 17, 2011 at 4:09 AM

Hi,

The reason to use class-to-class mapping is that we don't want to extract interface for a class just right now (at least until we don't need interface for real). Maybe we need to use different implementations of the same class in future and only then we'll extract interface from a class and then use interface to class mapping. So class-to-class mapping should help in preventing class-explosion. I can put a branch in if-else to  check if source and target type mapping is same and if same then I can bypass the exception thrown in the code. But my concern is if there's any side effect? Is there any known restrictions/limitations/problems under the hood to map class-to-class in SPG?

 

Thanks in advance

Sohel