Generics 使用Lambda表达式添加模型映射

Generics 使用Lambda表达式添加模型映射,generics,lambda,ef-code-first,factory-pattern,Generics,Lambda,Ef Code First,Factory Pattern,我有一个映射界面,如下所示: public interface IMapping<T> where T: class, IEntityModel { IQueryable<U> Project<U>(IQueryable<T> set) where U : class, IModel; } public class UserMapping : IMapping<User> { .... code that contai

我有一个映射界面,如下所示:

public interface IMapping<T> where T: class, IEntityModel
{
    IQueryable<U> Project<U>(IQueryable<T> set) where U : class, IModel;
}
public class UserMapping : IMapping<User>
{
    .... code that contains mapping between IEntityModel and IModel
}
public static class MappingResolver
{
    private static Dictionary<Type, object> Mappings = new Dictionary<Type, object>(); 

    public static void RegisterMapping<T, U>(U UMapping) where T: class, IEntityModel where U : IMapping<T> 
    {
        Mappings.Add(typeof(T), UMapping);
    }

    public static IMapping<T> GetMapping<T>() where T : class, IEntityModel
    {
        return Mappings[typeof(T)] as IMapping<T>;
    }
}
MappingResolver.RegisterMapping<User, UserMapping>(new UserMapping());
MappingResolver.RegisterMapping((x,y) => {User, new UserMapping()});