2016-03-07 68 views
2

我有一個對象,表示我的數據庫中的表的記錄,例如'Project'。調試和屬性加載

我的用戶類具有不同的屬性,這些屬性是其他表的記錄,例如'客戶'或'會計'。那些也有相關表格的屬性。

這些屬性中的每一個都返回一個本地值(已經加載),如果不爲空,並且沒有加載的信息,它會生成一個從數據庫中獲取該值的請求。

我的問題如下:當我設置斷點並在調試窗口中檢查對象時,它會自動加載屬性的所有值,因此請求數據庫。

在這種情況下,我目前無法獲得對象的精確和靜態快照。

有沒有一種方法,在代碼中,如果在調試窗口中不經過這部分代碼? 舉例來說,這樣的事情:

public MyBaseObject GetProperty<T>(string columnName_, string alias_ = null) where T : MyBaseObject, new() 
{ 
    var ret = GetExtract<T>(columnName_, alias_); 

    // if the data are loaded 
    if (ret.Id != null) 
     return ret; 

    // Fake boolean I would like 
    if(InDebugWindowAfterAbreakPointForInstance) 
     return ret; 
    else 
     ret = LoadFromDatabase<T>(columnName_, alias_) 
    return ret; 
} 

我發現不同的屬性與調試器,像DebuggerStepperBoundaryAttribute,但沒有什麼可以做類似的東西。

+0

爲什麼用C#和D標記這個標記?請刪除您未使用的語言標籤。 –

回答

0

在這樣的情況下,我知道的唯一方法是對每個類型使用DebuggerTypeProxy,然後在該代理中直接訪問支持字段,而不是通過導致數據庫查找發生的屬性。

這是一個簡單的示例程序。

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     var client = new Client(); 
     Debugger.Break(); 
     Debugger.Break(); 
    } 
} 

[DebuggerTypeProxy(typeof(ClientDebugView))] 
public class Client : MyBaseObject 
{ 
    private string _firstName; 
    private string _lastName; 

    public string FirstName 
    { 
     get 
     { 
      if (_firstName == null) 
       _firstName = GetProperty<string>("FirstName"); 

      return _firstName; 
     } 
     set 
     { 
      if (Equals(_firstName, value)) 
       return; 
      _firstName = value; 
      UpdateDatabase(_firstName, "FirstName"); 
     } 
    } 

    public string LastName 
    { 
     get 
     { 
      if (_lastName == null) 
       _lastName = GetProperty<string>("LastName"); 

      return _lastName; 
     } 
     set 
     { 
      if (Equals(_lastName, value)) 
       return; 
      _lastName = value; 
      UpdateDatabase(_lastName, "LastName"); 
     } 
    } 

    internal class ClientDebugView : MyBaseObjectDebugView 
    { 
     private readonly Client _client; 

     public ClientDebugView(Client client) 
      : base(client) 
     { 
      _client = client; 
     } 

     public string FirstName 
     { 
      get { return _client._firstName; } 
     } 

     public string LastName 
     { 
      get { return _client._lastName; } 
     } 
    } 
} 

[DebuggerTypeProxy(typeof(MyBaseObjectDebugView))] 
public class MyBaseObject 
{ 
    private Guid? _id; 

    public Guid? Id 
    { 
     get 
     { 
      if (_id == null) 
       _id = GetProperty<Guid?>("Id"); 

      return _id; 
     } 
     set 
     { 
      if (Equals(_id, value)) 
       return; 
      _id = value; 
      UpdateDatabase(_id, "Id"); 
     } 
    } 

    //Fake loading data from a database. 
    protected T GetProperty<T>(string columnName) 
    { 
     object ret = null; 
     switch (columnName) 
     { 
      case "Id": 
       ret = Guid.NewGuid(); 
       break; 
      case "LastName": 
       ret = "Smith"; 
       break; 
      case "FirstName": 
       ret = "John"; 
       break; 
      default: 
       ret = null; 
       break; 
     } 

     return (T)ret; 
    } 

    protected void UpdateDatabase<T>(T id, string s) 
    { 
     throw new NotImplementedException(); 
    } 

    internal class MyBaseObjectDebugView 
    { 
     private readonly MyBaseObject _baseObject; 

     public MyBaseObjectDebugView(MyBaseObject baseObject) 
     { 
      _baseObject = baseObject; 
     } 

     public Guid? Id 
     { 
      get { return _baseObject._id; } 
     } 
    } 
} 

如果您查看在調試器中client對象,你會看到它留下的後盾領域null的兩個斷點之間,除非你打開「原始視圖」在第一個斷點。

enter image description here