1

我有一个这样的映射接口:

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
}

现在我想在 global.asax application_start 中注册所有映射,并让我的基础存储库使用它们来投影以查看具有通用方法的模型。这就是我的映射解析器现在的工作方式:

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()});
4

0 回答 0