2011-08-31 39 views
8

這應該很簡單,但我找不到任何東西。使用合成在C#中自動生成包裝類

我有一個類在一個組件(共享庫 - 它是一個Web服務的一組代理類) 我在另一個組件(網絡工程)一類

有一類被稱爲「配置文件「,它位於代理程序集中。 有一組類在Web項目中「使用」配置文件。 當沒有用戶登錄時,使用GenericProfile。

遵循「關注點分離」的原則.... 代理程序集由其他項目使用,並只涉及Web服務的東西。 該網站的項目只是網上的東西在那裏

但是,現在有這種需要一個「GenericProfile」 - 認爲它是「訪客用戶」。

合乎邏輯的做法是構建一個名爲IProfile的接口,並使兩個類都從它派生出來。但是這會在兩個程序集之間創建循環依賴關係。

第二個最好的想法是創建一個名爲MyInterfaces的第三個程序集,並將IProfile放在那裏 - 但這導致違反了我認爲的分離關注原則。至少,這個問題的一個例子似乎是我的解決方案中創建額外模塊的一個很小的原因。

進入包裝類 - 或複合包裝類(不管你怎麼稱呼它)

我要找的東西,最終產生這樣的下面。是否有工具或Visual Studio擴展可以做到這一點?也許.tt文件?

namespace WebProject 
{ 
    public interface IProfile 
    {...} 

    class MyWrapperClass : IProfile 
    { 
     Proxy.Profile _profile; 

     public MyWrapperClass(Proxy.Profile proxy) 
     { 
      _profile = proxy; 
     } 

     public string IProfile.Property1{ get { return _profile.Property1; } set { _profile.Property1 = value; } } 
     public string IProfile.Property2{ get { return _profile.Property2; } set { _profile.Property2 = value; } } 
     public string IProfile.Property3{ get { return _profile.Property3; } set { _profile.Property3 = value; } } 
    } 

} 
+1

它看起來像有人問了類似的東西... http://stackoverflow.com/ques tions/2150416 /生成傳遞代碼時,寧願組成 - 繼承 – 010110110101

+0

雖然我沒有ReSharper ... – 010110110101

+1

我會使用T4(就像你想的一樣),但我不知道是否已有完整的模板。但我認爲這樣的模板可以用反射很容易寫出來。 –

回答

0

如果我面對你原來的問題,我把IProfile在你的共享庫,旁邊的Profile類。然後,您的Web項目可以實現它需要的GenericProfile類,其他任何事情都不需要知道它,並且庫的其他客戶端可以根據需要執行相同的操作。這對測試庫也很有用。

2

我不完全理解你在做什麼,但下面是我將如何用ReSharper生成包裝類。

就個人而言,如果我的僱主不想爲ReSharper支付費用,我會購買它。它使我成爲一個更好的開發者。我強烈建議你考慮把它作爲你職業生涯的投資。反免責聲明 - 我完全沒有與ReSharper聯繫或贊助。

  1. 接口添加到類,你希望是包裝類

    class MyWebElement : IWebElement { } 
    

  • 查找/點擊 「委託實施的」 YourInterfaceHere「到一個新的字段 Delegate Implementation

  • 選擇選項 Delegate options

  • 單擊Finish,享受你的新類

    class MyWebElement : IWebElement 
    { 
        private IWebElement _webElementImplementation; 
        public IWebElement FindElement(By @by) 
        { 
         return _webElementImplementation.FindElement(@by); 
        } 
    
        public ReadOnlyCollection<IWebElement> FindElements(By @by) 
        { 
         return _webElementImplementation.FindElements(@by); 
        } 
    
        public void Clear() 
        { 
         _webElementImplementation.Clear(); 
        } 
    
        public void SendKeys(string text) 
        { 
         _webElementImplementation.SendKeys(text); 
        } 
    
        public void Submit() 
        { 
         _webElementImplementation.Submit(); 
        } 
    
        public void Click() 
        { 
         _webElementImplementation.Click(); 
        } 
    
        public string GetAttribute(string attributeName) 
        { 
         return _webElementImplementation.GetAttribute(attributeName); 
        } 
    
        public string GetCssValue(string propertyName) 
        { 
         return _webElementImplementation.GetCssValue(propertyName); 
        } 
    
        public string TagName 
        { 
         get { return _webElementImplementation.TagName; } 
        } 
    
        public string Text 
        { 
         get { return _webElementImplementation.Text; } 
        } 
    
        public bool Enabled 
        { 
         get { return _webElementImplementation.Enabled; } 
        } 
    
        public bool Selected 
        { 
         get { return _webElementImplementation.Selected; } 
        } 
    
        public Point Location 
        { 
         get { return _webElementImplementation.Location; } 
        } 
    
        public Size Size 
        { 
         get { return _webElementImplementation.Size; } 
        } 
    
        public bool Displayed 
        { 
         get { return _webElementImplementation.Displayed; } 
        } 
    }