2011-12-28 68 views
0

我在使用MVC的第一步。我只是試圖使用MVC2 + Windsor Castle(以及NHibernate)創建一個簡單的應用程序。MVC +溫莎城堡。版本庫沒有正確註冊

我遇到存儲庫問題...似乎這些存儲庫沒有被註冊,我有點迷路。讀了大量的網站尋找信息後,我決定在這裏問。讓我們看看代碼。

關於庫,類層次結構:

public interface ICommonRepository{} 

public interface IRepository<T> : ICommonRepository 
{ 
    IEnumerable<T> FindAll(); 

} 

public class BookRepository:IRepository<Book> 
{ 
    public IEnumerable<Book> FindAll() 
    { 

     throw new NotImplementedException(); 
    } 


} 

關於我的服務:

public class BookService:IService 
{ 
    private IRepository<Book> _bookRepository; 

    public BookService(IRepository<Book> repository) 
    { 
     this._bookRepository = repository; 
    } 

    public IEnumerable<Book>FindAllBooks() 
    { 
     return this._bookRepository.FindAll(); 
    } 
} 

終於我的「溫莎的安裝程序」是改掉註冊應用

public class Installer : IWindsorInstaller 
{ 
    public void Install(IWindsorContainer container, IConfigurationStore store) 
    {   
     //Repositories 
     container.Register(AllTypes.FromThisAssembly() 
          .BasedOn<IService>(ICommonRepository) 
          .If(t => t.Name.EndsWith("Repository")) 
          .Configure(c => c.LifestyleTransient())); 
     //Services 
     container.Register(AllTypes.FromThisAssembly() 
           .BasedOn<IService>() 
           .If(Component.IsInSameNamespaceAs<BookService>()) 
           .If(t => t.Name.EndsWith("Service")) 
           .Configure(c => c.LifestyleTransient())); 
     //Controllers 
     container.Register(AllTypes.FromThisAssembly() 
          .BasedOn<IController>() 
          .If(Component.IsInSameNamespaceAs<HomeController>()) 
          .If(t => t.Name.EndsWith("Controller")) 
          .Configure(c => c.LifestyleTransient())); 

    } 
} 

所以...創建的所有的東西,當我嘗試請求對BookController的事端我獲得以下錯誤:

Can't create component 'Example_MVC.Services.BookService' as it has dependencies to be satisfied.'Example_MVC.Services.BookService' is waiting for the following dependencies: - Service 'Example_MVC.Repositories.IRepository`1[[Example_MVC.Class.Book, Example_MVC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' which was not registered.

有趣的是,Repository出現在「潛在錯誤配置的組件」中,所以我不確定這個存儲庫是不是被添加的,或者只是錯誤在其他地方。

任何想法?

感謝您的時間

問候

回答

0

這一切都在異常消息。沒有組件註冊,暴露IRepository<Book>

或許您註冊存儲庫的方式並不是您想要的。 Have a look at the documentation

+0

非常感謝你爲這個鏈接,Krysztof。您可能會笑,但......我無法找到一個總結所有關於註冊的可能性的頁面。我改變了註冊倉庫到這個新倉庫的方式: container.Register(Classes.FromThisAssembly() .BasedOn(typeof(IRepository <>))。WithService.Base()); 現在一切正常。感謝您的幫助,併爲這個愚蠢的問題感到抱歉 – JTH 2011-12-29 09:17:35