摘要:一。 在Jasper中,针对数据定义的具体情形,一般有三种方案:
1.数据库中的数据定义与应用中的对象模型有一一对应的关系,那么不用做任何改动,直接调用默认的API即可
2.如果数据库中的数据定义与应用中的对象模型结构类同,但名称有所不同,则可以通过Jasper的命名服务(NameService)来做映射
譬如,我想把我以前的例子中的User->Yonghu,Post->Tiezi, UserName->Zhanghao,我可以这么做
>>> import clr>>> import System>>> from System import *>>>>>> clr.AddReference("Microsoft.Jasper.CTP")>>> from Microsoft.Jasper import *
要使用NameService,需要引进这个程序集:
>>> clr.AddReference("System.Data.Entity.Design.CTP")>>> from System.Data.Entity.Design import *
生成一个命名服务对象,在其中的PropertyNameMap和ClassNameMap添加映射,如果需要的话,还可以添加成员变量,方法名,以及参数名的映射
>>> nameService = NameService()>>> nameService.BaseNameService = NameService.Default>>> nameService.PropertyNameMap.AddNameMapping("UserName", "Zhanghao")>>> nameService.ClassNameMap.AddNameMapping("Users", "Yonghus")>>> nameService.ClassNameMap.AddNameMapping("Posts", "Tiezis")>>>>>> connStr = "Provider='System.Data.SqlClient';Provider Connection String='Initial Catalog=Jasper;Data Source=.\SQLExpress;Integrated Security=True;';Generate Default EDM=True"
将命名服务对象作为CreateDynamicContext的第二个参数
>>> ctx = DynamicContext.CreateDynamicContext(connStr,nameService)>>>
Users不存在了,已经被换掉了
>>> u = ctx.Users[0]Traceback (most recent call last):File , line 0, in <string>##45File , line 0, in _stub_##46AttributeError: couldn't find member Users
变成了Yonghus和Tiezis:
>>> dir(ctx)['AcceptAllChanges', 'AddObject', 'Attach', 'AttachTo', 'CodeRepresentation', 'Connection', 'ContextSaved', 'ContextSaving', 'CreateDynamicContext', 'CreateKey', 'CreateQuery', 'DefaultContainerName', 'DelAttrMethod', 'DeleteObject', 'Detach', 'Dispose', 'Equals', 'ExecuteDirectQuery', 'ExecuteFunction', 'ExecuteNativeMethod',......[
阅读全文]