用xpo实现dc技术的关键点-XPO是如何处理接口类型与真实类型的对应关系的

https://www.devexpress.com/Support/Center/Question/Details/Q487000/xpodatamodel-and-model-interfaces

代码来自于上面的网址,这个代码功能是xpo解析类型信息时,可以有个中转.

这也成了xaf中dc机制的一个关键点,即,接口信息需要有一个真实的对应类型信息才行.

也说是说,接口类型只是一个公开给程序员的信息,ta必须对应一个真实的classinfo,这个classinfo用于xpo解析真实的表\字段\表达式等其他元数据时使用.

复制代码
 public class InterfaceAsForcedAliasHelper {
            public InterfaceAsForcedAliasHelper() { }
            public InterfaceAsForcedAliasHelper(ReflectionDictionary dictionary)
                : this() {
                Help(dictionary);
            }
            public void Help(ReflectionDictionary dictionary) {
                dictionary.CanGetClassInfoByTypeHandler += new EventHandler<CanGetClassInfoByTypeEventArgs>(canGet);
                dictionary.ResolveClassInfoByTypeHandler += new EventHandler<ResolveClassInfoByTypeEventArgs>(resolve);
            }
            Dictionary<Type, Type> map = new Dictionary<Type, Type>();
            public void Map(Type interfaceType, Type actualClassInfoType) {
                map.Add(interfaceType, actualClassInfoType);
            }
            void canGet(object sender, CanGetClassInfoByTypeEventArgs e) {
                if(e.CanGetClassInfo.HasValue)
                    return;
                if(map.ContainsKey(e.ClassType))
                    e.CanGetClassInfo = true;
            }
            void resolve(object sender, ResolveClassInfoByTypeEventArgs e) {
                if(e.ClassInfo != null)
                    return;
                Type mapType;
                if(map.TryGetValue(e.ClassType, out mapType)) {
                    if(mapType != null && mapType != e.ClassType) {
                        e.ClassInfo = e.Dictionary.QueryClassInfo(mapType);
                    }
                }
            }
        }
复制代码
            ReflectionDictionary myDictionary = new ReflectionDictionary();
            InterfaceAsForcedAliasHelper helper = new InterfaceAsForcedAliasHelper(myDictionary);
            helper.Map(typeof(IInterfaceAsForcedAliasTestPerson), typeof(InterfaceAsForcedAliasTestPerson));
            IDataLayer dl = new SimpleDataLayer(myDictionary, new InMemoryDataStore());

 

posted @   code first life  阅读(364)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端
点击右上角即可分享
微信分享提示