不继承服务时如何将 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>();
}
我在这里做错了吗?