0
votes

I'm on my first steps with MVC stuff. I'm just trying to create a simple application using MVC2 + Windsor Castle (and NHibernate too).

I'm having a problem with repositories... seems that these repositories are not being registered and I'm a bit lost. After read tons of web sites looking for info I decided to ask here. Let's see the code.

Regarding repositories, class hierarchy:

public interface ICommonRepository{}

also

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

}

and

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

       throw new NotImplementedException();
    }


}

Regarding my service:

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

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

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

and finally my "windsor installer" that trys to register all stuff created on the application

 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()));

    }
}

So... when I try to request somethin on the BookController I get following error:

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.

Funny thing is that Repository appears on the "Potentially Misconfigured Components" so I'm not sure if this repository are not being added or just the error is in other place.

Any ideas?

Thanks for your time

Best regards

1

1 Answers

0
votes

It's all in the exception message. No component was registered exposing IRepository<Book>.

Probably the way you're registering your repositories is not what you intended. Have a look at the documentation.