思归呓语

衣带渐宽终不悔,为伊消得人憔悴
随笔 - 413, 评论 - 2971, 引用 - 245

导航

关于

标签

每月存档

最新留言

广告

John Lam的动态Silverlight讲座

Mr.“IronRuby” John Lam在他的博客上登出了三篇贴子,其内容是他在MIX08大会上做的讲座的文字版
Microsoft Silverlight and Dynamic Languages
http://visitmix.com/blogs/2008Sessions/T28/

讲座录像的MP4版: http://msstudios.vo.llnwd.net/o21/mix08/08_MP4s/T28.mp4 (57.6MB)
讲座录像的WMV版: http://msstudios.vo.llnwd.net/o21/mix08/08_WMVs/T28.wmv (84.1MB)

演示代码可以在这里下载:
http://dynamicsilverlight.net/

在第一篇《Dynamic Silverlight Part 1: Hello, World! in Dynamic Silverlight》中,主要介绍随Silverlight 2 Beta 1一起发布的Chiron.exe命令行工具。这个工具有2大主要功能,一是可以帮你把应用的文件,资源,程序集等转换压制成.xap文件,二是提供一个简单的web服务器的功能,让你使用文件系统就能开发Silverlight应用。由于Chiron是动态生成.xap文件的,你对文件做变动后,只要刷新浏览器就可以获得新的.xap文件,不用重启Chiron或浏览器,提供了很好的交互开发体验。

在第二篇《Dynamic Silverlight Part 2: Managed JScript and flickr》中,示范了如何使用托管的JScript (与我们在浏览器中的客户端Javascript不同) 与Flickr API做交互,让用户查询相关主题的照片,并将照片显示在Silverlight中。还演示了在Silverlight中,原封不动地使用Peter Norvig(AI大师)用Python编写的Spelling Corrector(拼写纠错器),提示用户输入的主题可能有错。

在第三篇《Dynamic Silverlight Part 3: Integrating Silverlight with ASP.NET MVC 》中,示范了如何与ASP.NET MVC应用做集成。通过Chiron生成.xap文件,用IronRuby在客户端动态生成WPF元素,使用WebClient与服务器端做交互等。

posted on 2008-03-12 01:09:46 by saucer  评论(1) 阅读(5018)

MVC框架示范录像

在Scott Hanselman的这个博客贴子里,你能找到Scott Guthrie最近在ALT.NET大会上做的MVC框架示范的录像 http://www.hanselman.com/blog/ScottGuMVCPresentationAndScottHaScreencastFromALTNETConference.aspx

这里是这个录像的网址 (需要Silverlight):
http://www.hanselman.com/silverlight/ScottGuAtAltNetConf

如果你无法使用Silverlight,那么用这个直接的录像链接 (Scott Hanselman警告说,这些链接也许会有变动,所以最好通过他上面的博客贴子来访问这些链接):
http://download.microsoft.com/download/f/0/8/f0830f07-44db-4eea-ace3-8865856c8d65/ScottGuOnMVCatALTNET.wmv

