2011-10-24 59 views
2

我正在處理生成數據庫訪問代碼並使用靜態對象進行用戶標識的桌面應用程序。將用戶信息傳遞到數據訪問

現在我們需要通過webservice公開一些邏輯,我們正在尋找最少侵入性的形式來將用戶信息沿着管道推送到數據庫訪問類。

我們來到這了是一個委託傳遞給插入/更新方法,看起來像這樣:

public delegate string GetLogin(); 

public class BaseEntity : BaseNotifiableEntity, System.ComponentModel.IDataErrorInfo 
{ 
    public GetLogin Login { get; set; } 
    (...) 
} 

public static class BaseEntityHelper 
{ 
    public static SqlCommand buildUpdateCommand(BaseEntity entity) 
    { 
     UpdateDefaultValues(entity, false); 
     (...) 
    } 

    public static void UpdateDefaultValues(BaseEntity entity, bool affectCreationFields) 
    { 
     if (entity.Login == null && AppServer.RunningApplication.CurrentUser == null) 
      throw new Exception("Something went wrong"); 

     (...) 
    } 
} 

因此,在我們的邏輯將會有這樣的事情:

public class Service 
    { 
     T_DIST_Service record; 
     (...) 

     public bool Update(DataAccess.Base.GetLogin login) 
     { 
      record.Login = login; 
      (...) 
      record.Update(); 
     } 
    } 

這當然涉及到改變應用程序中的很多方法。 所以我想知道是否有無縫的方式來完成這個使用依賴注入(例如)。

也許你們中的一些人已經走上了這條路,並有一些見解分享。 謝謝你的時間。

編輯1: 使用.NET

+0

哪個平臺? 。淨? –

+0

我的不好,這是.NET的平臺 –

回答

2

在架構層面,它聽起來就像我說,你正試圖把邏輯不屬於那裏的數據訪問層英寸一個數據訪問組件應該只是一個anti-corruption layer,所以任何邏輯理想情況下都應該在調用層中實現。

但是,如果您想在此處立即找到更直接的解決方法,最好使用內置的Thread.CurrentPrincipal環境上下文。

如果您有用戶對象必須攜帶的特殊信息,則可以使用自定義實現IPrincipal來創建自定義User Context