public void AppScopeTest() { var builder = new ContainerBuilder(); builder.RegisterType <TestClass1>().AsSelf().SingleInstance(); builder.RegisterType <TestClass2>().AsSelf().InstancePerLifetimeScope(); IContainer cont = builder.Build(); using (var scope = new AppScope(cont)) { object r1 = scope.Resolve(typeof(TestClass1)); var r2 = scope.Resolve <TestClass1>(); Assert.AreSame(r1, r2); Assert.AreSame(cont, scope.Scope); var t1 = scope.Resolve <TestClass2>(); using (AppScope inner = scope.BeginScope()) { var t2 = inner.Resolve <TestClass2>(); Assert.AreNotSame(t1, t2); var resolvedScope = inner.Resolve <IAppScope>(); Assert.AreSame(inner, resolvedScope); } } }
public void AppCoreBuilderTest() { AppCoreBuilder.Create(new ContainerBuilder()).Build().Dispose(); using ( AppCore core = AppCoreBuilder.Create() .AddModule(new TestModule()) .AddModule <TestModule1>() .Configure(b => b.RegisterType <TestClass1>().AsSelf().SingleInstance()) .Build()) { using (AppScope scope = core.BeginScope()) { object r1 = scope.Resolve(typeof(TestClass1)); var r2 = scope.Resolve <TestClass1>(); Assert.AreSame(r1, r2); var t1 = scope.Resolve <TestClass2>(); using (AppScope inner = scope.BeginScope()) { var t2 = inner.Resolve <TestClass2>(); Assert.AreNotSame(t1, t2); } } } }
public RefrenceSession <TRefObject> GetRefrenceSession() { // Если _sessionFactory == null то создаем временый скоуп с продолжительностью жизни, // равной продолжительности жизни создаваемой сессии. Из этого скоупа временно резолвим _sessionFactory // и зануляем его после получения сессии. AppScope localScope = null; try { if (_sessionFactory == null || RepositoryFactory == null) { localScope = AppCore.Instance.BeginScope(); _sessionFactory = localScope.Resolve <Func <IDocumentSession> >(); RepositoryFactory = localScope.Resolve <Func <IDocumentSession, IRepository <TRefObject> > >(); } using (var rootSession = _sessionFactory()) { IDocumentSession session = rootSession.Advanced.DocumentStore.OpenSession(); IRepository <TRefObject> repository = RepositoryFactory(session); if (localScope != null) { _sessionFactory = null; RepositoryFactory = null; } return(new RefrenceSession <TRefObject>(repository, session, localScope)); } } catch { if (localScope != null) { localScope.Dispose(); } throw; } }
public void AppScopeTest() { var builder = new ContainerBuilder(); builder.RegisterType<TestClass1>().AsSelf().SingleInstance(); builder.RegisterType<TestClass2>().AsSelf().InstancePerLifetimeScope(); IContainer cont = builder.Build(); using (var scope = new AppScope(cont)) { object r1 = scope.Resolve(typeof (TestClass1)); var r2 = scope.Resolve<TestClass1>(); Assert.AreSame(r1, r2); Assert.AreSame(cont, scope.Scope); var t1 = scope.Resolve<TestClass2>(); using (AppScope inner = scope.BeginScope()) { var t2 = inner.Resolve<TestClass2>(); Assert.AreNotSame(t1, t2); var resolvedScope = inner.Resolve<IAppScope>(); Assert.AreSame(inner,resolvedScope); } } }
public void BasicRavenReposTest() { string realId; Guid realGuid; string refId; //using (var sess = _ds.OpenSession()) var curScope = _scope.Scope.IsRegistered <IAppScope>(); Assert.True(curScope); using (var scope = _core1.BeginScope()) using (var repos = scope.Resolve <IRepositoryWithGuid <TestObject> >()) using (var repos1 = scope.Resolve <IRepositoryWithGuid <TestObject1> >()) { var item1 = new TestObject1 { Name = "1233" }; repos1.Store(item1); var item2 = new TestObject1 { Name = "1233" }; //repos1.Store(item2); repos1.SaveChanges(); var ref1 = _scope.Resolve <Refrence <TestObject1> >(); var ref2 = _scope.Resolve <Refrence <TestObject1> >(); ref1.Object = item1; ref2.Object = item2; var item = new TestObject { Name = "123", Refrence = ref1, Refrence1 = ref2 }; repos.Store(item); repos.SaveChanges(); refId = item1.Id; realId = item.Id; realGuid = item.Guid; Assert.IsNotNullOrEmpty(realId); } using (var scope = _core1.BeginScope()) using (var repos = scope.Resolve <IRepositoryWithGuid <TestObject> >()) { TestObject item = repos.Find(realGuid); var clone = item; Assert.NotNull(repos.Find(clone.Guid)); item = repos.Find(clone.Guid); var item2 = repos.Find(new[] { realGuid }); TestObject item1 = repos.Find(realId + "123123123"); Assert.NotNull(item); //Assert.NotNull(item); //item.Refrence.RepositoryFactory = repos1; Assert.NotNull(item.Refrence); Assert.NotNull(item.Refrence.Object); Assert.AreEqual(item.Refrence.Object.Id, refId); Assert.Null(item1); repos.Delete(item); repos.SaveChanges(); } using (var repos = _scope.Resolve <IRepositoryWithGuid <TestObject> >()) using (var repos1 = _scope.Resolve <IRepositoryWithGuid <TestObject1> >()) { TestObject item = repos.Find(realId); Assert.Null(item); List <TestObject1> items = repos1.Query.ToList(); Assert.IsTrue(items.Count > 0); } var lst = new List <TestObject>(); using (var repos = _scope.Resolve <IRepositoryWithGuid <TestObject> >()) { for (int i = 0; i < 10; i++) { lst.Add(new TestObject { Name = i.ToString() }); } repos.StoreBulk(lst); repos.SaveChanges(); } using (var repos = _scope.Resolve <IRepositoryWithGuid <TestObject> >()) { List <TestObject> res = repos.Find(lst.Select(i => i.Id)).ToList(); Assert.AreEqual(res.Count, lst.Count); } }