[Python]Python视频教程

mikel阅读(744)

第1讲 Python概述:http://rzchina.net/node/11792
第2讲 基本语法:http://rzchina.net/node/11793
第3讲 控制语句:http://rzchina.net/node/11794
第4讲 内置数据结构:http://rzchina.net/node/11795
第5讲 模块与函数:http://rzchina.net/node/11807
第6讲 字符串与正则表达式:http://rzchina.net/node/11796
第7讲 文件的处理:http://rzchina.net/node/11797
第8讲 面向对象编程:http://rzchina.net/node/11798
第9讲 异常处理与程序调试:http://rzchina.net/node/11799
第10讲 Python 数据库编程:http://rzchina.net/node/11800
第11讲 WxPython的开发环境:http://rzchina.net/node/11801
第12讲 WxPython框架初步:http://rzchina.net/node/11802
第13讲 基本组件:http://rzchina.net/node/11803
第14讲 菜单、窗口与对话框:http://rzchina.net/node/11804
第15讲 高级控件:http://rzchina.net/node/11805
第16讲 WxPython下的高级功能:http://rzchina.net/node/11806
第17讲 Python的HTML应用:http://rzchina.net/node/11808
第18讲 Python和XML:http://rzchina.net/node/11809
第19讲 Python的Web开发——Django框架的应用:http://rzchina.net/node/11810
第20讲 敏捷方法学在Python中的应用——测试驱动开发:http://rzchina.net/node/11811
第21讲 Python中进程和线程:http://rzchina.net/node/11812
第22讲 基于Python的系统管理:http://rzchina.net/node/11813
第23讲 Python和网络编程:http://rzchina.net/node/11814
第25讲 图像处理和游戏开发:http://rzchina.net/node/11815
第26讲 Python语言的扩展与嵌入:http://rzchina.net/node/11816
第27讲 Windows下的Python:http://rzchina.net/node/11817

[Flex]flex 与asp.net 配合之道

mikel阅读(735)

1.将flex编译后的程序插入到ASP.NET页面
flex的最终输出就是一张网页+一个flash(.swf文件)
就是用他生成的网页的方式把那个.swf文件插入ASP.NET页面就可以了。
flex3项目名字叫TestApp,最简单直接的办法就是,
把"bin-Debug"目录下的:
TestApp.html
TestApp.swf
AC_OETags.js
playerProductInstall.swf
这4个文件复制到asp.net网站下面,打开TestApp.html,把内容复制到asp.net程序页面(.aspx文件)中。
比如Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
//把TestApp.html的内容全部复制到这里
//….
//…
总而言之FLEX3最后编译成了一个.swf文件而已,这个文件在网站里面插入的方法和普通的flash动画的那种.swf文件的使用方法是一样的。
还有其他的要求:flex3程序和网页还有交互,请用"flex externalinterface"搜索
2.flex程序与asp.net程序交互
可以使用flex的Loader往asp.net发送请求,获取xml。
也可以使用ExternalInterface和网页中的js交互,让js发送ajax请求到asp.net。
下面有一实例,目标是:在flex端将数据Post到asp.net页面中,并将返回的xml数据显示出来

//Asp.net端代码
//getxml.aspx代码,保留一行即可,删除其他的html代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="getxml.aspx.cs" Inherits="getxml" %>

//getxml.aspx.cs
//using System…
using System.Xml;
public partial class getxml : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string user_pkid = System.Web.HttpContext.Current.Request.Form["user_pkid"];
        if user_pkid != null)
        {
             CreateXml();//创建Xml的方法,可使用XmlTextWriter、XmlDocument ,或者直接读取Xml文件等待
        }
    }

    private void CreateXml()
    {
        XmlDocument doc = new XmlDocument();
        XmlNode root = doc.CreateElement("channel");

        XmlElement titleElm = doc.CreateElement("title");
        titleElm.InnerText = "blogweather";

        //…
   
        root.AppendChild(titleElm);
        doc.AppendChild(root);

        XmlTextWriter xw = new XmlTextWriter(Response.OutputStream,System.Text.Encoding.UTF8);//写到页面返回值中
        xw.Formatting = Formatting.Indented;//将Xml格式化
        doc.Save(xw);
        xw.Flush();
        xw.Close();
    }
}

Xml数据如下:
<?xml version="1.0" encoding="UTF-8" ?>
<channel>
    <title>blogweather</title>
    <link>http://www.blogweather.net</link>
    <description>博客天气预报</description>
</channel>

方法一:
如果所有值均在xml数据中,而且不需要拿这些数据做二次分析,则推荐使用 HTTPService控件

