C# 4.0: Truyền tham số theo tên

Trong C# 4.0, bạn không cần truyền các tham số theo đúng thứ tự của nó, mà có thể dùng tên để chỉ ra tham số được truyền tương ứng. Trong nhiều trường hợp, điều này giúp chương trình của bạn dễ hiểu hơn.

Ví dụ:

void MyMethod(int a, string s) 
{
    doSomeThing();

}

Bạn có 2 cách để gọi phương thức này:

MyMethod(10, "my string");

hoặc theo tên tham số:

MyMethod(a: 10, s: "my string");

và tất nhiên nếu dùng tên thì các tham số không cần phải đặt theo đúng thứ tự khi khai báo:

MyMethod(s: "my string", a: 10);

Cách truyền theo tên cũng có thể dùng chung với cách truyền theo thứ tự, tuy nhiên trong trường hợp này các tham số truyền theo tên phải nằm sau, ví dụ:

MyMethod(10, s: "my string");

ví dụ sau là không hợp lệ:

MyMethod(a: 10, "my string");

Silverlight 4 + RIA Services: Ready for Business: Cập nhật dữ liệu từ client

Để tiếp tục loạt bài, chúng ta hãy xem qua phần cập nhật dữ liệu. Tôi đã tạo một file có tên Plates.xaml có cấu trúc rất giống ở trên. Để xem chi tiết cách tạo file này, xin hãy xem các bước demo của tôi tại PDC 09.

image_thumb[49]

Continue reading “Silverlight 4 + RIA Services: Ready for Business: Cập nhật dữ liệu từ client”

LINQ to SQL vs. ADO.NET Entity Framework

LINQ to SQL and the Entity Framework have a lot in common, but each have features targeting different scenarios in the Orcas timeframe.

LINQ to SQL has features targeting “Rapid Development” against a Microsoft SQL Server database. Think of LINQ to SQL as allowing you to have a strongly-typed view of your existing database schema. LINQ to SQL supports a direct, 1:1 mapping of your existing database schema to classes; a single table can be mapped to a single inheritance hierarchy (i.e., a table can contain persons, customers, and employees) and foreign keys can be exposed as strongly-typed relationships.  You can build LINQ queries over tables/views/table valued functions and return results as strongly typed objects, and call stored procedures that return strongly typed results through strongly typed methods.  A key design principle of LINQ to SQL is that it “just work” for the common cases; so, for example, if you access a collection of orders through the Orders property of a customer, and that customer’s orders have not previously been retrieved, LINQ to SQL will automatically get them for you.  LINQ to SQL relies on convention, for example default insert, update, and delete logic through generated DML can be overwritten by exposing appropriately named methods (for example, “InsertCustomer”, “UpdateCustomer”, “DeleteCustomer”).  These methods may invoke stored procedures or perform other logic in order to process changes.

The Entity Framework has features targeting “Enterprise Scenarios”.  In an enterprise, the database is typically controlled by a DBA, the schema is generally optimized for storage considerations (performance, consistency, partitioning) rather than exposing a good application model, and may change over time as usage data and usage patterns evolve.  With this in mind, the Entity Framework is designed around exposing an application-oriented data model that is loosely coupled, and may differ significantly, from your existing database schema.  For example, you can map a single class (or “entity”) to multiple tables/views, or map multiple classes to the same table/view. You can map an inheritance hierarchy to a single table/view (as in LINQ to SQL) or to multiple tables/views (for example, persons, customers, and employees could each be separate tables, where customers and employees contain only the additional columns not present in persons, or repeat the columns from the persons table).  You can group properties into complex (or “composite”) types (for example, a Customer type may have an “Address” property that is an Address type with Street, City, Region, Country and Postal code properties). The Entity Framework lets you optionally represent many:many relationships directly, without representing the join table as an entity in your data model, and has a new feature called “Defining Query” that lets you expose any native query against the store as a “table” that can be mapped just as any other table (except that updates must be performed through stored procedures).  This flexible mapping, including the option to use stored procedures to process changes, is specified declaratively in order to account for the schema of the database evolving over time without having to recompile the application.

