I might be trying to “mis-use” Unity here, but what I’d like is for Unity to create a new class not based on an interface that the newe’d up class implements, but based on a different class.
This has to do with resolving a concrete DataMapper type based on the name of an domain object being passed into a method call in a DataContext object (in this case, it’s a concrete DataContext for Sql Server)
protected override IDataMapper<T> GetDataMapper<T>()
{
if (typeof(T) == typeof(Expert))
{
return (IDataMapper<T>)new ExpertDataMapper();
}
else if (typeof(T) == typeof(Case))
{
return (IDataMapper<T>)new CaseDataMapper();
}
else if (typeof(T) == typeof(CaseFile))
{
return (IDataMapper<T>)new CaseFileDataMapper();
}
etc... etc...
}
You get the point.
in this case, T will be the name of the domain object, NOT the name of the DataMapper that implements the IDataMapper interface… so a call like this:
IDataMapper<T> mapper = (IDataMapper<T>)container.Resolve(t);
fails.
The GetDataMapper call uses generics, and based on the name of a domain object (let’s say “Expert”), I want to instantiate an ExpertDataMapper object, and return that.
What I don’t want is this “dumb” factory approach, where it’s a huge list of case statements… especially when it comes time to add new domain objects and they’re related DataMappers. I’d rather handle that in the config section of my App.config file and NOT have to come in, change code, recompile, and then release to production.
Maybe Unity is the wrong tool for the job?
Thanks!
Mike
You can’t register
IDataMapper<Category>in configuration file because it is C# syntax but not a generic type’s name. Check rules from type names here.You should use something like (simplifyed Unity 2.0 config):