Flex 端代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()" >
 <mx:Script>
  <![CDATA[
   import mx.messaging.AbstractConsumer;
   import flash.events.MouseEvent;
   import mx.controls.Alert;
      
   private function init():void
   {
    getxml.url = "http://www.blogweather.net/getxml.aspx"; //接收Post方法的页面
    var data:Object = new Object();
    data["user_pkid"] = this.parameters.user_pkid;
    getxml.send(data);
   }
            ]]>
 </mx:Script>
 <mx:HTTPService id="getxml" showBusyCursor="true" useProxy="false" method="POST">
 </mx:HTTPService>
 <mx:TextArea  wordWrap="true" editable="false" enabled="true" id="lb_title">
  <mx:text>{getxml.lastResult.channel.title}</mx:text>
 </mx:TextArea>
</mx:Application>

方法二:
如果要将数据进行分析,则要使用URLLoader和URLRequest
Flex 端代码:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init();">
 <mx:Script>
  <![CDATA[
   import mx.messaging.AbstractConsumer;
   import mx.messaging.channels.StreamingAMFChannel;
   import flash.events.MouseEvent;
   import mx.controls.Alert;
      
             public var myLoader:URLLoader = new URLLoader();
             public var myRequest:URLRequest;
             public var user_pkid:String;

   private function init():void
   {
    var http://www.cnblogs.com/glaivelee/admin/String = "http://www.blogweather.net/getxml.aspx";
    myRequest = new URLRequest(url);
    myRequest.method = URLRequestMethod.POST;
    var data:URLVariables = new URLVariables();
    //接收来自flash的参数调用,比如flash文件为 loadxml.swf,带参数 loadxml.swf?user_pkid=10001
    data.user_pkid = this.parameters.user_pkid; // 获取10001
    myRequest.data = data;
    myLoader.load(myRequest);
    myLoader.addEventListener(Event.COMPLETE,onLoadComplete);
   }
   
   private function onLoadComplete(event:Event):void
   {
    var myxml:XML;
    var loader:URLLoader = URLLoader(event.target);
    myxml = new XML(loader.data);
    
    lb_title.text =myxml.child("channel")[0].child("title");
    if( lb_title.text == "blogweather")
    {
     Alert("页面名称为:博客天气预报");
    }
   }
   
  ]]>
 </mx:Script>
 <mx:TextArea  wordWrap="true" editable="false" enabled="true" id="lb_title">
  <mx:text>lb_title</mx:text>
 </mx:TextArea>
</mx:Application>

[ORM]NHibernate之旅系列文章导航

mikel阅读(935)

宣传语

NHibernate、NHibernate教程、NHibernate入门、NHibernate下载、NHibernate教程中文版、 NHibernate实例、NHibernate2.0、NHibernate2.0教程、NHibernate之旅、NHibernate工具

导游

NHibernate是把Java的Hibernate核心部分移植到Microsoft .NET Framework上。它是一个对象关系映射工具,其目标是把.NET对象持久化到关系数据库。

NHibernate在2008年8月31日发布了NHibernate2.0版本,代表NHibernate又向前走了一步,我相信 NHibernate将会越来越强大。唯一的遗憾是现在NHibernate对泛型的支持有点不足,源于Java中的泛型是编译时“搽拭法” 泛型不是真正的泛型,如果NHibernate添加上自己独特的功能——泛型,那么更为强大了很多。

这个NHibernate之旅系列带你来到NHibernate的世界。一步一步看清NHibernate中的种种细节。

环境优先

这次NHibernate2.0系列之旅使用Microsoft Visual Studio 2008 SP1、SQL Server 2008 Express、NHibernate2.0最新环境,非常舒适。不过你可以到这里获得NHibernate最新版本, 这里获得NHibernate Contrib最新版本。

休息接待区

欢迎加入NHibernate中文社区!在讨论中寻找乐趣!在问题中寻找答案!

请转向:http://space.cnblogs.com/group/NHibernate!全程接待!期待你的NHibernate中文社区之旅!

旅途站点路线

第一站:鸟瞰NHibernate

NHibernate之旅(1):开篇有益

第二站:接触NHibernate

NHibernate之旅(2):第一个NHibernate程序

第三站:数据在我手中

NHibernate之旅(3):探索查询之NHibernate查询语言(HQL)

NHibernate之旅(4):探索查询之条件查询(Criteria Query)

NHibernate之旅(5):探索Insert, Update, Delete操作

第四站:控制你的全部

NHibernate之旅(6):探索NHibernate中的事务

NHibernate之旅(7):初探NHibernate中的并发控制

观光站:实用技巧补偿

NHibernate之旅(8):巧用组件之依赖对象

第五站:关系如此复杂

NHibernate之旅(9):探索父子关系(一对多关系)

NHibernate之旅(10):探索父子(一对多)关联查询

NHibernate之旅(11):探索多对多关系及其关联查询

