为简化起见,我有一个AppSettings:IAppSettings类,该类在应用程序启动时初始化:
ObjectFactory.Initialize(x =>
{
x.Scan(scan => {
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
x.AddRegistry<DataAccessRegistry>();
x.AddRegistry<AuthorizationRegistry>();
x.For<IAppSettings>()
.Use<AppSettings>()
.Ctor<Dictionary<string, string>>()
.Is(ctx =>
{
Dictionary<string, string> dict = new Dictionary<string, string>();
var sessionFactory = ctx.GetInstance<ISessionFactory>();
using (var session = sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
// query the SETTINGS table
foreach (Setting _setting in session.QueryOver<Setting>().List())
{
dict[_setting.Key] = _setting.Value;
}
tx.Commit();
}
return dict;
});
x.UseDefaultStructureMapConfigFile = false;
});
因此,当创建需要设置的类时,我只需使用以下内容
public class AccessDataService {
private IAppSettings settings;
public AccessDataService(IAppSettings p_appSettings) {
settings = p_appSettings;
}
....
}
和structuremap很好地将对象注入到我的类实例中。
但是,我希望能够显式触发数据库设置的重新加载,所以我想知道是否有一种方法可以告诉Structuremap清除AppSettings的所有现有对象并重新创建它们,然后再次从数据库加载设置?
干杯
最佳答案
您可以在每次需要刷新时使用EjectAllInstancesOf方法:
ObjectFactory.EjectAllInstancesOf<IAppSettings>();
ObjectFactory.Inject<IAppSettings>(*your new object*);
关于c# - 重新加载StructureMap对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31650843/