2

不继承服务时如何将 IDbConnectionFactory 设置为自动装配/注入?

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

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

我在这里做错了吗?

4

1 回答 1

2

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

这是一个快速的 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()}
        ));
    }
}
于 2013-09-18T21:36:28.040 回答