第六站:我来加载你

NHibernate之旅(12):初探延迟加载机制

NHibernate之旅(13):初探立即加载机制

第七站:数据的镜子

NHibernate之旅(14):探索NHibernate中使用视图(new!)

NHibernate之旅(15):探索NHibernate中使用存储过程(上)(new!)

NHibernate之旅(16):探索NHibernate中使用存储过程(中)(new!)

NHibernate之旅(17):探索NHibernate中使用存储过程(下)(new!)

第八站:转载请注明

NHibernate之旅(18):初探代码生成工具使用(new!)

NHibernate之旅(19):初探SchemaExport工具使用(new!)

NHibernate之旅(20):再探SchemaExport工具使用(new!)

下一站:停靠在哪儿

NHibernate之旅(21):探索对象状态(new!)

NHibernate之旅(22):探索NHibernate一级缓存(new!)

NHibernate之旅(23):探索NHibernate二级缓存(上)(new!)

NHibernate之旅(24):探索NHibernate二级缓存(下)(new!)

……期待更新……

最终站:旅途更新中

最后宣传

期待这次旅途愉快!一路顺风!期待你的再次光临!下一站我们去哪儿?

作者:李永京YJingLee's Blog
出处:http://lyj.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

[模板]Template merging with NVelocity and ASP.NET

mikel阅读(758)

Introduction

If you ever tried to implement a newsletter system or a service for communicating with website users you have probably faced the requisite to send multiple email messages with a common template and some differences, like a personalized greeting in the message header, or an unsubscribe link with a personalized querystring.

Maybe the first way to accomplish this is by writing the static parts of the message in plain text files – for example emailheader.txt, emailfooter.txt – and reassemble them in the code, merging the varying parts, like recipient's name, surname, personalized querystring link, and so on.

But what about having a template engine which, given a template – be it a file on the filesystem, an assembly embedded resource or an in-memory object – and the information we want to merge in the template, did this all for us?

"NVelocity[^] is a .Net-based template engine. It permits anyone to use the simple yet powerful template language to reference objects defined in .Net code.

The purpose of this project is to port the Jakarta Velocity[^] project to Microsoft .Net (written in C#)."

Going back to the newsletter example, using NVelocity the the developer just needs to:

  • create a template for email messages specifying the parts that will have to be merged using a simple language called VTL (Velocity Template Language)
  • supply the information to be merged.

NVelocity will do the rest.

Background

In order to write NVelocity templates a basic knowledge of the language it uses would be helpful. Anyway in email templates it's rarely necessary to write loops, methods or branch conditions, instead more often the need is to replace single variables with values provided programmatically.

In this article I'm going to show a simple way of writing templates, but if you think that you need something more complex my advice is to read the VTL Refence Guide[^].

The notation for variables, as taken from the VTL Reference guide, is the following:

Notation (variable name):

$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, , _ ][ } ]

Examples:

  • Normal notation: $mud-Slinger_9
  • Silent notation: $!mud-Slinger_9
  • Formal notation: ${mud-Slinger_9}

About the article

In this article I am going to show how I implemented a wrapper for NVelocity functionalities, which simplifies merging templates coming from filesystem, assembly resources or in-memory objects, and will build a simple web project for demonstration.
The source code zip file contains the wrapper project, while the sample project zip file contains a web project which shows how to use it to merge templates in ASP.NET.

As a note, keep in mind that the wrapper can be used in any other project type, from console applications to WindowsForms and WebServices as well. I have chosen to build the demo in ASP.NET because it is a good compromise between nice user interface and ease of creation.

Using the code

The main access point is the class NVelocityEngineFactory, which exposes three static methods. Each of these methods return an object that implements the INVelocityEngine interface, which can respectively be used to retrieve templates from embedded resources, filesystem or memory. The bool parameter required by each of the methods is used to specify wether the result of the merge has to be cached or not.

The three correspondent engine types are NVelocityAssemblyEngine, NVelocityFileEngine and NVelocityMemoryEngine.

All of the inherit from the NVelocityEngineBase base class, which defines a common constructor and a static protected method.

Since implementing the INVelocityEngine interface, the three engine classes expose one public method with one overload called Process, which take two or three parameters and return a string or void.

In the first case the two input parameters are the IDictionary object containing the values to be merged in the template and the template itself, while the return value is the merged template put in a string object.

In the second case the additional parameter is a TextWriter object, which after the processing will contain the merged template.

The template

To use the three types of engines we need to create a template to be merged. Using VTL syntax, we write a simple template which can be used to render a message containing a simple personalized greeting, along with the date when the merging has been done.

To test all of the three engines the demo project contains a template file placed on the filesystem, a template file embedded in the assembly as a resource, and an in-memory template (a string) created at runtime.

