Nikhil Kothari的Script#

用服务器端语言写客户端脚本已经成为趋势了

RJS允许你使用Ruby来编写客户端的脚本,Google Web Toolkit使用Java,Nikhil Kothari的Script# 允许你使用C#来写客户端的脚本:

http://www.nikhilk.net/Entry.aspx?id=121

他的Script# 编译器生成的不是IL,而是Javascript, 这可以极大地简化开发AJAX类的应用。

这个10分钟的录像里,Nikhil Kothari演示了怎么使用Visual Studio 2005和Script#做一个简单的AJAX调用,Cool!

他有一个原型可以下载:

http://www.nikhilk.net/Content/Samples/ScriptSharp.zip

 

Ruby 和 .NET

除了上次提到的昆士兰大学在开发的Ruby.NET外,发现还有几个跟Ruby和.NET有关的项目

1。[来源:Rob Chartier] Wilco Bauwer刚推出他的IronRuby预览版

http://wilcoding.xs4all.nl/wilco/News/IronRubyPreview.aspx

他目前专注于CLR的集成,还没怎么移植Ruby的基本类库。这个预览版的内容包括对范型,接口,block/delegate,调试等的支持。

2。 John Lam的名叫RubyCLR的Ruby to CLR bridge

http://rubyforge.org/projects/rubyclr

从这个帖子里提到的支持来看,好像非常成熟,甚至支持ActiveRecord!

http://www.iunknown.com/articles/2006/03/16/third-drop-of-rubyclr

这个podcast是对RubyCLR作者的采访:

http://dnic.ca/blogs/dnic/archive/2006/05/16/37.aspx

3。 Steel是个Visual Studio 2005的Ruby语言的add-in,目前支持使用Ruby本身的解释器,句法加色,调试等
http://www.sapphiresteel.com/

另外,下面是上次提到的《Dobb博士杂志》2006年6月期的Ruby On Rails封面文章的连接

http://www.ddj.com/dept/architect/187203512;pgno=1

五月份DLINQ版本里的新功能

在五月份这个版本里,DLINQ提供了下面这些新功能

1。支持继承,目前只支持单表继承
2。对多层应用的支持
3。对远程和本地实体集合的支持
4。支持Join
5。对外部XML映射文件的支持
6。对存储过程以及自定义函数的支持
7。提供了一个DLinq Query Visualizer,在调试时查看实际SQL语句以及参数情况
8。新添加了一个设计器,用于设计类(表)间关系以及生成编码

这里还有一些细节,摘自文档《DLinq Overview for CSharp Developers.doc》第七章“New Features in May 2006 Preview”

DLINQ的新功能

在五月份这个版本里,DLINQ提供了下面这些新功能(详情参考《DLinq Overview for CSharp Developers.doc》第七章“New Features in May 2006 Preview”)

1。支持继承,目前只支持单表继承
2。对多层应用的支持,DataContext对象多了一个Attach()方法,可在不同层或不同请求中更新实体对象
3。对远程和本地实体集合的支持。实体集合实现了IQueryable<T> ,确保过滤性查询在数据库端完成
4。支持Join,这样你能做这样的查询

var q = from s in db.Suppliers
 join c in db.Customers on s.City equals c.City
 select new {
  Supplier = s.CompanyName,
  Customer = c.CompanyName,
  City = c.City
 };

5。对外部XML映射文件的支持

编码调用

String path = @”C:\Mapping\NorthwindMapping.xml”;
XmlMappingSource prodMapping =
 XmlMappingSource.FromXml(File.ReadAllText(path));
Northwind db = new Northwind(
 @”Server=.\SQLExpress;Database=c:\Northwind\Northwnd.mdf”,
 prodMapping
 );

映射文件

<Database Name=”Northwind”>
 <Table Name=”Products“>
  <Type Name=”Mapping.Product“>
   <Column Name=”ProductID” Member=”ProductID” Storage=”_ProductID”
    DbType=”Int NOT NULL IDENTITY” IsIdentity=”True”
    IsAutoGen=”True” />
   <Column Name=”ProductName” Member=”ProductName”
    Storage=”_ProductName” DbType=”NVarChar(40) NOT NULL” />
   <Column Name=”SupplierID” Member=”SupplierID” Storage=”_SupplierID”
    DbType=”Int” />
….. 
   <Association Name=”FK_Order_Details_Products” Member=”OrderDetails”
    Storage=”_OrderDetails” ThisKey=”ProductID”
    OtherTable=”Order Details” OtherKey=”ProductID” />
   <Association Name=”FK_Products_Categories” Member=”Category”
