Is it possible to restate these generic constraints in a cleaner way?
Assume following:
public class MyFunkyTable : DbObject
{
// this class will be generated
}
public class MyFunkyDomainObject : DomainObject
{
// this class will be custom-made
}
public class MyFunkyMapper : Mapper<MyFunkyTable, MyFunkyDomainObject>
{
// this will be custom mapping code due to wired abstraction and ... "supercool" db-system
}
in general we do following:
MappingHelper<MyFunkyTable, MyFunkyMapper, MyFunkyDomainObject>.GetSingle(...);
bu the repeating of the generic constraints is a bit an cumbersome (MyFunkyMapper
开发者_JS百科 already specifies the generics..)
Is there any way to do something like:
MappingHelper<MyFunkyMapper>.GetSingle(..);
edit:
I've already came up with an idea: usage of extension methods, but this isn't what I want...Why don't you just do something like
var item = mappers.Get<MyFunkyMapper>().GetSingle(...);
This assumes that Mapper<TTable, TDomain>
has a GetSingle<TDomain>(...)
method. If this is the case, type inference will figure out the generic argument to GetSingle even if you don't write it.
BTW, have you considered using AutoMapper for mapping purposes instead of rolling your own?
I think you can't. The compiler can infer type parameters by looking at the arguments of a function, but that's it. Since you do not pass any parameters, you won't be able to use type inference.
A shortcut I might suggest is a lesser-used functionality of the "using" keyword. Just add at the top of your file (next to the normal using's) this line:
using TheMapper = MappingHelper<MyFunkyTable, MyFunkyMapper, MyFunkyDomainObject>;
And then in code
TheMapper.GetSingle();
This shorthand can come in handy if a file accesses one or few mappers.
Another idea - instead of MappingHelper
, why not add all of those things in the Mapper<T1, T2>
class? Then you will be able to use
MyFunkyMapper.GetSingle();
精彩评论