CSharp

ROR flavor ASP.NET

in

Nobody cannot reject that Ruby are one of becoming mainstream languages nowadays. With the fame of Rails, it does not just push Ruby far like this but inspires many other frameworks to do the same.

Of course, C# and ASP.NET are also impacted from Rails too. I wrote about the extension method, one of new C# 3.0 feature set, that makes C# look like Ruby than before. Instead of typing DateTime.Now.AddMinutes(30) to get 30 minutes later time, 30.minutes.later does the trick. Today, I’ve crossed a blog post that utilizes the anonymous type to make ASP.NET Web Form even more looks like Rails View. Actually, he didn’t intentionally make it but I see the similarity as I read on.

If you want to create a hyperlink in Rails, you have two choices, create it manually or use helper. The former works with more performance at the cost of flexibility and the latter is the vice versa.

<a href="url here">The Link</a>

<%= link_to 'The Link', :url=>'url here' %>

Like Rails, you also have 2 choices in ASP.NET Web form to create a link, use HyperLink control or create it by hand. While Rails offers the flexibility in using helper to create link, controller and action can be provided to produce the url, ASP.NET provides you with a little advantage over the manual way, the Application Path, which I’ll discuss in this post.

To reproduce the same as above example with ASP.NET, we use this code.

<a href="url here">The Link</a>

<asp:HyperLink id="aHyperLink" runat="server" NavigateUrl="url here">The Link</asp:HyperLink>

Note that it has significantly more characters to type.

Let’s say that I want to crate a hyperlink with class attribute. I can add a attribute as an option to the link_to helper like this.

<%= link_to 'The Link', :url=>'url here' , :class=>"cssclass"%>

Although this can be achieve easily in ASP.NET by using HyperLink control, creating a helper maybe a good idea.Now we get this instead.

<%= HtmlHelper.GetHtmlLink('The Link', new { url = "url here", class = "cssclass")} %>

With anonymous types and property initializes feature, we can set up a property dictionary for hyperlink that’ll be passed to method GetHtmlLink easily. As the result, we get a more rails-like helper in ASP.NET. The two only problems for this thing are slowness and practicality. This approach heavily relies on Reflection to imply the anonymous type at runtime. Reflection related processes, which function at runtime, are always slower than compile time counter part. Importantly, you can get this by using HyperLink control by adding a cssclass property which is faster and more practical.

Now the ASP.NET Web Form is looked more like Rails.

PS. If you want have a try, go to Eilon Lipton’s blog to get the working code. As a reminder, you also need .NET Framework 3.5 to get C# 3.0.

Technorati tags: , , ,

How to reverse string

in

น่าแปลกใจที่ .NET ไม่มีคำสั่งสำหรับ reverse string แฮะ

C#
public string reverse(string s){
  char[] arrS = s.ToCharArray();
  Array.Reverse( arrS );
  string reversedString = new string( arrS );
  return reversedString;
}

php

$st = 'a string';
$st=strrev($st);

ruby

a = "abcdefg"
a.reverse
=> "gfedcba"

จริงๆของ python ก็ไม่มีคำสั่ง reverse string โดยตรง แต่ว่าใน python มอง string ว่าเป็น sequence type แบบนึง ก็เลยเขียน reverse string แบบนี้ได้

python

a = "abcdefg"
a[::-1]
=>'gfedcba'

รู้สึกว่าของ python จะเท่สุดเนอะ

Technorati tags: , , , , , ,

หัดใช้ XSLT

in

ปกติผมใช้ Google Reader อ่าน feed ซึ่งตัว GR เนี่ยก็จะมีความสามารถนึงคือ shared item ที่ให้สามารถเอาเรื่องไหนก็ได้มา share ให้คนอื่น ทีนี้ถ้าจะเอามาติดก็ใช้ wdiget ที่กูเกิลเตรียมให้มาแปะไว้ในหน้าเว็บเอา วิธีก็ง่ายดี สะดวกด้วย แต่หน้าตามันไม่สวย ปรับแต่งเองลำบากหน่อยนึง

