用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 @ 2017-09-21 18:05  code first life  阅读(362)  评论(0编辑  收藏  举报