gpt4 book ai didi

c# - 如何在不继承服务时将 IDbConnectionFactory 设置为 Autowiring /注入(inject)?

转载 作者:太空宇宙 更新时间:2023-11-03 10:57:43 25 4
gpt4 key购买 nike

如何设置 IDbConnectionFactory 在不继承 Service 时 Autowiring /注入(inject)?

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

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

这是应用主机配置:

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

我是不是做错了什么?

最佳答案

查看您的示例,我相信 AuthExtended 类是在您向容器注册各种类之前创建的。如果在 UserControlSettingsRepository 注册之前调用了 AuthExtended 构造函数,它将不会被解析。检查 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()}
));
}
}

关于c# - 如何在不继承服务时将 IDbConnectionFactory 设置为 Autowiring /注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18828646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com