2012-05-25 52 views
0

我想要得到一個非常簡單的運行StructureMap的裝飾模式版本,但我無法使它工作。這裏是我的代碼(注意斷點):EnrichWith沒有運行

public interface ITestClass { void DoSomething(); } 

public class TestClass : ITestClass 
{ 
    public void DoSomething() 
    { 
     Console.WriteLine("Doing something"); //Breakpoint 
    } 
} 

public class LoggingTestClass : ITestClass 
{ 
    private ITestClass originalClass; 

    public LoggingTestClass(ITestClass original) 
    { 
     originalClass = original; //Breakpoint 
    } 
    public void DoSomething() 
    { 
     Console.WriteLine("Log start"); 
     originalClass.DoSomething(); 
     Console.WriteLine("Log finish"); 
    } 
} 

而且在我的註冊表:

For<ITestClass>().Use<TestClass>(). 
EnrichWith(original => new LoggingTestClass(original)); 

最後一個測試:

[TestMethod] 
    public void DoSomeTesting() 
    { 
     using (IContainer container = new Container(new ApiRegistry())) 
     { 
      ITestClass testClass = container.GetInstance<TestClass>(); //Breakpoint 
      testClass.DoSomething(); 
     } 
    } 

當我調試測試,我命中斷點在測試中,然後是DoSomething()方法中的斷點。 LoggingTestClass的構造函數永遠不會被執行。

我不知道我怎麼能簡化它再向下,似乎EnrichWith根本就不是被稱爲...

+0

剛剛檢查,我得到與 相同的問題對於()。使用().OnCreation(x => x.DoSomething()); DoSomething方法只被測試調用,而不是OnCreation –

回答

0

唉唉,看來我是紀念碑式的愚蠢!我正在使用GetInstance<TestClass>而不是GetInstance<ITestClass>。這小小的一直讓我忙了好幾個小時!去談談你的商界人士。