如何设置IDbConnectionFactory在不继承Service时自动连接/注入?

时间:2013-09-16 12:56:34

标签: c# inversion-of-control servicestack funq

如何设置IDbConnectionFactory在不继承Service时自动连接/注入?

我将在另一个存储库类中使用某个存储库类,但不会从Service类继承。它看起来像这样:

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

这是apphost配置:

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

然后,我使用这样的类:

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

我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

查看您的示例我相信在您使用容器注册各种类之前,正在创建AuthExtended类。如果在AuthExtended注册之前调用UserControlSettingsRepository构造函数,则无法解析。在注册存储库之前,请检查AppHost的Configure方法,看看是否在创建提供程序。

这是一个快速的1文件ServiceStack控制台应用程序,用于演示:

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

以下是注册数据库信息的应用主机示例,然后然后了解身份验证提供程序:

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