To write a template file we just need to open a text editor and save the file with the .vm extension.

Collapse
Hi $name $surname, this output has been generated from a $templateType template on $date.

This is a very simple template, but it's not difficult to imagine in its place an entire html email with nice formatting and some images.

Using the wrapper along with the templates

Now all we need to do is:

  • Create new instances of the template engines supplying the information about the template.
    In order to process a file template the directory of the template is needed while processing an embedded resource template will require the assembly name. Finally, to process an in-memory object no specific parameter is needed, the template will be supplied when the Process method of the engine is called.
  • Add some contents to an object implementing IDictionary interface and pass it to the engine Process method.

File template

Supposing we have placed the template file in a subdirectory called Templates in the root folder of our web project and named it SimpleTemplate.vm:

Collapse
string templateDir = HttpContext.Current.Server.MapPath("Templates");
string templateName = "SimpleTemplate.vm";
INVelocityEngine fileEngine =
NVelocityEngineFactory.CreateNVelocityFileEngine(templateDir, true);
IDictionary context = new Hashtable();
context.Add("name", TextBox1.Text);
context.Add("surname", TextBox2.Text);
context.Add("templateType", "file");
context.Add("date", DateTime.Now.ToString("D"));
LabelMergedFile.Text = fileEngine.Process(context, templateName);

The LabelMergedFile Text attribute will contain the text of the template and, in place of the variables, the values suppied in the code.

Embedded resource template

Supposing we have placed the template file in a subdirectory called EmbeddedResources in the root folder of our web project, named it SimpleTemplate.vm and marked it as an embedded resource (under VisualStudio this can be accomplished by right clicking the file, choosing Properties, and then setting the "Build Action" property to "Embedded Resource"):

Collapse
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
INVelocityEngine embeddedEngine =
NVelocityEngineFactory.CreateNVelocityAssemblyEngine(assemblyName, true);
IDictionary context = new Hashtable();
context.Add("name", TextBox1.Text);
context.Add("surname", TextBox2.Text);
context.Add("templateType", "embedded");
context.Add("date", DateTime.Now.ToString("D"));
LabelMergedEmbedded.Text =
embeddedEngine.Process(context, "EmbeddedResources.SimpleTemplate.vm"); 

Note that when calling the Process method you need to specify the path of the resource, like in file system paths but with dots in place of slashes.

In-memory template

Differently from the previous cases, now the template must reside in memory, so we create a string object that contains the template:

Collapse
string template = "Hi $name $surname, this output has been generated from a $templateType template on $date.";
INVelocityEngine memoryEngine =
NVelocityEngineFactory.CreateNVelocityMemoryEngine(true);
IDictionary context = new Hashtable();
context.Add("name", TextBox1.Text);
context.Add("surname", TextBox2.Text);
context.Add("templateType", "memory");
context.Add("date", DateTime.Now.ToString("D"));
LabelMergedMemory.Text = memoryEngine.Process(context, template);

Note that the keys of the objects you place in the IDictionary object must correspond to the variable's names specified in the template if you want NVelocity to merge them. In case you forget to supply a key whose corresponding variable is contained in the template NVelocity just forgets about it and will give in output $variablename.

Points of Interest

I wrote this brief tutorial because me myself have been looking for something like this while I was implementing a newsletter service, and after getting crazy at trying to merge the templates by hand.

NVelocity can do much more than this anyway, and if you want to find a place where NVelocity is truly "milked" you should take a look at CastleProject[^], where they use it as a view engine in their MonoRail[^] project.

The sad thing about NVelocity is that the project is almost dead; the latest release, 0.4.2, is dated October 27, 2003.

The positive thing is that CastleProject developers have made a fork of the project, fixed some bugs and killed a tedious assembly dependence on an old Log4Net distribution, which NVelocity used as internal logger engine.

Actually this NVelocity fork has reached version 0.5, which is the one I have included in the sample project, and whose source can be found in CastleProject's SVN repository.

Revision History

  • 09 Feb 2006: Added support for embedded and in-memory templates
  • 17 Jan 2006: Initial release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Simone Busoli

Member
Occupation: Software Developer
Location: Italy Italy

[MVC]最简单的 MVC 模式实例

mikel阅读(1062)

有许多朋友质疑这种MVC模式不正确。如果按照经典的MVC模式定义,这种实现确实是不正确的。

经典的MVC模式中,控制器仅仅是根据请求来决定调用哪一个视图,然后视图再调用模型来获取结果并显示。而我下面实现这种MVC模式,控制器要根据请求来决定调用哪一个模型,并传递参数给模型,然后获取结果。最后将结果传递给视图,视图只负责显示结果。

两种实现的比较如下:

  经典MVC模式 异化的MVC模式
