December 26, 2010

Data Transfer Object使用心得及時機

Data Transfer Object (DTO)一詞最早出現於何處筆者並不確定,但大部份對DTO的研究常會參考Martin Folwer的著作Patterns of Enterprise Application Architecture其中Data Transfer Object章節。事實上,許多開發人員可能早就已經使用它而不自知,以下是筆者在使用DTO上的心得

December 25, 2010

泛型Singleton Pattern

關於Singleton Pattern,可參考GoF寫的Design Patterns一書,中文版書名為物件導向設計模式,在此不詳述。當我們要讓某個類別具有singleton能力時,我們可能會透過以下程式碼來實現
public sealed class GoFSingleton
    {
        private static readonly GoFSingleton _instance = new GoFSingleton();

        private GoFSingleton()
        {
        }

        public static GoFSingleton Instance
        {
            get
            {
                return _instance;
            }
        }
    }
接下來即可在程式碼中以GoFSingleton gof = GoFSingleton.Instance;呼叫singleton物件

如果系統小,用到singleton可能只有一兩支class,那麼以上的singleton程式碼就只會有一兩支code duplication出現在系統內。
但當系統需要大量使用到singleton pattern時註1,duplication的情況就會比較多。以下提供一範例可使用泛型來實作出Singleton Pattern
public sealed class Singleton<T> where T : class, new()
    {
        private static readonly T _instance = new T();

        private Singleton()
        {
        }

        public static T Instance
        {
            get
            {
                return _instance;
            }
        }
    }
使用方式:SingletonObject obj = Singleton<SingletonObject>.Instance;

範例程式下載:SingletonPatternSample.zip

備註

1.關於Singleton Pattern濫用,Joshua Kerievsky在其著作Refactoring to Patterns(中文書名為重構-向範式前進)中Inline Singleton章節有詳細的說明

2.有關Singleton Pattern於threading的議題,可參考網友內向人推薦的好文Implementing the Singleton Pattern in C#

December 16, 2010

好用的error logging framework - ELMAH

看到RiCo兄一文介紹elmah這支error logging framework,覺得還蠻不錯的,自己也安裝來玩看看。elmah能將所有exception都log起來,除了可以把整個例外訊息log成XML,或存在資料庫中,也可以將訊息以email寄給管理者。

關於如何安裝elmah,可參考Introductory article by Simone BusoliLogging Error Details with ELMAHELMAH - Error Logging Modules And Handlers三篇文章,以下為試用後的一些心得

December 1, 2010

NHibernate - 外部設定檔範例

前篇文章說明了NHibernate的設定檔設定方式,將設定細節寫在Web.config或App.config中,然而將NHibernate設定置於Web.config/App.config中可能會造成設定檔過於肥大,因為可能還有其它framework的設定檔,本篇文章將說明以外部設定檔的方式設定NHibernate,將細部設定自Web.config/App.config抽離。

 1.於專案中建立hibernate.cfg.xml檔案[註1]並將原本置放在*.config中hibernate-configuration區段移到hibernate.cfg.xml,並將其檔案屬性內的複製到輸出目錄設為永遠複製,因為NHibernate預設會讀取bin下的hibernate.cfg.xml,若不將hibernate.cfg.xml一同輸出至bin裡,NHibernate將讀取不到設定檔。


hibernate.cfg.xml 檔案內容如下
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
        <property name="dialect">NHibernate.Dialect.PostgreSQL82Dialect</property>
        <property name="connection.connection_string">Server=10.0.0.10;Port=5432;Database=dbname;User ID=username;Password=password;</property>
        <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
        <mapping assembly="NHibernateTest.Data"/>
    </session-factory>
</hibernate-configuration>
2.在呼叫任何NHibernate功能前於程式碼中加入以下片段
Configuration cfg = new Configuration();
cfg.Configure();
經由以上設定即可完成NHibernate設定。如果我們不想每次都要將hibernate.cfg.xml  輸出至bin呢?NHibernate也提供指定設定檔位置的方式讀取設定檔。假設hibernate.cfg.xml 置放在web ap根目錄下,我們可將程式碼修改為以下片段,即可讀取設定檔資訊[註2]
Configuration cfg = new Configuration();
cfg.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));

備註
1.名稱需固定為hibernate.cfg.xml,否則run time會出現找不到設定檔的錯誤訊息
2.此時設定檔名稱可自訂