ทีนี้ถ้าเราจะเอามาใช้ ก็ง่ายๆ Google Reader มี feed สำหรับ Shared Item ให้ ก็เลยคิดว่าจะลองเอามาใช้หัดเล่น XSLT ดู

reader-shared

ก่อนอื่นก็สร้างไฟล์ XSLT สำหรับแปลงไฟล์ XML ขึ้นมาก่อน โดยจะให้ผลลัพธ์ของไฟล์อยู่ในรูป

<h3>Title ของ RSS</h3>

<ul>

<li><a href="permalink">ลิงค์ไปเนื้อหา</a></li>

...

</ul>

จะได้ไฟล์ XSLT แบบนี้

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:gr="http://www.google.com/schemas/reader/atom/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
    <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/atom:feed">
        <h3>
            <xsl:value-of select="atom:title"/>
        </h3>
        <ul>
            <xsl:apply-templates select="atom:entry"/>
        </ul>
    </xsl:template>
    <xsl:template match="atom:entry">
        <li>
            <a href="{atom:link/@href}">
                <xsl:value-of select="atom:title"/>
            </a>
        </li>
    </xsl:template>
</xsl:stylesheet>

กว่าจะรู้เกี่ยวกับไอ้ตรงที่ทำตัวหนาไว้นี่กินเวลาไปหลายวันทีเดียว ก่อนอื่นเราต้องประกาศ namespace ของ atom ที่จะใช้กับ feed ของเราก่อน เพราะว่า feed ของเราใช้ namespace ของ atom เป็น namspace หลัก หลังจากนี้เวลาที่เราอ้างอิงถึง element ใน feed ก้ใช้ atom: นำหน้าเพื่อให้รู้ว่าเราหมายถึง element ไหน

ทีนี้ก็ลองสร้าง Console Application ที่ใช้ Xslt นี้ขึ้นมา

class Program {
    static void Main(string[] args) {

XmlReader reader = XmlTextReader.Create("http://www.google.com/reader/public/atom/user/14671298264700078722/state/com.google/broadcast");

XmlWriterSettings settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;

StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlTextWriter.Create(sb, settings);

XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load("feed.xslt");
xslt.Transform(reader, writer);

Console.WriteLine(sb.ToString());

ตรงนี้ต้องระบุให้ ConformanceLevel ให้เป็น Fragment เพราะผลลัพธ์ที่ออกมาจากการ Transform ไม่ใช้เอกสาร XML แต่เป็นส่วนหนึ่งของ XML หลังจากนั้นก็ยัด XML ที่อ่านมาเข้าไปใน XSLT แล้วก็จัดการ Transform ปุ๊บ ก็ได้ออกมาอยู่ใน StringBuilder เป็นอันเสร็จสิ้น

พอลองรันดูก็ได้ผลลัพธ์ออกมาแบบนี้

reader-shared2

ปล. Shared Item ทางขวา ใช้ clip ปกติที่ Google เตรียมให้

Technorati tags: , , ,

Second class citizen

in

C# กลายเป็นพลเมืองชั้นสองใน Silverlight ไปแล้วเหรอเนี่ย

InfoQ is reporting that:

"First of all, C# won't be fully supported in Silverlight. Unlike VB, Python, Ruby, and JavaScript, C# does not support the Dynamic Language Runtime and cannot be hosted for runtime compilation in Silverlight."

This is a bit of a stretch. What happens is that Silverlight will ship with compiler/interpreters that can compile source code written in Javascript, Python, Ruby and Visual Basic to native code.

But Silverlight will not include a C# compiler on the client side. You will still be able to author libraries and assemblies with C# and write your application with it, you just wont be able to dump a C# source file over the network and expect that to be compiled and ran on the client machine.

via Miguel's Blog

Technorati Tags: , ,