…..
  </Type>
 </Table>
 <Table Name=”Order Details”>
  <Type Name=”Mapping.OrderDetail”>
…..    
  </Type>
 </Table>

 …
</Database>

6。对存储过程以及自定义函数的支持

存储过程:

CREATE PROCEDURE GetCustomerOrderCount(@CustomerID nchar(5))
AS
Declare @count int
SELECT @count = COUNT(*) FROM ORDERS WHERE CustomerID = @CustomerID
RETURN @count

映射方法:

[StoredProcedure(Name=”GetCustomerOrderCount”)]
public int GetCustomerOrderCount(
 [Parameter(Name=”CustomerID”)] string customerID
 )
{
 StoredProcedureResult result = this.ExecuteStoredProcedure(
  ((MethodInfo)(MethodInfo.GetCurrentMethod())), customerID);

 return result.ReturnValue.Value;
}

7。提供了一个DLinq Query Visualizer,在调试时查看实际SQL语句以及参数情形

8。新添加了一个设计器,用于设计类(表)间关系以及生成编码

在项目里添加一个DLinqObjects模块,然后把Northwind数据库里的2个类Customers和Orders两个表先后拖到DLinq设计器上,设计器会根据数据库里的关系自动建立关联,同时生成下列编码

namespace LINQConsoleApplication1 { 
   
    public partial class NorthwindDataContext : System.Data.DLinq.DataContext {
       
        public System.Data.DLinq.Table<Customer> Customers;
        public System.Data.DLinq.Table<Order> Orders;   
…..
   
    [System.Data.DLinq.Table(Name=”Customers”)]
    public partial class Customer : System.Data.DLinq.INotifyPropertyChanging,

System.ComponentModel.INotifyPropertyChanged {
        private string _CustomerID;    
…..  
        private System.Data.DLinq.EntitySet<Order> _Orders;
       
…..     
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.Data.DLinq.Association(Name=”FK_Orders_Customers”, Storage=”_Orders”, OtherKey=”CustomerID”)]
        public virtual System.Data.DLinq.EntitySet<Order> Orders {
            get {
                return this._Orders;
            }
            set {
                this._Orders.Assign(value);
            }
        }
…..
    }
   
    [System.Data.DLinq.Table(Name=”Orders”)]
    public partial class Order : System.Data.DLinq.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged

{
       
        private int _OrderID;
…..
        private System.Data.DLinq.EntityRef<Customer> _Customer;
…..
        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.Data.DLinq.Association(Name=”FK_Orders_Customers”, Storage=”_Customer”, ThisKey=”CustomerID”,

IsParent=true)]
        public Customer Customer {
            get {
                return this._Customer.Entity;
            }
            set {
                if ((this._Customer.Entity != value)) {
                    this.OnPropertyChanging(“Customer”);
                    if ((this._Customer.Entity != null)) {
                        this._Customer.Entity = null;
                        this._Customer.Entity.Orders.Remove(this);
                    }
                    this._Customer.Entity = value;
                    if ((value != null)) {
                        value.Orders.Add(this);
                    }
                    this.OnPropertyChanged(“Customer”);
                }
            }
        }
…..
    }

假设你有类似这样的数据表Person,当Type为2时表明是Employee

字段名 数据类型 允许空
Type int True
ID(主键) int False
FirstName nvarchar(200) False
LastName nvarchar(200) False
Manager int True

拖动2份Person到设计器,把第二份改名为Employee

把Manager属性从Person类删去,从Employee类,保持Manager属性,删去其他的属性,从工具箱里选择“继承”连接,然后依次选择Employee类,Person类,建立继承关系

设置DiscriminatorColumn为Type,把Employee的DiscriminatorValue设置为2,把Person的DiscriminatorValue设置为1,保存后,会生成下列编码

[System.Data.DLinq.InheritanceMapping(Code=1, Type=typeof(Person), IsDefault=true)]
[System.Data.DLinq.InheritanceMapping(Code=2, Type=typeof(Employee))]
[System.Data.DLinq.Table(Name=”Person”)]
public partial class Person : System.Data.DLinq.INotifyPropertyChanging, System.ComponentModel.INotifyPropertyChanged {  

…..
    }
   
    public partial class Employee : Person {
       
        private System.Nullable<int> _Manager;
…..
    }
}

.NET 点滴

1。Scott Guthrie的博客开始连载怎么在ASP.NET中使用LINQ

Using LINQ with ASP.NET (Part 1)
http://weblogs.asp.net/scottgu/archive/2006/05/14/446412.aspx