控制器 仅负责决定调用哪一个视图 需要负责调用模型,并将从模型获取的结果传递给视图
视图 需要决定调用哪一个模型,获取结果后显示 仅仅是显示传入的数据
模型 根据调用返回结果,不与控制器和视图发生关系 根据调用返回结果,不与控制器和视图发生关系
优点 分离表现层和业务层,控制器非常简单 分离表现层和业务层,视图非常简单,更容易和模版引擎结合
缺点 视图依赖于模型,容易将业务逻辑混入视图 控制器更复杂,容易将业务逻辑混入控制器

上面的表格简单比较了经典MVC模式和异化后的MVC模式之间的差别和优劣。

事实是,现在流行的PHP开发框架,都采用了异化后的MVC模式。因为异化后的MVC模式更容易和模版引擎结合,而且再配合Template View、Front Controller、Dispatcher等模式,能够进一步提高应用程序的结构化程度。

对于简单的应用程序,模型提供数据库CRUD操作,而控制器完成一部分业务逻辑操作。如果没有使用模版引擎,那么在控制器中使用include()就可以载入模版文件(视图)来显示结果。

对于复杂的应用程序,模型封装业务逻辑,而数据库操作则由数据源层负责。此时控制器只是调用模型获得结果,然后传递给视图。即便使用模版引擎,控制器也可以通过控制器基类的方法来处理。这样不但简单、代码量少,而且可以很方便的使用不同的模版引擎。

======================================

这个实例虽然简单,但是充分体现了 MVC 模式对分离“表现层”和“业务逻辑层”带来的帮助。

首先呢,还是有一个调度器,负责根据 HTTP 请求决定要调用的控制器:

PHP代码
  1. <?php    
  2. require ('controller/' . preg_replace('/[^a-z0-9_]+/i'''$_GET['controller']));    
  3. ?>  

一个控制器:

PHP代码
  1. <?php    
  2. // 从 Model 获取数据    
  3. require ('model/m1.php');    
  4. $m = new m1();    
  5. $data = $m->getData();    
  6.      
  7. // 构造视图,显示输出    
  8. require ('view/v1.php');    
  9. $v = new v1();    
  10. $v->assign($data);    
  11. $v->display();    
  12. ?>  

一个 Model:

PHP代码
  1. <?php    
  2. class m1    
  3. {    
  4.     function getData() {    
  5.         return 'hello';    
  6.     }    
  7. }    
  8. ?>   
 

一个 View:

PHP代码
  1. <?php    
  2. class v1    
  3. {    
  4.     var $data;    
  5.      
  6.     function assign($data) {    
  7.         $this->data = $data;    
  8.     }    
  9.      
  10.     function display() {    
  11.         echo $this->data;    
  12.     }    
  13. }    
  14. ?>

[Web]2009年值得关注的5个威客类网站

mikel阅读(767)

2008是不平凡的一年, 无论是百年盛世的奥运,还是百年难遇的金融危机, 都让身在其中的我们感到有些应接不暇。 网络世界也同样有许多新鲜事物的涌现。
威客(witkey)理论自从诞生以来,两年多来在众多威客模式网站的探索和实践下,初步形成了自己的理论体系并涌现出大批创新应用。2008年,这些网站又有哪些创新? 本文将挑选出较为典型的案例进行介绍和分析。
一、任务中国(www.taskcn.com)的“三大保障”出炉
从悬赏免费到建立国际化英文网站,再到设计“招标任务“流程,任务中国(taskcn.com)是威客模式领域中具有创新精神的网站之一。在 2008年为了将客户的在线外包风险降至最低, 率先提出了“三大保障”概念。内容如下:一、风险保障,任务结果不满意就退款。二、售后服务,所获作品有问题享受先行赔付。三、控制时间成本,找到能力强 且信誉好的工作者。3年来任务中国把交易方式提升了几个台阶,制定了很多行业规则。这一次也不例外。
二、猪八戒威客网(www.zhubajie.com)的“预付20%赏金”即可发布任务
猪八戒威客网是目前威客模式领域最为出名的网站之一,这与猪八戒网出现的几次重大的营销事件有直接关系,用威客模式进行市场营销为猪八戒网赢得了 巨大的关注和经济效益。这一次为鼓励任务发布者发布高金额,高复杂度的任务,猪八戒允许2000元以上的任务先预付20%就可以发布,威客们可以选择有兴 趣的任务报名,等待客户支付余下的80%赏金,就可以正式开始工作了。
三、标志客网(www.logoke.com)的“垂直门户”模式
标志客网是一个刚刚成立的专门针对标志设计方面的威客网。就在各大主要威客网向着更大更全的方向发展的时候,标志客却在向纵深发展。在任何商业领 域,市场的细分都是一种趋势,看来威客网也不例外。值得主意的是该网站提出的标志超市概念,试图为没有中标的作品找到了一条出路。就目前来看垂直门户不失 为一种新的探索, 值得大家继续关注。
四、淘智网(www.tallzhi.com)的“知识出售”模式
威客模式一个重要发展方向知识出售式威客即C类威客,在淘智网出现后开始逐步得到人们的注意。淘智网为每一个用户开立一个虚拟店铺,在店铺里出售 的不是各种实物商品,而是个人的知识,智慧,能力和经验。目前淘智网已拥有近十万名各领域的专业威客,包括退休老工人。大学教授和博士,企业经营管理人员 等。淘智网对C类威客进行了有益的尝试,让威客真正体会到知识就是财富。国际文传电讯,计算机世界对淘智网的商业模式进行了详细报道。
五、职客网(www.facejob.cn)的“悬赏找工作”模式
职客网(facejob.cn)建立于2006年,是中国科学院研究生院管理学院的一批学生在我国著名网络经济学家吕本富教授的指导下,把威客理 念应用在求职领域的一次创新。它围绕“求职个性化”的主题,通过求职者的职位发布,职客的揭榜,双方互相选择后,职客利用自己的信息和人脉关系把求职者推 荐到合适的工作岗位。2007年职客网得到了中央电视台,人民日报,工人日报的关注。职客并成为2007年中国最热的词之一。

