2

How to set up IDbConnectionFactory to be autowired/injected when not inheriting Service?

I some repository class that will be used in another repository class, but not inheriting from the Service class. it look like this:

public class UsersControlSettingsRepository
{
    //Property should be injected, but still null
    public IDbConnectionFactory Conn { get; set; } 
    public UsersControlSettings GetUsersSettings()
    {
        using (var _db = Conn.Conn.OpenDbConnection())
        {
            return _db.Select<UsersControlSettings>()
                .OrderByDescending(u => u.Id).FirstOrDefault();
        }
    }
}

And this is the apphost config:

var conn = new OrmLiteConnectionFactory(
    ConfigurationManager.ConnectionStrings["BlaBla"].ConnectionString, 
    MySqlDialect.Provider);

container.Register<ICacheClient>(new MemoryCacheClient());
Container.RegisterValidators(typeof(MainServices).Assembly);

container.Register<IDbConnectionFactory>(c => conn);

container.Register(c => new UsersControlSettingsRepository()
{
    Conn = c.TryResolve<IDbConnectionFactory>()
});

And then, I use the class like this:

public class AuthExtended : CredentialsAuthProvider
{
    private readonly Md5 _hashing;
    private Users _user;
    private UsersControlSettings _userSettings;

    private readonly UsersRepository _userRepository;
    private readonly UsersControlSettingsRepository _controlSettings;
    public AuthExtended()
    {
        _hashing = new Md5();
        _userSettings= EndpointHost.AppHost
            .TryResolve<UsersControlSettingsRepository>();
    }

Am I doing Something Wrong in here?

Steven
  • 166,672
  • 24
  • 332
  • 435
reptildarat
  • 1,036
  • 2
  • 18
  • 35
  • 1
    How are you creating your `UsersControlSettingsRepository`? If you are just new'ing one up then you will not have anything injected, you should be resolving it using the container. i.e. `EndpointHost.AppHost.TryResolve();` – Mike Sep 16 '13 at 16:19
  • Yes, I tried both of them and no luck, I update my question. Thanks. – reptildarat Sep 16 '13 at 23:29

1 Answers1

2

Looking at your examples I believe the AuthExtended class is being created before you have registered your various classes with the container. If the AuthExtended constructor is called before the UserControlSettingsRepository has been registered it will not be resolved. Check the Configure method of your AppHost to see if you are creating the provider before you register your repository.

Here's a quick 1-file ServiceStack console application to demonstrate:

public interface IFakeDbConnectionFactory
{
    string Name { get; set; }
}
public class FakeDbConnectionFactory : IFakeDbConnectionFactory
{
    public string Name { get; set; }
}

public class UsersControlSettingsRepository {
    public IFakeDbConnectionFactory Conn { get; set; }        
}

public class AppHost : AppHostHttpListenerBase
{
    public AppHost() : base("Test App", typeof (AppHost).Assembly) {}

    public override void Configure(Container container)
    {
        var conn = new FakeDbConnectionFactory { Name = "TestDB1"};
        container.Register<IFakeDbConnectionFactory>(c => conn);

        container.Register(c => new UsersControlSettingsRepository {
            Conn = c.TryResolve<IFakeDbConnectionFactory>()
        });
    }
}

class Program
{
    static void Main(string[] args)
    {
        var app = new AppHost();
        app.Init();
        app.Start(@"http://+:8085/");
        var repo = EndpointHost.AppHost.TryResolve<UsersControlSettingsRepository>();
        Console.WriteLine(repo.Conn.Name);
        Console.ReadLine();
    }
}

Here's an example of an app host that registers the database information and then ceratse the authentication provider:

public class MyApphost : AppHostHttpListenerBase
{
    public MyApphost() : base("Service Name", typeof(MyApphost).Assembly) {}

    public override void Configure(Container container)
    {
        // Register database connection before creating a provider...
        var conn = new OrmLiteConnectionFactory(ConfigurationManager.ConnectionStrings["BlaBla"].ConnectionString, MySqlDialect.Provider);

        Container.Register<ICacheClient>(new MemoryCacheClient());
        Container.RegisterValidators(typeof(MainServices).Assembly);
        Container.Register<IDbConnectionFactory>(c => conn);
        Container.Register(c => new UsersControlSettingsRepository() {
            Conn = c.TryResolve<IDbConnectionFactory>()
        });

        // Setup authentication
        Plugins.Add(new AuthFeature(
            () => new AuthUserSession(),
            new IAuthProvider[] { new CustomCredentialsAuthProvider()}
        ));
    }
}
Mike
  • 1,837
  • 10
  • 9