第一部分演示了怎么使用五月份LINQ技术预览版提供的LINQ ASP.NET Web Site Template模板(注意:其中使用的C#编译器是3.0),然后对C# 3.0中的新构造器,LINQ的各种Query操作,以及匿名类型做了示范。Scott将在以后演示如何使用DLINQ。

2。从.NET 1.1移植到.NET 2.0的常见问题解答

.NET 1.1 to .NET 2.0 Migration
http://blogs.msdn.com/peterlau/archive/2006/05/11/595294.aspx

3。[来源:frankarr ] 微软推出 Expression Web Designer 的预览版

http://www.microsoft.com/products/expression/en/web_designer/default.mspx

4。[来源:Bertrand Le Roy] 调试XmlHttp的酷工具

Tracing XmlHttp requests: an unobtrusive solution
http://weblogs.asp.net/bleroy/archive/2006/05/15/446532.aspx

5。 推荐Irena Kennedy的博客,她的<<开发人员须知系列>>提供了很多有用的技巧和编码

AppDev: Something You Should Know by Irena Kennedy
http://blogs.msdn.com/irenak/default.aspx

Ruby和Rails

最新一期的《Dobb博士杂志》(2006年6月)的封面文章的标题为“Ruby On Rails–Java’s Successor?”。该文对Ruby On Rails做了个综述性的介绍,同时还对Rails框架的作者David Heinemeier Hansson做了采访。

本月,业界著名人士Martin Fowler在他的bliki上发表了“EvaluatingRuby”的专文,也对Ruby做了推荐,

http://www.martinfowler.com/bliki/EvaluatingRuby.html

“…It’s still early days yet, but I now have a handful of project experiences to draw on. So far the results are firmly in favor of Ruby. When I ask the question “do you think you’re significantly more productive in Ruby rather than Java/C#”, each time I’ve got a strong ‘yes’. (虽然为期还早,但我现在能从ThoughtWorkers公司的多个项目的经验中获取一些结论。到目前为止,结果是看好Ruby。当我向参与Ruby项目的有关人员询问下面这个问题,“你是否认为,你使用 Ruby,比之于Java或C#,生产效率高出很多?”,每次的答复都是很坚定的,“是!”)

…In all these cases, those involved said they are getting functionality, and value, faster out of the door than they had in other platforms. This suggests to me that if you’re looking for delivery speed and productivity you should take a serious look at Ruby. (在涉及的项目个案中,参与人员都说,与其他平台相比,他们推出功能和价值方面都比较快。在我看来,如果你的目的是交付速度和生产力,那么你应该认真地看一下Ruby。)

…But overall these experiences, from trusted colleagues mean I’m increasingly positive about using Ruby for serious work where speed, responsiveness, and productivity are important. (总的来说,我非常信任的同事的这些体验意味着,在那些注重速度,响应性以及生产力的严肃的工作中使用Ruby,我的看法是日趋肯定。)”

与此同时,我们尊敬的Don Box近来也写了不少针对Ruby的博客,涉及DSL,Block,与C#比较等内容。 在其中一文中,他说,

http://pluralsight.com/blogs/dbox/archive/2006/04/27/22819.aspx

“…From where I sit, Ruby has the language thought leadership position and is the competitor I hope AndersH is losing the most sleep over nowadays. (从我的角度来看,Ruby处于语言的思想领袖的地位,应该是让AndersH这些日子辗转难眠的竞争对手,我希望如此。)…”

ADO.NET 3.0

微软推出了下一代数据访问技术的前景,这个技术将随着代号为Orcas的下一个.NET框架版本一起推出。主要技术是ADO.NET Entity Framework,由实体数据模型(Entity Data Model/EDM)以及一套设计时和运行时的服务组成,允许开发人员使用与业务应用相关的抽象来描述和操作数据,从而与底层的具体数据存储隔离。

这个实体的数据模型通过设计时的映射连接到具体的数据存储,然后开发人员可以通过一个Mapping Provider在这个实体的数据模型的层次做操作,而由Mapping Provider与具体的数据存储打交道,做需要的映射/转换等等。具体来说,开发人员通过eSQL(Entity SQL,基于SQL的扩展)来对实体的数据模型(EDM)做查询。

在这个上面,ADO.NET Entity Framework包括了一个对象服务,可以从EDM schema生成相应的.NET类,这些类是partial类,开发人员可以在另外的文件里添加跟业务有关的逻辑。这个 Entity Framework负责跟踪其中对象的状态变化,包括实体集合的成员变动,在SaveChanges时生成具体的SQL等。

最后,ADO.NET Entity Framework利用了LINQ技术,使开发人员不需要跟包含在字符串里的SQL语句打交道,而是直接与Entity Framework生成的实体类和集合打交道。

这个技术也包括了对普通以及强类DataSet做LINQ操作的支持。

说白了,ADO.NET Entity Framework就是微软自己的基于LINQ的O/R M框架。。。翘首以待,希望不久就能玩到这框架!

具体的细节参考

Microsoft Data blog
http://blogs.msdn.com/data/archive/2006/05/10/594771.aspx

Next-Generation Data Access: Making the Conceptual Level Real
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/nexgenda.asp

ADO.NET Tech Preview: Overview
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/adonetv3.asp

Channel9上有个ADO.NET产品组讨论ADO.NET 3.0的采访录像
http://channel9.msdn.com/Showpost.aspx?postid=191667

还有一个相关的screencast[来源:ADO.NET mapping screencast],用具体的编码演示ADO.NET 3.0的映射概念,以及怎么映射复杂类,继承等

http://datajunkies.net/screencasts/mapping1/mapping1.html

ASP.NET 2.0中ReadOnly的TextBox

[来源:AppDev-SYSK 118] 有时候,我们不希望用户直接编辑TextBox,而是希望通过客户端脚本的方式来设置内容,一般的做法是设置TextBox的属性ReadOnly为true。但在ASP.NET 2.0里有了变化,设置了ReadOnly为true的TextBox,在服务器端不能通过Text属性获取在客户端设置的新内容,在Reflector里比较一下LoadPostData的实现

.NET 1.1中,

bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
      string text1 = this.Text;
      string text2 = postCollection[postDataKey];
      if (!text1.Equals(text2))
      {
            this.Text = text2;
            return true;
      }
      return false;
}