The Entity Framework includes LINQ to Entities which exposes many of the same features as LINQ to SQL over your conceptual application data model; you can build queries in LINQ (or in “Entity SQL”, a canonical version of SQL extended to support concepts like strong typing, polymorphism, relationship navigation and complex types), return results as strongly typed CLR objects, execute stored procedures or table valued functions through strongly-typed methods, and process changes by calling a single save method.

However, the Entity Framework is more than LINQ to Entities; it includes a “storage layer” that lets you use the same conceptual application model through low-level ADO.NET Data Provider interfaces using Entity SQL, and efficiently stream results as possibly hierarchical/polymorphic DataReaders, saving the overhead of materializing objects for read-only scenarios where there is no additional business logic.


The Entity Framework works with Microsoft SQL Server and 3rd party databases through extended ADO.NET Data Providers, providing a common query language against different relational databases through either LINQ to Entities or Entity SQL.

So while there is a lot of overlap, LINQ to SQL is targeted more toward rapidly developing applications against your existing Microsoft SQL Server schema, while the Entity Framework provides object- and storage-layer access to Microsoft SQL Server and 3rd party databases through a loosely coupled, flexible mapping to existing relational schema.

I know this is a confusing area, and we’re trying to figure out how best to describe these differences to help customers make the appropriate choices.  Please let me know if this helps, or if there are still areas of confusion…


Thanks,

Michael Pizzo

Principal Architect

Microsoft Data Programmability

Silverlight 4 + RIA Services – Ready for Business: Bắt đầu một ứng dụng mới với Business Application Template

Để bắt đầu loạt bài mới này, tôi muốn nhắm vào mục tiêu là giúp các bạn tập trung vào business, không phải code (chỗ này chưa biết dịch sao 😦 ). Cái đầu tiên bạn thấy là các thành phần có sẵn trong Business Application Template. Nó mô tả một cấu trúc ứng dụng có quy tắc, trông rất hay và dễ dàng tùy biến.

Continue reading “Silverlight 4 + RIA Services – Ready for Business: Bắt đầu một ứng dụng mới với Business Application Template”

Silverlight 4 + RIA Services – Ready for Business

Silverlight 4 đã xuất hiện, và với mục tiêu xây dựng Silverlight như một nền tảng ứng dụng, chứ không đơn giản là một công nghệ đơn lẻ cho web. Silverlight 4 đã bước một bước tiến rất dài kể từ khi ra mắt phiên bản đầu tiên, giờ đây khi nói đến nó, người ta không thể không nhắc đến .NET, IIS, cơ sở dữ liệu…

Cùng với nhiều công nghệ và cải tiến khác, RIA Service là một trong những thành phần quan trọng đối với Silverlight, nó giúp chúng ta xây dựng các ứng dụng hướng dữ liệu một cách dễ dàng, và như vậy việc xây dựng các ứng dụng doanh nghiệp trên Silverlight giờ đây đơn giản hơn bao giờ hết. Người dùng bây giờ khó tính hơn trước đây rất nhiều, ngay cả với những ứng dụng doanh nghiệp, vốn chỉ để làm việc, thì cũng cần phải đẹp, phải có tính tương tác, hình ảnh, các bảng biểu phải động, khả năng kéo thả, theme,… và nhiều thứ khác trước đây chỉ có trong các ứng dụng giải trí thì nay cũng đã xuất hiện trong nhiều ứng dụng cho người dùng doanh nghiệp.

Với mục đích chia sẻ kiến thức, tôi sẽ dịch lại loạt bài về RIA Service, từ blog của Brad Abrams, hiện là Product Unit Manager của nhóm Application Framework (phát triển Silverlight), ông cũng là thành viên sáng lập của nhóm phát triển Common Language Runtime, và .NET Framework (tuy nhiên đã rời khỏi Microsoft vào cuối tháng 4/2010). Các bài viết trong loạt bài này tương đối ngắn nhưng đủ cho các bạn có thể hiểu và làm việc được với RIA Service.

Link gốc cả bài viết tại đây: http://blogs.msdn.com/brada/archive/2010/03/15/silverlight-4-ria-services-ready-for-business-index.aspx

Danh sách các bài dịch tại đây (đang được cập nhật thêm):

Đào Hải Nam

Dịch bởi Đào Hải Nam

Thông tin về tác giả bài viết có thể xem tại đây: https://namdh.wordpress.com/2010/04/19/silverlight-4-ria-service-ready-for-business/

