2012-03-16 84 views
0

對不起我的愚蠢,但我正在尋找一種方法來構建一個對象從一個類連接到一個服務,還包含另一個類與服務的方法。C#參考父母

我面臨的問題是試圖找出一種方法,只需要連接到服務一次。我拒絕使用全局變量和類似的東西。

我很難理解C#中的對象概念,因爲我的編程背景主要來自JavaScript。

任何幫助最受讚賞。

namespace Tests.Classes 
{ 
    public class L 
    { 
     dynamic uri; 
     dynamic service; 
     dynamic credentials; 
     dynamic proxy; 

     public L L() 
     { 
      this.uri = new Uri("bladie bladie blah"); 
      this.credentials = new ClientCredentials(); 
      this.credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials; 
      this.proxy = new OrganizationServiceProxy(this.uri, null, this.credentials, null); 
      this.service = (IOrganizationService)this.proxy; 
      return this; 
     } 

     public class OL 
     { 
      public OL OL(dynamic a) 
      { 
       this.service = parent.service; // <- doesn't compile 
       return this; 
      } 
     } 
    } 
} 

要清楚它是如何叫:

var l = new L(); 
l.OL("haha"); 

也許我的代碼是不太清楚。這將使分類狂熱分子保持在海灣:P。

namespace Tests.Classes 
{ 
    public class L 
    { 
     Uri uri; 
     IOrganizationService service; 
     ClientCredentials credentials; 
     OrganizationServiceProxy proxy; 

     public L L() 
     { 
      this.uri = new Uri("hBladie Bladie Blah"); 
      this.credentials = new ClientCredentials(); 
      this.credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials; 
      this.proxy = new OrganizationServiceProxy(this.uri, null, this.credentials, null); 
      this.service = (IOrganizationService)this.proxy; 

      return this; 
     } 

     public class OL 
     { 
      Entity entity = new Entity(); 
      IOrganizationService service = null; 

      public OL OL(dynamic a) 
      { 
       if (a is Entity) 
       { 
        this.entity = a; 
       } 
       if (a is string) 
       { 
        this.entity = new Entity(a); 
       } 

       return this; 
      } 

      public OL attr(dynamic key, dynamic value) 
      { 
       this.entity[key] = value; 
       return this; 
      } 

      public Boolean save() 
      { 
       this.parent.service.create(this.entity); // parent does not exist 
      } 
     } 
    } 
} 

我討厭凌亂的編程,我喜歡jQuery風格。

下面的代碼如何,必須使用:

new L().OL("haha").attr("Hello", "world").save(); 

var l = new L(); 
l.OL("HAHA").attr("foo", "bar").save(); 
l.OL("pff").attr("boppa", "deebop").save(); 
+0

首先,用'var'替換'dynamic'。你不需要動態的。 – Jason 2012-03-16 09:07:51

+0

目前還不清楚你想要什麼'parent',或者爲什麼你所有的變量都是使用'dynamic'聲明的... – 2012-03-16 09:08:43

+0

不回答我的問題,但沒問題 – Chris 2012-03-16 09:08:58

回答

1

這將在Java中工作。在C#中,你需要爲L傳遞給OL的構造函數:

public OL(dynamic O, L parent) 
{ 
    this.service = parent.service; 
} 

順便說一句,你的構造將不能編譯,你有兩個OL那兒,和return

順便說一下2,爲什麼你使用這麼多的動態?

+0

純粹的例子..動態,因爲他們總是工作:D(我來自JavaScript背景,一切都是動態的) – Chris 2012-03-16 09:15:08

+1

「因爲他們總是工作」。不,他們總是編譯。發生錯字或其他錯誤,他們將無法正常工作。 :) – 2012-03-16 09:37:43

0

不要somethign這樣的:

public class OL 
{ 
    dynamic olService = nnull; 
    public OL OL(dynamic a) 
    { 
      this.olService = parent.service; // <- doesn't compile 
      //return this; //remove this !!!! 
    } 
} 

編輯

請注意:儘量不要使用dynamic,直到你真的需要這個。儘可能使用C#語言中的強類型。

0

如果您希望單個service存在於您的類的所有實例中,請將其聲明爲static。例如:

static IOrganizationService service; 

然後在你的嵌套類中,你可以通過L.service來引用它。

+0

如何在靜態時更改其值? – Chris 2012-03-16 09:38:30

+0

@Chris,我在我的回答中提到你可以用'L.service'來引用它。例如:'L.service = ...;' – 2012-03-16 10:35:43