[MVC]ASP.NET MVC 2 Preview 1 发布

mikel阅读(715)

ASP.NET MVC是既ASP.NET WebForms之后,微软推出的Front Controller式的Web开发模型,它弥补了前者对HTML控制能力不足,单元测试较为困难等缺点。更重要的是,ASP.NET MVC基于MS-PL发布,是一个真正的开源框架——且没有任何平台限制,也就是说,您可以在mono下使用或开发ASP.NET MVC的相关项目。

其实微软在今年3月的MIX大会上发布ASP.NET MVC RTM的时候,就已经公布了部分ASP.NET MVC 2的计划,并且在官方代码源中包含的MvcFutures项目中实现了V2的部分功能雏形。在沉寂了4个多月之后,现在微软终于发布了ASP.NET MVC 2的Preview 1版本,并在论坛中向社区征求反馈意见和建议。令人放心的是,ASP.NET MVC 2 Preview 1能够与ASP.NET MVC 1.0 RTM共存,不会影响后者的正常使用。

Scott Guthrie一如既往地在第一时间内撰写博文,详细而又简单地介绍了Preview 1中的新特性。ASP.NET MVC 2的“主题”是“提高生产力”,Preview 1的主要功能有:

  • 区域(Area):Area提供了将Controller和View分组的功能,这个特 性可以构建一个大型应用程序中相互独立的部分。每个Area可以独立放在不同的ASP.NET MVC项目中,并且由主应用程序共同引用。这个特性可用于应对大型应用程序所带来的复杂性,也使多个团队能够更方便地同时开发同一个应用程序。
  • 数据标记验证(Data Annotation Validation):ASP.NET MVC 2提供了内置的数据标记验证功能。这个功能利用了.NET 3.5 SP1中加入的自定义属性(Required,StringLength,Range,RegularExpression等),并且已经运用在 ASP.NET Dynamic Data框架与.NET RIA Services中。利用这一功能,开发人员可以为Model或ViewModel添加验证规则,ASP.NET MVC框架则会自动进行数据绑定或UI验证。
  • 强类型UI辅助方法:ASP.NET MVC V2包含了新的HTML UI辅助方法,它利用了强类型的Lambda表达式来操作View模板的Model对象。这样在编写视图代码时便可以充分获得IDE的智能提示。更重要的是,它为视图带来更好的编译期检验能力。
  • 模板化辅助方法(Templated Helper):这一功能可以根据数据类型自动选择相关的模板。例如,在视图中生成一个System.DateTime输入功能时,将会运用一个日期选择器模板。这与ASP.NET Dynamic Data框架中的Field Template有些接近,不过Preview 1中的模板化辅助方法是专为ASP.NET MVC框架而设计的。

此外,微软还公布了ASP.NET MVC 2的路线图。除了Preview 1中已经公开的内容之外,Preview 2中会包括以下功能:

  • 客户端验证:在Preview 1中模板化辅助方法及数据标记验证功能的基础上,构建一个客户端验证功能。
  • 强类型输入(input)辅助方法:使用强类型的表达式构建出针对Model的输入元素。这些辅助方法还会利用数据标记验证功能来减少错误(如拼写错误)。
  • 强类型链接(link)辅助方法:在IDE智能提示的辅助下,使用强类型的表达式来生成面向特定Controller和Action的链接。
  • 异步Action:提供开发不阻塞线程的Action的方法,这可以显著提升站点的伸缩性,尤其是在需要访问外部资源的情况下。
  • 区域(Area)功能增强:可以在同一个项目中更好地组织应用程序,而不必分拆成多个项目。
  • 其他改进:继续修复ASP.NET MVC 1.0及ASP.NET MVC 2 Preview 1中已知的问题,并根据用户反馈进行API增强,以及一些细微的新功能。

