Thứ Sáu, 20 tháng 7, 2007

Sử dụng DotNetNuke như một mô hình phát triển ứng dụng Desktop (Phần II)

2.5. Mô hình lập trình với DotNetNuke

2.5.1. Mô hình các đối tượng trong DNN components

Chúng ta hãy xem xét một ví dụ của việc tạo ra các lớp để có thể thao tác với đối tượng lớp nhân viên (ứng với bảng HT_NhanVien trong CSDL). Các components được tạo ra sẽ có dạng thức như sau. Trong ví dụ này, phương thức lấy thông tin của một nhân viên theo khóa chính MaNhanVien được sử dụng để cho thấy các đối tượng trong kiến trúc của DNN.

Mô hình của các đối tượng trong DNN được trình bày trong sơ đồ dưới đây

Controller Class

Đối tượng điều khiển, sự dụng các phương thức của DataProvider để truy xuất dữ liệu.

Info Class

Lớp mô tả các đối tượng lưu trữ thông tin

DataProvider Class

Lớp ảo (abstract) định nghĩa các phương thức truy nhập dữ liệu. Lớp ảo này cho phép phát triển độc lập các mô tả truy nhập dữ liệu.

SqlDataProvider Class

Đối tượng truy nhập dữ liệu thực sự

Store Procedure

Các thủ tục ở CSDL MSSQL server được SqlDataProvider sử dụng

2.5.2. Triển khai cụ thể của các DNN Components

public class NhanVienController : DotNetNuke.Entities.Modules.ISearchable, DotNetNuke.Entities.Modules.IPortable

{
public NhanVienInfo Get(string maNhanVien)
{
return
(NhanVienInfo)DotNetNuke.Common.Utilities.CBO.FillObject(DataProvider.Instance().GetNhanVien(maNhanVien),
typeof(NhanVienInfo));
}
}
public class NhanVienInfo
{
#region "Private Members"
string _maNhanVien;
string _ten;
bool _gioiTinh;
DateTime _ngaySinh;
string _dienThoai;
string _diaChi;
string _maPB;
double _heSoLuong;
#endregion
#region "Constructors"
public NhanVienInfo()
{
}

public NhanVienInfo(string maNhanVien, string ten , bool gioiTinh , DateTime ngaySinh , string dienThoai , string diaChi , string maPB , double heSoLuong)
{
public abstract class DataProvider {
public abstract IDataReader GetNhanVien(string maNhanVien);
}
public class SqlDataProvider : DataProvider
public override IDataReader GetNhanVien(string maNhanVien)
{
return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "HT_NhanVienGet", maNhanVien);
}
}

2.6. Xây dựng thành phần (components) cho lớp Data Access Layer (DAL)

2.6.1. Khởi động CodeSmith Explorer

2.6.2. Tạo một kết nối mới

Chọn Bảng trong CSDL cần triển khai sqlDataProvider cho nó (Chúng ta có thể chọn nhiều bảng để sản sinh cùng một lúc, nhưng để việc trình bày trong sáng hơn thì tôi dẫn ra việc sinh mã với bảng HT_NhanVien

Có một số lưu ý: Các bảng được chọn sinh mã bắt buộc phải có trường khóa chính (Primary Key) trong bảng. Khi đó các phương thức của SqlDataProvider được sinh ra có thể nhận biết được các tiêu chí như thêm, sửa, xóa, cập nhật và tìm kiếm.

2.6.3. Việc sinh mã thực hiện thành công

Khi đó ta chỉ việc chép (copy) phần mã vừa sinh ra vào file SqlDataProvider.cs của lớp SqlDataProvider trong project tạo DAL mà ta đã tạo ra ở trên.

Đoạn mã chứa các phuơng thức của SqlDataProvider dành cho truy xuất thông tin Nhân viên trong CSDL

#region "NhanVien Methods"

public override IDataReader GetNhanVien(string maNhanVien)

{
return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "HT_NhanVienGet", maNhanVien);
}
public override IDataReader ListNhanVien()
{
return (IDataReader)SqlHelper.ExecuteReader(ConnectionString, DatabaseOwner + ObjectQualifier + "HT_NhanVienList");
}
public override int AddNhanVien(string ten, bool gioiTinh, DateTime ngaySinh, string dienThoai, string diaChi, string maPB, double heSoLuong)
{
return
int.Parse(SqlHelper.ExecuteScalar(ConnectionString, DatabaseOwner + ObjectQualifier + "HT_NhanVienAdd", GetNull(ten), GetNull(gioiTinh), GetNull(ngaySinh), GetNull(dienThoai), GetNull(diaChi), GetNull(maPB), GetNull(heSoLuong)).ToString());
}
public override void UpdateNhanVien(string maNhanVien, string ten, bool gioiTinh, DateTime ngaySinh, string dienThoai, string diaChi, string maPB, double heSoLuong)
{
SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "HT_NhanVienUpdate", maNhanVien, GetNull(ten), GetNull(gioiTinh), GetNull(ngaySinh), GetNull(dienThoai), GetNull(diaChi), GetNull(maPB), GetNull(heSoLuong));
}
public override void DeleteNhanVien(string maNhanVien)
{
SqlHelper.ExecuteNonQuery(ConnectionString, DatabaseOwner + ObjectQualifier + "HT_NhanVienDelete", maNhanVien);
}
#endregion

