LINQ to SQL實現資料訪問通用基類(續)

來源:互聯網
上載者:User

在我們基於Domain驅動模式開發物件導向的多層架構的時候,層和層之間資料的傳輸對象(DTO)往往 簡化為領域物件模型,在上文中就是我們利用LINQ TO SQL對象設計器產生的Bill,Customer等實體類。

存在的問題

通常的做法,我們把這些實體類單獨分成一層,這樣程式分層劃分成如下:

資料訪問層(Data Access Layer)

業務層 (Business Layer)

使用者介面層(UI Layer)

實體層(Entity Layer)

注意,多層應用程式,一般遵守這樣的規則:UI調用BL,BL調用DL,不能跨級調用,也不能底層調用 上層,但是實體層是我們的DTO,各個層都可以調用它。

如果這樣劃分,我們系統就出現問題:

我們把RepositoryBase划到資料層,LINQ TO SQL對象設計器產生的類分到實體層,資料層引用實體層 ,沒有問題,但是我們看到,在我們實體層,存在一個方法(以Bill為例):

public static RepositoryBase<Bill, LINQtoSQLHelper.DataContextSetUp> CreateRepository() { return new BillRepository(); }

資料層也必須引用資料訪問層,循環參考。另外,對於如何體現loadoption那?

解決循環參考

以Bill實體為例,循環參考的病因在於Bill實體中添加這樣一個方法:

public static RepositoryBase<Bill, LINQtoSQLHelper.DataContextSetUp> CreateRepository() { return new BillRepository(); }

為什麼要添加這個方法那?這樣做就是充血模式的實體了。注意到RepositoryBase.cs有一段代碼:

try
{
    object Repository = association.OtherType.Type.GetMethod("CreateRepository").Invoke (null, null);
    Repository.GetType().GetMethod("IterateEntitySet",
                                 BindingFlags.NonPublic | BindingFlags.Instance).Invoke(
        Repository,
        new object[4]
        {
            AssociationProperty.GetValue(theEntity, null),
            context,
            OperationMode,
            Recursively
        }
        );
}
catch (System.Reflection.TargetInvocationException e)
{
    throw (e.InnerException);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.