我有這樣的IoC級:UnityContainer解決新問題,例如
public static class IoC
{
private static IUnityContainer container;
private static void setupIoC()
{
container = new UnityContainer();
container.RegisterType<MessageContext>(
new InjectionConstructor(
new DatabaseRepository<Message>(new RepositoryConfig() {AutoDetectionEnabled = false})));
}
public static T Resolve<T>()
{
if (container == null)
{
setupIoC();
}
return container.Resolve<T>();
}
}
在我的ViewModel我有:
public MessageViewModel()
: base(Resources.MENU_BAR_COREDATA_MESSAGE)
{
msgContext = IoC.Resolve<MessageContext>();
}
的msgContext是實體框架抽象...如果我加載用戶控件不止一次,存儲庫在DbSet.Local中有一些條目。如果我寫
public MessageViewModel()
: base(Resources.MENU_BAR_COREDATA_MESSAGE)
{
msgContext = new MessageContext(new DatabaseRepository<Message>(new RepositoryConfig(){AutoDetectionEnabled = false}));
}
我總是沒有任何DbSet.Local條目等一個全新的msgContext ......在我看來,它是我的國際奧委會並沒有給我一個新的instace當我解決這個問題的一個指標。我使用的UnityContainer和文件說,它總是會默認返回一個新的實例...
所以我不知道爲什麼它不工作,因爲我所期望的。
嘿Makubex,謝謝你的鏈接。但是我在{AutoDetectionEnabled = false}時遇到了一些問題,應該如何寫?如果我使用typeof(RepositoryConfig),我無法設置AutoDetection ... – GrayFox