1

I have a repository that implements MongoRepository which uses generics I'm trying to register the type in the container so far this is what I got:

public override void Configure(Container container)
{
    container.RegisterAutoWiredAs<UserRepository, 
        IUserRepository>();

    //Trying to wireup the internal dependencies
    container.RegisterAutoWiredType(
        typeof(MongoRepository<>),
        typeof(IRepository<>));
}

I tried RegisterAutoWiredAs<UserRepository> as well.

The problem is when I run the application I got the following error:

Error trying to resolve Service '{ServiceName}' or one of its autowired dependencies

I guess because I have registered the repository without registering the mongoDB repository as a dependency.

As far as I know funq doesn't support generics, but I'm not sure if that's the same scenario with ServiceStack.

the repo has the following definition:

public class UserRepository
    : MongoRepository<User>, IUserRepository
{
}

public interface IUserRepository : IRepository<User>
{   
}

Edit:

This is the service definition, actually pretty basic!

public class MyService : Service {
    private readonly IUserRepository _userRepository;

    public MyService(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public UserResponse Any(UserRequest userRequest)
    {
            //_userRepository.GetById(id)                
    }
}

DTOs:

public class UserRequest
{
    public string Id { get; set; }
}

public class UserResponse
{
    public User User { get; set; }
}
Steven
  • 166,672
  • 24
  • 332
  • 435
pedrommuller
  • 15,741
  • 10
  • 76
  • 126

1 Answers1

4

Funq works pretty intuitively, i.e. whatever you register you can resolve. So just make sure what you're trying to resolve, is the same thing as what you've registered.

There is no magic behavior in Funq, so this is an example of what doesn't work:

container.RegisterAutoWiredType(typeof(MongoRepository<>), typeof(IRepository<>));

If you want to be able to resolve MongoRepository<User> you need to register it, e.g:

container.RegisterAutoWiredAs<MongoRepository<User>,IUserRepository>();

Which you're then free to resolve like a normal dependency:

public class MyService : Service
{

    private readonly IUserRepository _userRepository;

    public MyService(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }
 }
mythz
  • 141,670
  • 29
  • 246
  • 390
  • I´m having problems trying to wire IRepository with MongoRepository – pedrommuller Jul 13 '13 at 02:59
  • @Pedro you're assuming magic behavior, where there is none in Funq - I've updated my answer to show an explicit registration. – mythz Jul 13 '13 at 04:20
  • thanks for the update! but I'm trying to resolve UserRepository which inherits from MongoRepository and I already have it registered, so I was thinking that it has to be else, because If I remove the mongoRepository from the Inheritance it works fine! – pedrommuller Jul 13 '13 at 05:05
  • mongoRepository has 2 constructors, I guess funq is selecting the greediest one, so I'm not providing the contectionString, the question now is how do I specify the constructor during the registration? – pedrommuller Jul 13 '13 at 06:38
  • @Pedro specify your own lambda initializer, e.g `container.Register(c => new UserRepository(c.Resolve))`. You're asking new questions already [covered in the IOC docs](https://github.com/ServiceStack/ServiceStack/wiki/The-IoC-container) – mythz Jul 13 '13 at 08:01
  • I figure it out, the problem was the constructor, choosing the right one did the trick, thanks a lot! – pedrommuller Jul 28 '13 at 01:30