public void PerThreadShouldResolveOncePerThread() { Func <FakeRepository> repositoryFactory = () => new FakeRepository(); using (var lifeTime = new LifeTime()) { FakeRepository repo1 = null; FakeRepository repo2 = null; var t1 = new Thread(() => { repo1 = lifeTime.PerThread(repositoryFactory); }); var t2 = new Thread(() => { repo2 = lifeTime.PerThread(repositoryFactory); }); t1.Start(); t2.Start(); t1.Join(); t2.Join(); Assert.NotNull(repo1); Assert.NotNull(repo2); Assert.NotSame(repo1, repo2); } }
public void PerThreadShouldResolveTheSameForOneThread() { Func <FakeRepository> repositoryFactory = () => new FakeRepository(); using (var lifeTime = new LifeTime()) { var fakeRepo1 = lifeTime.PerThread(repositoryFactory); var fakeRepo2 = lifeTime.PerThread(repositoryFactory); Assert.Same(fakeRepo1, fakeRepo2); } }
public void DeactivateCommandShouldDeactivateItemWithLogAndPerRequest() { var dispatcher = new Dispatcher <ICommand>(); var sh = new StudentEnrollmentHandlers(); var ch = new CommonCommandHandlers(); var lifeTime = new LifeTime(); Func <StudentRepository> studentRepositoryFactory = () => new StudentRepository(); Func <ClassRepository> classRepositoryFactory = () => new ClassRepository(); Action <StudentEnrollCommand> studentEnrollPipeline = x => ch.Log(x, next => sh.Enroll(lifeTime.PerThread(studentRepositoryFactory), lifeTime.PerThread(classRepositoryFactory), next)); dispatcher.Subscribe(studentEnrollPipeline); dispatcher.Dispatch(new StudentEnrollCommand(1, 2, DateTime.Now, DateTime.Now.AddMonths(6))); lifeTime.Dispose(); }