Xây dựng một ứng dụng “Hello World” cho Windows Phone 7 dùng Silverlight

Bài viết này được dịch từ http://weblogs.asp.net/scottgu/archive/2010/03/18/building-a-windows-phone-7-twitter-application-using-silverlight.aspx

Xây dựng một ứng dụng “Hello World” cho Windows Phone 7

Đầu tiên phải đảm bảo là bạn đã cài đặt Windows Phone Developer Tools CTP – nó hoặc bao gồm công cụ phát triển Visual Studio 2010 Express for Windows Phone (miễn phí mãi mãi và là thứ duy nhất bạn cần để xây dựng các ứng dụng cho Windows Phone 7) hoặc được cài như một add-on cho VS 2010 cho phép phát triển ứng dụng cho phone bên trong bản đầy đủ của VS 2010.

Continue reading “Xây dựng một ứng dụng “Hello World” cho Windows Phone 7 dùng Silverlight”

Một ví dụ về mô hình 3-tiers

Tranh thủ hôm cuối tuần, gửi con đi nhà trẻ, lên công ty viết cái ví dụ này. Hi vọng sẽ giúp ích cho những ai quan tâm.

Đây là một ứng dụng quản lý sách được viết dựa trên mô hình 3 tầng (3-tiers ), sử dụng LINQ to SQL như lớp truy xuất dữ liệu (DAL). Vì đây là một ứng dụng mẫu nên tôi đã cố gắng giảm thiểu chức năng cũng như việc dùng các tính năng được hỗ trợ bởi COM+, giúp chương trình đơn giản, dễ hiểu.

Continue reading “Một ví dụ về mô hình 3-tiers”

TV Today

Giới thiệu với các bạn sản phẩm đã đoạt giải khuyến khích cuộc thi “Thử thách Widget N97”: TV Today.

Đây là phần mềm cho phép người dùng xem lịch chiếu phim trên kênh CineMax và HBO. Lịch chiếu được lấy từ trang http://www.hboasia.com, ngoài ra, phần mềm còn cho phép người dùng lưu lại giờ chiếu phim vào Calendar, hoặc gửi tin nhắn đến bạn bè bằng SMS. Bạn có thể chọn lịch chiếu cho các ngày khác nhau, chọn quốc gia (Việt Nam hoặc Singapore), chọn sử dụng trình nhắn tin có sẵn trong máy hay được cung cấp bởi phần mềm…

Continue reading “TV Today”

Java 7 sẽ hỗ trợ biểu thức Lambda

Java Development Kit 7 (JDK 7) là phiên bản chính tiếp theo của nền tảng Java SE, dự kiến sẽ được phát hành trong năm 2010 (hiện đã có bản preview). Phiên bản này giới thiệu nhiều tính năng mới quan trọng trong việc tăng hiệu năng, tính khả dụng và bảo mật của nền tảng Java. Một trong những cập nhật về ngôn ngữ trong phiên bản này là các biểu thức Lambda.

Continue reading “Java 7 sẽ hỗ trợ biểu thức Lambda”

Hỏi đáp về mô hình 3 tầng

1. Mô hình 3 tầng (3-tiers) là gì?

Theo wikipedia thì:

“3-tiers là một kiến trúc kiểu client/server mà trong đó giao diện người dùng (UI-user interface), các quy tắc xử lý(BR-business rule hay BL-business logic), và việc lưu trữ dữ liệu được phát triển như những module độc lập, và hầu hết là được duy trì trên các nền tảng độc lập, và mô hình 3 tầng (3-tiers) được coi là một kiến trúc phần mềm và là một mẫu thiết kế.” (dịch lại từ wikipedia tiếng Anh).

Như vậy, ta có thể mô hình này phân tách ứng dụng ra làm 3 module riêng biệt, bao gồm:

– Tầng Presentation: được dùng để giao tiếp với người dùng, nhiệm vụ chính là hiển thị dữ liệu và nhận dữ liệu từ người dùng.

– Tầng Business Logic: nhiệm vụ chính là cung cấp các chức năng của phần mềm.

– Tầng Data: lưu trữ dữ liệu, cho phép lớp Business Logic có thể tìm kiếm, trích xuất, cập nhật… dữ liệu.

Continue reading “Hỏi đáp về mô hình 3 tầng”