除了Scott Guthrie之外,Scott Hanselman以及ASP.NET MVC团队的Phil Haack也在博客中介绍了ASP.NET MVC 2 Preview 1的情况,MSDNChannel 9还为“模板化辅助方法”这一重要功能提供了进一步的讲解和演示。更多消息请参考ASP.NET MVC 2 Preview 1的Release Notes,您还可以下载源代码对其进行深入了解。

[C#]C#生转换网页为pdf

mikel阅读(881)

最近工作中遇到一个将htm转换为pdf的任务,这是一个有很有用的功能块,然而很遗憾,网上没有现成可行(包括开源/免费、易用和可维护性的考虑)方案。既然没有现成的解决方案就自己着手解决吧。
从 htm生成pdf大概可以分两步实现,第一步,解析htm,就是将htm源文件中那一对文本转换为浏览器最终呈现给我们那种图文并茂的结果。这是一个不可 完成的任务,因为目前为止业界的软件巨头也没有谁把htm解析做得很好的。对比ie、firefox等浏览器的显示结果便可想而知。既然业界难题,我也就 不去钻牛角尖做技术攻关了,先跳过这步,考虑下一步的事情。
第二步,绘制pdf,这个简单,网上有很多资料,有兴趣的朋友可以研究pdf的文件格 式,安装二进制组装pdf。我有兴趣,然而没有时间,我觉得软件从业者时刻都应该关注最有价值的事情。软件从业者要提高效率的第一法门便是重用,网上有一 个叫itextsharp的东西是用来绘制pdf的,可以免费使用而且开源。
下载itextsharp,试着用itextsharp绘制htm看看效果,如您所料,绘制出的是htm的源代码。因为第一步的事情我们还没有解决,下面来解决第一步的事情。
记得很久以前见过一个.net写的网页snap工具,大概思路是利用webbrowser的DrawToBitmap方法将ie的显示结果输出到Sytem.Drawing.Bitmap对象。大概代码如下:

//WebBrowser wb=null;
 System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(w, h);
 wb.DrawToBitmap(bmp, 
new System.Drawing.Rectangle(0,0, w, h));

ok,htm可以解析了,现在重组刚才的代码,思路如下:
使用webbrowser将htm解析并转换为图片,使用itextsharp将刚才的图片绘制成pdf。
有用是给公司开发的功能,暂时不便公开源码,提供我编译后的工具供下载使用,您也可以根据上面的思路定制:
使用方法,
1.将单个url转换为pdf:PageToPDF.exe "http://www.g.cn/" "google.jpg"
2.将多个url转换为pdf:pagetopdf.exe task.txt "C:\pdfdir\"
 task.txt是任务里表,里面提供多行url,每个url以#文件名为后缀,如:http://www.baidu.com/#b表示将http://www.baidu.com/转换为pdf文件名为b(扩展名系统自己会追加)
ASP.NET环境下使用
将pagetopdf上传至网站中,设定好目录权限,示例代码:



        
public static bool CreatePPDF(string url,string path)
        {
            
try
            {
                
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path))
                    
return false;
                Process p 
= new Process();
                
string str = System.Web.HttpContext.Current.Server.MapPath("~/afafafasf/PageToPDF.exe ");
                
if (!System.IO.File.Exists(str))
                    
return false;
                p.StartInfo.FileName 
= str;
                p.StartInfo.Arguments 
= " \"" + url + "\" " + path;
                p.StartInfo.UseShellExecute 
= false;
                p.StartInfo.RedirectStandardInput 
= true;
                p.StartInfo.RedirectStandardOutput 
= true;
                p.StartInfo.RedirectStandardError 
= true;
                p.StartInfo.CreateNoWindow 
= true;
                p.Start();
                System.Threading.Thread.Sleep(
500);
                
return true;
            }
            
catch(Exception ex)
            {
                Sys.Log.error(
"Pdf create err.",ex);
            }
            
return false;
        }

特性
在使用任务形式工作时,系统会启动多个进程,即任务管理器中会有多个pagetopdf.exe的进程,这是系统调度程序自己启动的,为了加个任务处理速度。进程数由调度程序自己控制,最多不会超过十个。

[性能]网站页面静态化方案

