(这个问题最初是在Ninject Google Group中提出的,但我现在看到Stackoverflow似乎更活跃了.)
我正在使用NamedScopeExtension将相同的ViewModel注入到View和Presenter中.发布View后,内存分析显示Ninject缓存仍保留ViewModel.如何让Ninject发布ViewModel?表单关闭和处置时释放所有ViewModel,但我使用表单中的Factory创建和删除控件,并希望将ViewModel垃圾收集到(收集Presenter和View).
有关问题的说明,请参阅以下UnitTest,使用dotMemoryUnit:
using System;
using FluentAssertions;
using JetBrains.dotMemoryUnit;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ninject;
using Ninject.Extensions.DependencyCreation;
using Ninject.Extensions.NamedScope;
namespace UnitTestProject
{
[TestClass]
[DotMemoryUnit(FailIfRunWithoutSupport = false)]
public class UnitTest1
{
[TestMethod]
public void TestMethod()
{
// Call in sub method so no local variables are left for the memory profiling
SubMethod();
// Assert
dotMemory.Check(m =>
{
m.GetObjects(w => w.Type.Is<ViewModel>()).ObjectsCount.Should().Be(0);
});
}
private static void SubMethod()
{
// Arrange
var kernel = new StandardKernel();
string namedScope = "namedScope";
kernel.Bind<View>().ToSelf()
.DefinesNamedScope(namedScope);
kernel.DefineDependency<View, …
Run Code Online (Sandbox Code Playgroud)