.NET 2.0中,

protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
      base.ValidateEvent(postDataKey);
      string text1 = this.Text;
      string text2 = postCollection[postDataKey];
      if (!this.ReadOnly && !text1.Equals(text2, StringComparison.Ordinal))
      {
            this.Text = text2;
            return true;
      }
      return false;
}

就可以看出,如果设置了ReadOnly为true,从客户端传回的新的值是不被设置到Text属性的。

想要保持.NET 1.*中的行为,建议的做法是设置客户端属性ContentEditable=false,参考

SYSK 118: ReadOnly or ContentEditable?
http://blogs.msdn.com/irenak/archive/2006/05/03/589085.aspx

其实如果是设置客户端属性的话,设置客户端的readonly属性应该也是可以的:

TextBox1.Attributes[“readonly”] = “true”;

VS 2005 Web Application Project V1.0发布了

[来源:Scott Guthrie]

Visual Studio 2005 Web Application Projects
http://msdn.microsoft.com/asp.net/reference/infrastructure/wap/default.aspx

VS 2005 目前支持的Web应用 template 叫网站项目模型(Web Site Project Model)。Web应用项目模型(Web Application Project Model)与网站项目模型间的区别,可以参考蝈蝈的博客或者MSDN上的文章。

VS2005将支持的两种WEB编程模型的比较(蝈蝈)
http://blog.joycode.com/ghj/archive/2006/04/17/74699.aspx

Introduction to Web Application Projects
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/WAP.asp

简而言之,Web应用项目模型是VS 2003中的web项目模型的翻版,具体来说,

  • 项目中的文件是通过一个项目文件来定义的,该项目文件也包括了程序集的引用以及其他与项目有关的信息。在项目所在文件夹里的文件,如果在项目文件中没有定义的话,并不属于这个项目
  • 编译时,所有的编码文件会被集中编译,生成一个单一的程序集,存放在bin目录里,同时IDE支持递增发布
  • 编译过程是通过MSBuild实现的,通过MSBuild的扩展规则,你可以扩展和定制编译过程

最大的好处在于,从VS 2003升级到VS 2005应该是极其容易的,具体的细节,参考Scott Guthrie的教程。

VB: http://webproject.scottgu.com/VisualBasic/Migration/Migration.aspx

C#: http://webproject.scottgu.com/CSharp/Migration/Migration.aspx

同时,VS 2005 Web Application Project的新功能包括

  • 支持VSTS的Team Build
  • 对App_GlobalResources 的Resource强类支持
  • 对定制Build Tool Action的支持
  • 对编辑/继续的支持,Scott Guthrie博客上有个示范

由于自定义Profile是动态生成的,Tim McBride发布了一个能在VS 2005 Web Application Project中编译时自动生成自定义Profile类的工具,具体参考

http://weblogs.asp.net/bradleyb/archive/2006/05/08/445727.aspx