Question

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; }
}
Was it helpful?

Solution

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;
    }
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top