同时,在Hanselman的贴子里,还有一个他做的MVC+IronPython示范(C# Model,IronPython Controller和View,以及一个IronRuby视图引擎!)的录像
http://www.hanselman.com/silverlight/ScottHaAtAltNetConf

http://download.microsoft.com/download/f/0/8/f0830f07-44db-4eea-ace3-8865856c8d65/ScottHaOnDLRandMVCatALTNET.wmv

posted on 2007-10-09 23:16:00 by saucer  评论(1) 阅读(5965)

Jasper中的定制

一。 在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>##45
File , line 0, in _stub_##46
AttributeError: 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', 'Finalize', 'GetAttribute', 'GetHashCode', 'GetObjectByKey', 'GetQuery', 'GetType', 'Hash', 'MemberwiseClone', 'MetadataWorkspace', 'ObjectStateManager', 'PythonToString', 'QueryTimeout', 'Reduce', 'ReferenceEquals', 'Refresh', 'SaveChanges', 'SetAttrMethod', 'Tiezis', 'ToString', 'TryGetObjectByKey', 'TryGetQuery', 'Yonghus', '__class__', '__delattr__', '__doc__', '__entityHelper', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '_tiezis', '_yonghus', 'add_ContextSaved', 'add_ContextSaving', 'get_Connection', 'get_DefaultContainerName', 'get_MetadataWorkspace', 'get_ObjectStateManager', 'get_QueryTimeout', 'get_Tiezis', 'get_Yonghus', 'remove_ContextSaved', 'remove_ContextSaving', 'set_DefaultContainerName', 'set_QueryTimeout']

UserName也变成了Zhanghao:

>>> u = ctx.Yonghus[0]
>>> dir(u)
['CodeRepresentation', 'DelAttrMethod', 'EmailAddress', 'EntityKey', 'Equals', 'Finalize', 'GetAttribute', 'GetHashCode', 'GetType', 'Hash', 'LastLogon', 'MemberwiseClone', 'Password', 'PythonToString', 'Reduce', 'ReferenceEquals', 'RelationshipManager', 'SetAttrMethod', 'SetChangeTracker', 'Tiezis', 'ToString', 'UserID', 'Zhanghao', '__class__', '__delattr__', '__doc__', '__entityHelper', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '_emailAddress', '_lastLogon', '_password', '_userID', '_zhanghao', 'get_EmailAddress', 'get_EntityKey', 'get_LastLogon', 'get_Password', 'get_RelationshipManager', 'get_Tiezis', 'get_UserID', 'get_Zhanghao', 'set_EmailAddress', 'set_EntityKey', 'set_LastLogon', 'set_Password', 'set_UserID', 'set_Zhanghao']


>>> u.Zhanghao
'abc'

同样的,Post成了Tiezi:

>>> p = ctx.Tiezis[0]
>>> dir(p)
['CodeRepresentation', 'CreatedDate', 'DelAttrMethod', 'EntityKey', 'Equals', 'Finalize', 'GetAttribute', 'GetHashCode', 'GetType', 'Hash', 'MemberwiseClone', 'Message', 'ModifiedDate', 'PostID', 'PythonToString', 'Reduce', 'ReferenceEquals', 'RelationshipManager', 'SetAttrMethod', 'SetChangeTracker', 'Title', 'ToString', 'Yonghu', '__class__', '__delattr__', '__doc__', '__entityHelper', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '_createdDate', '_message', '_modifiedDate', '_postID', '_title', 'get_CreatedDate', 'get_EntityKey', 'get_Message', 'get_ModifiedDate', 'get_PostID', 'get_RelationshipManager', 'get_Title','get_Yonghu', 'set_CreatedDate', 'set_EntityKey', 'set_Message', 'set_ModifiedDate', 'set_PostID', 'set_Title', 'set_Yonghu']


>>> p.Yonghu.Zhanghao
'abc'

3.如果结构不同的话,那么需要使用实体模型的中的CSDL,SSDL,和MSL来做映射,具体参考样例或文档

二。 除了结构性方面的定制,还能定制对象的行为么?

在Jasper的安装里,有一个VB.NET的Customization的例程,其中的App_Code中的BusinessLogic.vb:

Imports Microsoft.VisualBasic

'Project Jasper will create a Product class based on the Northwind schema
'At class creation time, Jasper will find this class and derive from it
'Jasper will find the SetUnitPrice function and call it from inside of
'the setter for the UnitPrice property on the Product class it creates


Public Class Product
 Public Function SetUnitPrice(ByVal value As Decimal) As Decimal
    If value < 0 Then
            Throw New Exception("Can't set UnitPrice to a negative value")
    Else
            Return value
    End If
 End Function
End Class

其中的注释说,

Project会根据Northwind数据库的数据定义创建一个Product类

在创建类时,Jasper会发现这个类(指上面BusinessLogic.vb里定义的这个Product类),然后从这个类继承

Jasper会发现SetUnitPrice函数,然后在它创建的Product类的UnitPrice属性的setter里调用这个函数

如果你用Reflector看一下,你会发现DynamicContext.SaveChanges()->DynamicContext.SetupEntitiesForChanges()时看实体对象是否拥有OnPersist()方法,如果有的话,就会调用这个方法

让我们来在IronPython里试验一下

>>> import clr
>>> import System
>>> from System import *

创建一个类,继承自System.Object,内含2个方法HelloWorld和SetCreatedDate:

>>> class Post(System.Object):
...           def HelloWorld(self):
...              return 'hello world'
...           def SetCreatedDate(self,dt):
...              if (dt < DateTime.Now):
...                   raise Exception("CreatedDate must be in the future")
...

导入所需类

>>> clr.AddReference("Microsoft.Jasper.CTP")
>>> from Microsoft.Jasper import *

设置连接字符串

>>> connStr = "Provider='System.Data.SqlClient';Provider Connection String='Initial Catalog=Jasper;Data Source=.\SQLExpress;Integrated Security=True;';Generate Default EDM=True"

创建动态上下文,生成实体类

>>> ctx = DynamicContext.CreateDynamicContext(connStr)

取出昨天生成的贴子的一个对象

>>> p = ctx.Posts[0]

看一下其中的方法

>>> for m in p.GetType().GetMethods(): print m.Name
...
set_PostID
set_Title
set_Message
set_CreatedDate
set_ModifiedDate
SetChangeTracker
get_EntityKey
set_EntityKey
get_RelationshipManager
get_PostID
get_Title
get_Message
get_CreatedDate
get_ModifiedDate
get_User
set_User
GetType
ToString
Equals
GetHashCode

好像没有HelloWorld和SetCreatedDate啊,看一下类型

>>> p.GetType().FullName
'dboModel.Post'

其基类

>>> p.GetType().BaseType.FullName
'System.Object'
>>>

没有继承啊,生成一个Post对象,看一下其类型

>>> p2 = Post()
>>> p2.GetType().FullName
'IronPython.NewTypes.System.Object_1'
>>>

类名居然不是Post,而是IronPython.NewTypes.System.Object_1! 看来Python里的类跟.NET里的类还是有区别的啊!

那就用C#写个类吧, dboModel.cs:

using System;
using System.Reflection;

namespace DomainModel
{
public class Post
{

//因为是静态的语言,无法获取还不存在的数据,在这里加一个成员变量保存数据为OnPersist()所用
DateTime _createdDate;

public DateTime SetCreatedDate(DateTime dt)
{
   Console.WriteLine("in memory:{0}, passed in:{1}", _createdDate, dt);

//这里有关校验,传入的时间必须是现在或将来,否则出错
   if (dt < DateTime.Now)
            throw new Exception("CreatedDate must be in the future");

   _createdDate = dt;

   return dt;
}

public void Test()
{
   Console.WriteLine("Test");
}

public void OnPersist()
{

//这是另一个校验,_createdDate不能是明天以后,应该在SetCreatedDate里做的,但。。。。
     if (_createdDate >= DateTime.Now.AddDays(1))
            throw new Exception("CreatedDate cannot go beyond tomorrow");
 }
}
}

编译成dboModel.dll

csc /t:library dboModel.cs

>>> import clr
>>> import System
>>> from System import *
>>>
>>> clr.AddReference("Microsoft.Jasper.CTP")
>>> from Microsoft.Jasper import *
>>>

导入dboModel.dll里的类

>>> clr.AddReference("dboModel.dll")
>>> import DomainModel.Post
>>>
>>> connStr = "Provider='System.Data.SqlClient';Provider Connection String='Initial Catalog=Jasper;Data Source=.\SQLExpress;Integrated Security=True;';Generate Default EDM=True"

>>> ctx = DynamicContext.CreateDynamicContext(connStr)

取出昨天生成的贴子的一个对象

>>> p = ctx.Posts[0]
>>> p.GetType().FullName
'dboModel.Post'

基类

>>> p.GetType().BaseType.FullName
'DomainModel.Post'

这回对了

>>> for m in p.GetType().GetMethods() : print m.Name
...
set_PostID
set_Title
set_Message
set_CreatedDate
SetCreatedDate
set_ModifiedDate
SetChangeTracker
get_EntityKey
set_EntityKey
get_RelationshipManager
get_PostID
get_Title
get_Message
get_CreatedDate
get_ModifiedDate
get_User
set_User
Test
OnPersist
GetType
ToString
Equals
GetHashCode

>>> p = ctx.Posts[0]
>>> p.CreatedDate = DateTime.Now.AddDays(-1)
in memory:1/1/0001 12:00:00 AM, passed in:5/1/2007 4:29:28 PM
Traceback (most recent call last):
File , line 0, in <stdin>##27
File , line 0, in set_CreatedDate##31
File dynamicAssembly1, line unknown, in set_CreatedDate
File Microsoft.Jasper.CTP, line unknown, in CallMethodIfExists
Exception: CreatedDate must be in the future
>>>

看来SetCreatedDate真被调用了

>>> p.CreatedDate = DateTime.Now.AddDays(2)
in memory:1/1/0001 12:00:00 AM, passed in:5/4/2007 4:30:20 PM

试着保存

>>> ctx.SaveChanges()
Traceback (most recent call last):
File dboModel, line unknown, in OnPersist
File , line 0, in <stdin>##33
File , line 0, in SaveChanges##34
File Microsoft.Jasper.CTP, line unknown, in SaveChanges
File Microsoft.Jasper.CTP, line unknown, in SetupEntitiesForChanges
StandardError: Exception has been thrown by the target of an invocation.

出错了,没显示具体错误信息,奇怪

继承法大致如此,虽然还有些细节不是很对,但就到此为止吧

三。但,IronPython是门动态语言,我一定要用继承才行么?我不能动态添加方法么?

譬如在Python里,你可以这么做

>>> class A:
...          def HelloWorld(self):
...                  self.af = "hello"
...          def Print(self):
...                  print self.af
...
>>> A
<class __main__.A at 0x000000000000002C>

>>> a = A()
>>> a.HelloWorld()
>>> a.Print()
hello

定义一个新方法,

>>> def HelloWorld2(self):
...             self.af = "hello 2"
...

使用setattr:

>>> setattr(A, HelloWorld2.__name__, HelloWorld2)

调用新的方法:

>>> a.HelloWorld2()
>>> a.Print()
hello 2

可以给.NET 类动态添加方法么?用上面那个C#写的Post类作为例子:

>>> from DomainModel import *
>>> def HelloWorld2(self):
...             self.af = "hello 2"
...
>>> Post
<type 'Post'>
>>>
>>> setattr(Post,HelloWorld2.__name__,HelloWorld2)
Traceback (most recent call last):
File , line 0, in <stdin>##23
File , line 0, in SetAttr##24
AttributeError: 'Post' object has no attribute 'HelloWorld2'

好像不行!

看来我的IronPython的道行还比较浅,请哪位对IronPython有研究的指点一下,谢谢!

posted on 2007-05-08 12:40:00 by saucer  评论(11) 阅读(6447)

使用Jasper和IronPython操作数据 - 补充说明

1.数据模型的Visio是这样的

2.至于实体模型的定义,可以在动态的上下文对象生成后,用反射来看一下

>>> ctx = DynamicContext.CreateDynamicContext(connStr)
>>> from System import *

>>> ut = Type.GetType("dboModel.User")

>>> for p in ut.GetProperties() : print "%s - %s" % (p.Name,p.PropertyType.FullName)
...
EntityKey - System.Data.EntityKey
RelationshipManager - System.Data.Objects.DataClasses.RelationshipManager
UserID - System.Int32
UserName - System.String
Password - System.String
EmailAddress - System.String
LastLogon - System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Posts - Microsoft.Jasper.Query

>>> pt = Type.GetType("dboModel.Post")

>>> for p in pt.GetProperties() : print "%s - %s" % (p.Name,p.PropertyType.FullName)
...
EntityKey - System.Data.EntityKey
RelationshipManager - System.Data.Objects.DataClasses.RelationshipManager
PostID - System.Int32
Title - System.String
Message - System.String
CreatedDate - System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
ModifiedDate - System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
User - dboModel.User

3. 对数据集合的操作

>>> for ps in ctx.Posts : print "%s - %s" % (ps.Title, ps.CreatedDate)
...
abc - 5/2/2007 9:46:29 AM
def - 5/2/2007 9:46:29 AM
>>> for ps in u.Posts : print "%s - %s" % (ps.Title, ps.CreatedDate)
...
abc - 5/2/2007 9:46:29 AM
def - 5/2/2007 9:46:29 AM
>>>

也可以这么做查询

>>> for ps in ctx.Posts.Select("it.Title,it.User.UserName"): print "%s - %s" % (ps.Title, ps.UserName)
...
abc - joel_cool
def - joel_cool

4. 要删除集合中的对象,应该在集合所在类的实例那里做

>>> u = ctx.Users.First()
>>> u.Posts.Count()
2
>>> p = u.Posts[0]
>>> u.Posts.Delete(p)
True
>>> ctx.Posts.Count()
1
>>> u.Posts.Count()
1
>>> ctx.SaveChanges()
2
>>> u.Posts.Count()
1
>>> ctx.Posts.Count()
1

posted on 2007-05-02 22:03:00 by saucer  评论(2) 阅读(5815)

使用Jasper和IronPython操作数据

1.建立数据库

use master
go

create database Jasper
go

use Jasper
go

CREATE TABLE Users (
UserID int identity NOT NULL,
UserName nvarchar(40) NOT NULL,
Password nvarchar(20) NOT NULL,
EmailAddress nvarchar(40) NOT NULL,
LastLogon datetime,
PRIMARY KEY (UserID)
)
go

CREATE TABLE Posts (
PostID int identity NOT NULL,
Title nvarchar(100) NOT NULL,
Message ntext,
CreatedDate datetime,
ModifiedDate datetime,
UserID int NOT NULL,
PRIMARY KEY (PostID),
FOREIGN KEY (UserID) References Users(UserID)
)
go

2. 运行ipy命令行工具

C:\>ipy
IronPython 1.1 (1.1) on .NET 2.0.50727.1318
Copyright (c) Microsoft Corporation. All rights reserved.

3. 装载所需库

>>> import clr
>>> clr.AddReference("Microsoft.Jasper.CTP")
>>> from Microsoft.Jasper import *

4. 设置连接字符串,注意我们使用了默认的机制来通过实体框架动态生成对象

>>> connStr = "Provider='System.Data.SqlClient';Provider Connection String='Initial Catalog=Jasper;Data Source=.\SQLExpress;Integrated Security=True;';Generate Default EDM=True"

5. 生成动态上下文对象

>>> ctx = DynamicContext.CreateDynamicContext(connStr)

6. 看一下ctx对象的成员

>>> dir(ctx)

['AcceptAllChanges', 'AddObject', 'Attach', 'AttachTo', 'Connection', 'ContextSaved', 'ContextSaving', 'CreateDynamicContext', 'CreateKey', 'CreateQuery', 'DefaultContainerName', 'DeleteObject', 'Detach', 'Dispose', 'Equals', 'ExecuteDirectQuery', 'ExecuteFunction', 'ExecuteNativeMethod', 'Finalize', 'GetHashCode', 'GetObjectByKey', 'GetQuery', 'GetType', 'MakeDynamicType', 'MemberwiseClone', 'MetadataWorkspace', 'ObjectStateManager', 'Posts', 'QueryTimeout', 'Reduce', 'ReferenceEquals', 'Refresh', 'SaveChanges', 'ToString', 'TryGetObjectByKey', 'TryGetQuery', 'Users', '__class__', '__doc__', '__entityHelper', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '_posts', '_users', 'add_ContextSaved', 'add_ContextSaving', 'remove_ContextSaved', 'remove_ContextSaving']

里面有Users和Posts

7. 目前数据库还没有任何数据

>>> ctx.Users.Count()
0
>>> ctx.Posts.Count()
0

8. 让我们来生成一个User对象

>>> u = ctx.Users.Create()
>>> u.UserName = "joel_cool"
>>> u.Password = "abc123"
>>> u.EmailAddress = "joe@cool.com"
>>> u.LastLogon = DateTime.Now
Traceback (most recent call last):
File , line 0, in <stdin>##42
NameError: name 'DateTime' not defined
>>> u.LastLogon = System.DateTime.Now
Traceback (most recent call last):
File , line 0, in <stdin>##43
NameError: name 'System' not defined
>>> from System import *
>>> u.LastLogon = DateTime.Now
>>> ctx.SaveChanges()
1
>>> ctx.Users.Count()
1
>>> u.UserID
1

忘了引进System命名空间

9. 生成几个贴子对象

>>> p = ctx.Posts.Create()
>>> p.User = u
>>> p.Title = "abc"
>>> p.Message = "Hello world"
>>> p.CreatedDate = p.ModifiedDate = DateTime.Now
>>>

>>> p2 = ctx.Posts.Create()
>>> p2.User = u
>>> p2.Title = "def"
>>> p2.Message = "Jasper is fun"
>>> p2.CreatedDate = p2.ModifiedDate = DateTime.Now
>>> ctx.SaveChanges()
4
>>> ctx.Posts.Count()
2
>>> p.PostID
1
>>> p2.PostID
2
>>> u.Posts.Count()
2

10. 查询一个贴子,然后做修改

>>> p3 = ctx.Posts.FindByKey(2)
>>> p3
<Post object at 0x000000000000002B>
>>> p2
<Post object at 0x000000000000002B>
>>>注意p2和p3指向同一个对象ID
>>> p3.Message += ", yes, really fun"
>>> p3.ModifiedDate = DateTime.Now
>>> ctx.SaveChanges()
1
>>> p2.Message
'Jasper is fun, yes, really fun'
>>> p2.CreatedDate
<System.DateTime object at 0x000000000000002C [5/1/2007 8:15:16 PM]>
>>> p2.ModifiedDate
<System.DateTime object at 0x000000000000002D [5/1/2007 8:17:18 PM]>

11. 删除其中一个贴子

>>> p4 = u.Posts.FindByKey(1)
>>> ctx.Posts.Delete(p4)
True
>>> ctx.SaveChanges()
2
>>> ctx.Posts.Count()
1
>>> u.Posts.Count()
2

#!!!好像有问题,用户对象还需要从自己的贴子集合里删除贴子,是特性还是缺陷? 看样子这里需要一个用法模式

>>> p4.User
Traceback (most recent call last):
File , line 0, in <stdin>##106
File , line 0, in get_User##107
File dynamicAssembly1, line unknown, in get_User
File Microsoft.Jasper.CTP, line unknown, in GetRelatedReferenceValue
File System.Data.Entity.CTP, line unknown, in Load
SystemError: Unable to load the EntityReference because it is not attached to a
context.

#但这里p4指向的User已经出问题了,p3呢?

>>> p3.User
<User object at 0x000000000000002E>
>>> p3.User.UserName
'joel_cool'

12.把用户也删除吧

>>> ctx.Users.Delete(u)
True
>>> ctx.SaveChanges()
Traceback (most recent call last):
File , line 0, in <stdin>##116
File , line 0, in SaveChanges##49
File Microsoft.Jasper.CTP, line unknown, in SaveChanges
File System.Data.Entity.CTP, line unknown, in SaveChanges
File System.Data.Entity.CTP, line unknown, in SaveChanges
File System.Data.Entity.CTP, line unknown, in Update
File System.Data.Entity.CTP, line unknown, in Update
File System.Data.Entity.CTP, line unknown, in ValidateConstraints
SystemError: A relationship is being added or deleted from relationship set 'FK__Posts__UserID__7F60ED59'. Given cardinality constraints, a corresponding 'Posts' must also be added or deleted.
>>>

外键约束的关系,无法删除用户,把贴子也全删了!

>>> ctx.Posts.DeleteAll()
>>> ctx.SaveChanges()
3
>>> ctx.Users.Count()
0
>>> ctx.Posts.Count()
0
>>> u.Posts.Count()
2

posted on 2007-05-02 08:38:00 by saucer  评论(15) 阅读(6506)

动态语言运行时

昨天发布的 SilverLight1.1 Alpha 除了包括跨平台的CLR和类库外,还包括了动态语言运行时(Dynamic Language Runtime -- DLR),目前支持的动态语言包括IronPython和JScript。这个IronPython版本,IronPython 2.0 Alpha 1,是重构过的,而这里的JScript是全新的JavaScript实现,与目前在CLR里的托管JScript语言不同。今年晚些时候,微软将推出对Ruby和VB的支持。

DLR是其中的关键,虽然CLR对动态语言来说是个不错的平台,IronPython-1.0的实现可以证明这一点,但DLR使它更棒。CLR提供了世界级的JIT和GC,沙箱式安全模型,调试/运行分析(profiling)接口等共享服务。DLR则建立在CLR之上,提供了一整套共享的语言服务,例如动态类型系统,快速动态分派,智慧代码生成,和一个宿主API等。这些特性能够使得所有的动态语言都可以通过DLR来与其他动态语言和现有平台上的静态语言(C#,VB.NET)自由地共享代码。

DLR让你感受到最好的语言体验,真正地体现了语言,出色的工具,性能,与大量的库和平台的无缝集成的体验。

DLR最主要的好处在于共享。

它让语言实现者共享标准的特性,而不是从零做起,让他们集中精力在使得给定语言独特的特性之上,而不是重新发明另一个GC系统。它让开发人员共享代码,不管该代码是用哪种语言实现的,使用任何一门他们喜欢的语言,而不管他们需要运行的环境的首选语言是什么。

 

【参阅】
John Lam的博客 - Introducing IronRuby
http://www.iunknown.com/2007/04/introducing_iro.html

Jim Hugunin的博客 - A Dynamic Language Runtime (DLR)
http://blogs.msdn.com/hugunin/archive/2007/04/30/a-dynamic-language-runtime-dlr.aspx

JonUdell采访John Lam的podcast
http://channel9.msdn.com/ShowPost.aspx?PostID=304541

IronPython v2.0 Alpha
http://www.codeplex.com/IronPython

posted on 2007-05-02 02:49:00 by saucer  评论(4) 阅读(5875)

微软的秘密项目“Simplified Data Scenarios”

星期二,微软CLR 动态语言开发组的架构师 David Ebbo 在正于拉斯维加斯举行的开发人员大会上展示了IronPython for ASP.NET, 同时也提到了一个叫“Simplified Data Scenarios”的秘密项目,旨在简化数据库驱动的ASP.NET 数据绑定应用的建造,目前可以使用IronPython,将来会支持Ruby,PowerShell,JScript等。

下面是eweek.com部分报道

....

Ebbo said his team has delivered a prototype of a "secret project" called Simplified Data Scenarios where developers can "easily build data-bound applications from scratch" starting only with a database.

The prototype enables users to customize applications declaratively using ASP.NET or programmatically using IronPython, Ebbo said.

Meanwhile, King said IronPython is "really just the first of a series of dynamic languages" Microsoft will support. "We're looking at Ruby, PowerShell, JScript" and others, he said.

Ebbo said the Simplified Data Scenarios framework will work with other dynamic languages as well. "We'll have it first on IronPython, but it will work with others," he said. ...

posted on 2006-11-10 14:29:00 by saucer  评论(5) 阅读(6600)

IronPython for ASP.NET

微软昨天推出了IronPython for ASP.NET 的CTP版本

由于目前的ASP.NET的扩展性模型采用的是CodeDOM模型,是针对静态编译的语言的。为解决这个问题,他们引入了一个新的扩展性模型,采用了ASP.NET的No-Compile特性,修改了System.Web.dll中的PageParserFilter API,通过几个基类(ScriptPage, ScriptUserControl, ScriptMaster)来参与页面的生命周期。这个新模型也为其他动态语言与ASP.NET协作指明了方向。新的模型好处多多,与静态语言比,除了页面初始处理快外,还提高了扩缩性,他们的加载测试标明,同样的硬件配置,一个拥有一万个静态编译的ASP.NET网页的一个网站已经不堪重负,却可以轻松处理一百万个动态编译的IronPython 网页!

想理解IronPython在ASP.NET下是如何工作的的话,读一下微软 .NET CLR 组架构师 David Ebbo 写的白皮书《ASP.NET之新动态语言扩展性模型(The New Dynamic Language Extensibility Model for ASP.NET)》,博客园的木野狐对该文做了中文摘要,值得一读。

可以在www.asp.net下载IronPython for ASP.NET:

Microsoft IronPython for ASP.NET CTP
http://www.microsoft.com/downloads/details.aspx?FamilyId=55932211-0D7E-4C6E-9B18-8F9D2AC1EE43&displaylang=en

相应的文档除了上面提到的白皮书外,还有涉及创建页面,创建用户控件,使用共享代码,数据绑定,以及如何调试等5个教程的文档:

IronPython for ASP.NET
http://www.asp.net/ironpython/

posted on 2006-11-04 02:30:00 by saucer  评论(4) 阅读(7553)

Powered by: Joycode.MVC引擎 0.5.2.0