Xây dựng các Store Procedure một cách tự động cũng sử dụng một template có sẵn mà ta đã tải về trong DNN Template đó là StoredProcedures.cst. Có một số chú ý, vì các Store procedure được sử dụng trong việc cài đặt các modules của DNN trên mô hình portal nên một số tiền tố được lưu vào tong đoạn mã sql. Để chuyển về mô hình ứng dụng của chúng ta thì cần thay thế

* “{databaseOwner}” thành “dbo.” * “{objectQualifier}” thành “” (chuỗi rỗng)

Và công việc cuối cùng của chúng ta chỉ là chạy sql script này trong SQL Analyzer.

2.7. Xây dựng thành phần cho lớp triển khai (Bussiness Logic Layer-BLL)

Công việc triển khai lớp BLL cũng được thực hiện tương tự. Chúng ta sẽ sử dụng các code templates và dùng Code Smith để sinh mã cho các lớp trong BLL. Các templates này được liệt kê dưới đây:

Có một lưu ý khi sử dụng các template này là chúng ta phải cung cấp “{objectQualifier}” khi thực hiện sinh mã, tham số này bắt buộc phải có. Trong mô hình đưa ra ví dụ ở đây chúng ta đã xây dựng các bảng trong CSDL với tiền tố “HT_” và nó chính là tham số mà chúng ta phải cung cấp cho ObjectQualifier.

Nguyên nhân của việc xây dựng hệ thống với ObjectQualifier là để chúng ta có thể quản lý nhiều modules và các project khác nhau một cách độc lập nhưng lại có thể giao tiếp với nhau một cách rõ ràng. Đặc điểm này cho phép xây dựng những hệ thống có ưu thế về quản lý mã nguồn.

3. 3. Gắn kết các thành phần vào một Solution cho phát triển ứng dụng

School@net

32 nhận xét:

Nặc danh nói...

I absolutely love your website.. Excellent colors & theme.
Did you make this amazing site yourself? Please reply back as I'm attempting to create my own website and would like to learn where you got this from or exactly what the theme is called. Appreciate it!

Also visit my web page ... william shakespeare quotes

Nặc danh nói...

We're a gaggle of volunteers and starting a brand new scheme in our community. Your site provided us with valuable information to work on. You've performed a formidable process and
our whole group shall be thankful to you.

My blog: letting go quotes

Nặc danh nói...

I like looking through an article that can make men and women
think. Also, thanks for permitting me to comment!


My website: understanding quotes

Nặc danh nói...

Hey there would you mind letting me know which webhost you're using? I've loaded your
blog in 3 completely different internet browsers and I must say this blog loads a lot
faster then most. Can you suggest a good internet hosting provider at
a reasonable price? Cheers, I appreciate it!

my web site :: e. e. cummings quotes

Nặc danh nói...

I am no longer sure where you're getting your information, but good topic. I needs to spend a while finding out more or working out more. Thanks for wonderful information I used to be searching for this information for my mission.

Take a look at my web blog best quotes ever

Nặc danh nói...

Remarkable! Its genuinely amazing post, I have got much clear idea concerning from
this article.

Also visit my homepage; william shakespeare quotes

Nặc danh nói...

When I initially commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get several emails with the same comment.
Is there any way you can remove me from that service?

Cheers!

Feel free to surf to my website: feelings quotes

Nặc danh nói...

It's actually a cool and useful piece of information. I'm happy that you just
shared this useful info with us. Please stay us informed like this.
Thank you for sharing.

my web blog; hatred quotes

Nặc danh nói...

you're in point of fact a excellent webmaster. The web site loading velocity is incredible. It kind of feels that you're doing any distinctive trick.
Furthermore, The contents are masterpiece.
you have done a great process in this subject!

my homepage; discipline quotes

Nặc danh nói...

This is a topic that is close to my heart.
.. Cheers! Exactly where are your contact details though?


My web page; douglas adams quotes

Nặc danh nói...

Hello! Would you mind if I share your blog with my zynga group?

