2011-08-31 33 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; } 
        } 
    }