mikel阅读(817)

      在 大型网站中,访问者看到的页面基本上是静态页面。为什么都要把页面静态化呢?把页面静态化,好处有很多。例如:访问速度快,更有利于搜索引擎收录等。目前 主流的静态化主要有两种:一种是通过程序将动态页面抓取并保存为静态页面,这样的页面的实际存在于服务器的硬盘中,另外一种是通过WEB服务器的 URL Rewrite的方式,他的原理是通过web服务器内部模块按一定规则将外部的URL请求转化为内部的文件地址,一句话来说就是把外部请求的静态地址转化 为实际的动态页面地址,而静态页面实际是不存在的。这两种方法都达到了实现URL静态化的效果,但是也各有各自的特点。

将动态页面转化为实际存在的静态页面这种方法,由于静态页面的存在,少了动态解析过程,所以提高了页面的访问速度和稳定性,使得优化效果非常明显。所以这种方法被广泛采用。但是它的局限性同样存在。对于大型网站而言,这种方法将带来不可忽视的问题。

一、由于生成的文件数量较多,存储需要考虑文件、文件夹的数量问题和磁盘空间容量的问题;

二、页面维护的复杂性和大工作量,及带来的页面维护及时性问题,需要一整套站点更新制度。

URL Rewrite方式特点同样鲜明,由于是服务器内部解析的地址,所以内容是实时更新的,也不存在文件管理和硬件问题,维护比较方便。在服务器级URL Rewrite重写技术并不影响页面的执行速度。但是URL Rewrite的门槛比较高,国内虚拟主机大多不支持,而且虚拟主机是目录级的URL Rewrite,通过遍历目录读物URL转发规则的方式将大大降低页面的执行速度。

除了抓取动态页面和URL Rewrite的方法外,在这里我们再看一下另外的一种方法。此方法的核心思想就是:把页面划分成子数据块,每个数据块可能是一个inc文件,也可能多个数据块包含在一个inc文件中。具体的数据块划分根据页面的业务结构来处理。比如:网站头尾等公共数据块可以独立成一个文件。这种方法需要考虑以下几个方面:

1、用什么方式生成页面及里面的数据块

2、页面的更新机制;

3、大量的页面文件的维护工作;

4、页面数据块的及时性。

这种方式的话,通常可以在后台增加一个服务程序,专门生成某个频道或栏目的页面。这样虽然可行,按照频道分的话,逻辑结构也清晰。



【单服务模式】

这样会带来一些问题。例如:当频道修改后,相应的服务程序都要重新翻一遍。如果频道栏目很多,对应的服务程序也会很多,导致程序的维护工作量大。前台开发人员不仅要去做页面,也要考虑后台的服务程序结构,给他们增加了不必要的开发难度,降低了开发效率。

 


【多服务模式】

而在多服务模式下,会出现多台服务去争抢指令数据的情况。动作指令的状态必须在多个服务之间同步。服务升级了,也要一个一个去更新,出现错误了也要一个一个去排查。。。。。。

那 么有没有一种方法能把生成页面的功能独立抽象成一个平台,同时提供一个程序接口,前台开发人员只需要按照这个接口,开发业务组件即可。现在前台开发人员只 需要把写好的业务组件,部署到指定的地方即可。剩下的事情交给这个平台去做,这样就简化了系统发布,维护工作,减轻了前台开发人员的工作量,提高了他们的 开发效率。


【平台集中处理模式】

       动作指令是指页面更新的动作,当页面数据有变化时,会根据业务规则从某个地方发出一个动作。它的来源大致可以分为三种:前台页面触发,后台内容管理系统触发,后台自动定时触发。

静态数据生成系统与业务组件的接口设计。通过反射的方式调用业务组件,接口的参数在指令结构的基础上扩展即可。比如增加一些错误描述,数据库链接对象等。

数据分发是一个独立的数据传输系统,它负责根据预先设定好的配置,把生成的页面数据传输到指定的web服务器上。

为了使系统在随着网站访问量的上升的同时做到水平扩展,加快指令的处理速度。所以需要把系统部署到多台服务器上,这样以来各个子系统就要统一通信协调。可以用MQ消息作为子系统之间的通信手段。子系统的部署模式变为Master-Slave的形式。Master主机上的系统负责读指令,然后把指令发送到MQ。各个Slave主机系统负责接收MQ消息指令,调用业务组件并更新某条指令的状态,这样就把处理业务逻辑的压力平均的分配到了各台slave主机。

    对于一个大型网站来说,生成的页面数据会非常多,管理这些页面文件又是一个问题。例如有的页面被删除了,而已经生成的页面数据还会存在各个web服务器上。这时就需要通过后台系统记录这些页面文件的部署位置,以便今后统一管理。同时业务组件的量也可能会比较多,特别是存在多版本的情况下,所以也需要把业务组件的配置情况记录到数据库中,便于统一管理。