There's a lot of folks that I think would really appreciate your content. Please let me know. Many thanks

Here is my page rolling stones songs

Nặc danh nói...

It is appropriate time to make some plans for the future and it's time to be happy. I have read this post and if I could I desire to suggest you some interesting things or advice. Perhaps you can write next articles referring to this article. I want to read more things about it!

Also visit my website; future quotes

Nặc danh nói...

Wonderful, what a web site it is! This weblog presents valuable information
to us, keep it up.

Check out my page ... stephen hawking quotes

Nặc danh nói...

Keep on writing, great job!

Also visit my web page: amazing quotes

Nặc danh nói...

Very nice post. I just stumbled upon your blog and wanted to say that I
have really enjoyed surfing around your blog posts.
In any case I'll be subscribing to your rss feed and I hope you write again soon!

Here is my weblog; thanks quotes

Nặc danh nói...

Have you ever thought about adding a little bit more than just your
articles? I mean, what you say is valuable and everything.
Nevertheless think about if you added some great visuals or videos to give your posts more, "pop"!

Your content is excellent but with images and video clips, this blog could definitely be one of the most beneficial in
its niche. Wonderful blog!

Also visit my web site commitment quotes

Nặc danh nói...

Incredible points. Outstanding arguments. Keep up the good spirit.


Feel free to visit my site nora ephron quotes

Nặc danh nói...

These are truly impressive ideas in regarding blogging.

You have touched some fastidious factors here. Any way keep
up wrinting.

Feel free to surf to my page - unusual animals

Nặc danh nói...

We stumbled over here coming from a different web address and thought
I might as well check things out. I like what I see so now i'm following you. Look forward to exploring your web page again.

Visit my site depressing quotes

Nặc danh nói...

Hi there, You have done a great job. I'll definitely digg it and personally recommend to my friends. I'm confident they'll be benefited from this site.

Also visit my blog: teddy roosevelt quotes

Nặc danh nói...

For most recent information you have to pay a quick visit web and on world-wide-web I found this site as a most excellent site for most
recent updates.

Take a look at my homepage; zayn malik quotes

Nặc danh nói...

Write more, thats all I have to say. Literally, it seems as though you relied
on the video to make your point. You clearly know what youre talking about, why throw
away your intelligence on just posting videos to your site when
you could be giving us something informative to read?


Feel free to visit my web-site; depressing quotes

Nặc danh nói...

I have been browsing online more than 3 hours today, yet I never found any interesting article like yours.
It is pretty worth enough for me. In my view, if all web owners and bloggers made good content as you did, the net will be much
more useful than ever before.

my web blog; stephen hawking quotes

Nặc danh nói...

This post will help the internet viewers for creating new weblog or
even a weblog from start to end.

Here is my web site: commitment quotes

Nặc danh nói...

I seriously love your blog.. Pleasant colors & theme. Did you make this website yourself?

Please reply back as I'm trying to create my own blog and want to find out where you got this from or what the theme is called. Many thanks!

my webpage - genghis khan quotes

Nặc danh nói...

Greetings! I know this is somewhat off topic but I was wondering which blog platform are
you using for this website? I'm getting fed up of Wordpress because I've had issues with hackers and I'm looking at options for another platform. I would be awesome if you could point me in the direction of a good platform.

Feel free to surf to my website country flags

Nặc danh nói...

Hey there! This is my 1st comment here so I just wanted to give
a quick shout out and say I really enjoy reading your posts.
Can you recommend any other blogs/websites/forums that deal with the
same topics? Thanks!

Look into my blog; tired quotes

Nặc danh nói...

Hi there! This is kind of off topic but I need some help from an established blog.
Is it hard to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about creating my own
but I'm not sure where to begin. Do you have any points or suggestions? Cheers

Also visit my blog post: kahlil gibran quotes

Nặc danh nói...

Heya i am for the primary time here. I found this board and I find
It truly useful & it helped me out much. I hope to give something again and help others
like you aided me.

Feel free to surf to my web page: e. e. cummings quotes

Nặc danh nói...

Hi there to all, how is all, I think every one is getting more from this website, and your views are pleasant designed for new people.


Look into my web page - sylvia plath quotes

Nặc danh nói...

Heya i am for the first time here. I came across this board and I in finding It
really useful & it helped me out much. I'm hoping to offer something back and aid others like you helped me.

Here is my blog ... sylvia plath quotes

Nặc danh nói...

Do you mind if I quote a couple of your posts as long as I provide credit
and sources back to your weblog? My blog is in
the very same niche as yours and my visitors would definitely benefit from a lot of the
information you present here. Please let me know if this ok
with you. Cheers!

Here is my web site; country flags