public void CTS_EntityContext() { var repo = RF.Concrete <PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var pbsType = new PBSType { Name = "PBSType1", PBSList = { new PBS { Name = "PBS1" }, new PBS { Name = "PBS2" }, new PBS { Name = "PBS3" }, new PBS { Name = "PBS4" }, new PBS { Name = "PBS5" }, } }; repo.Save(pbsType); var id = pbsType.Id; using (RF.EnterEntityContext()) { var type1 = repo.CacheById(id) as PBSType; var count = Logger.DbAccessedCount; var type2 = repo.CacheById(id) as PBSType; Assert.IsTrue(Logger.DbAccessedCount == count, "GetById 内存缓存应该命中,不会发生数据层访问。"); Assert.IsTrue(type1 == type2, "由于使用了 EntityContext,从缓存中读取的对象,也应该是同一个。"); var list1 = type1.PBSList; Assert.IsTrue(list1.Count == 5); count = Logger.DbAccessedCount; var list2 = type2.PBSList; Assert.IsTrue(Logger.DbAccessedCount == count, "GetByParentId 内存缓存应该命中,不会发生数据层访问。"); foreach (var pbs1 in list1) { var pbs2 = list2.Find(pbs1.Id); Assert.IsTrue(pbs1 == pbs2, "由于使用了 EntityContext,从缓存中读取的对象,也应该是同一个。"); } } } }
public void CT_CTC_CacheByParentId() { var repo = RF.ResolveInstance <PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var pbsType = new PBSType { Name = "PBSType1", PBSList = { new PBS { Name = "PBS1" }, new PBS { Name = "PBS2" }, new PBS { Name = "PBS3" }, new PBS { Name = "PBS4" }, new PBS { Name = "PBS5" }, } }; repo.Save(pbsType); var id = pbsType.Id; var type1 = repo.CacheById(id) as PBSType; var list1 = type1.PBSList; Assert.IsTrue(list1.Count == 5); DbAccessedCount = 0; var type2 = repo.CacheById(id) as PBSType; var list2 = type2.PBSList; Assert.IsTrue(DbAccessedCount == 0, "GetByParentId 内存缓存应该命中,不会发生数据层访问。"); Assert.IsTrue(list2.Count == 5); } }
public void CTS_CacheByParentId() { var repo = RF.Concrete <PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var pbsType = new PBSType { Name = "PBSType1", PBSList = { new PBS { Name = "PBS1" }, new PBS { Name = "PBS2" }, new PBS { Name = "PBS3" }, new PBS { Name = "PBS4" }, new PBS { Name = "PBS5" }, } }; repo.Save(pbsType); var id = pbsType.Id; var type1 = repo.CacheById(id) as PBSType; var list1 = type1.PBSList; Assert.IsTrue(list1.Count == 5); var count = Logger.DbAccessedCount; var type2 = repo.CacheById(id) as PBSType;//Cache By Parent Id var list2 = type2.PBSList; Assert.IsTrue(Logger.DbAccessedCount == count, "CacheByParentId 内存缓存应该命中,不会发生数据层访问。"); Assert.IsTrue(list2.Count == 5); } }
public void CT_CTC_CacheById() { var repo = RF.ResolveInstance <PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var pbsType = new PBSType { Name = "PBSType1" }; repo.Save(pbsType); var id = pbsType.Id; var type1 = repo.CacheById(id) as PBSType; DbAccessedCount = 0; var type2 = repo.CacheById(id) as PBSType; Assert.IsTrue(DbAccessedCount == 0, "GetById 内存缓存应该命中,不会发生数据层访问。"); Assert.IsTrue(type1 != type2, "虽然是从缓存中获取,但是只缓存数据,所以对象不应该是同一个。"); } }
public void CTS_CacheById() { var repo = RF.Concrete <PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var pbsType = new PBSType { Name = "PBSType1" }; repo.Save(pbsType); var id = pbsType.Id; var type1 = repo.CacheById(id) as PBSType; var count = Logger.DbAccessedCount; var type2 = repo.CacheById(id) as PBSType; Assert.IsTrue(Logger.DbAccessedCount == count, "GetById 内存缓存应该命中,不会发生数据层访问。"); Assert.IsTrue(type1 != type2, "虽然是从缓存中获取,但是只缓存数据,所以对象不应该是同一个。"); } }
public void ET_Repository_Submit_ChildrenOnly() { var repo = RF.Concrete <PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var root = new PBSType(); var pbs = new PBS(); root.PBSList.Add(pbs); repo.Save(root); Assert.IsTrue(root.PersistenceStatus == PersistenceStatus.Unchanged); Assert.IsTrue(pbs.PersistenceStatus == PersistenceStatus.Unchanged); int count = 0; EventHandler <Rafy.Logger.DbAccessedEventArgs> handler = (o, e) => { if (e.ConnectionSchema == repo.RdbDataProvider.DbSetting) { count++; } }; Logger.DbAccessed += handler; pbs.Name = "DDDDDDDDDD"; Assert.IsTrue(root.PersistenceStatus == PersistenceStatus.Unchanged); Assert.IsTrue(pbs.PersistenceStatus == PersistenceStatus.Modified); var c = count; repo.Save(root); Assert.IsTrue(root.PersistenceStatus == PersistenceStatus.Unchanged); Assert.IsTrue(pbs.PersistenceStatus == PersistenceStatus.Unchanged); Assert.IsTrue(count == c + 1, "只进行了一次数据访问,即子对象的保存。"); Logger.DbAccessed -= handler; } }
public int IndexOf(PBSType entity) { return base.IndexOf(entity); }
public bool Contains(PBSType entity) { return base.Contains(entity); }
public void Add(PBSType entity) { base.Add(entity); }
public bool Remove(PBSType entity) { return base.Remove(entity); }
public void Insert(int index, PBSType entity) { base.Insert(index, entity); }
public void ET_Repository_Submit_ChildrenOnly() { var repo = RF.Concrete<PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var root = new PBSType(); var pbs = new PBS(); root.PBSList.Add(pbs); repo.Save(root); Assert.IsTrue(root.PersistenceStatus == PersistenceStatus.Unchanged); Assert.IsTrue(pbs.PersistenceStatus == PersistenceStatus.Unchanged); int count = 0; EventHandler<Rafy.Logger.DbAccessedEventArgs> handler = (o, e) => { if (e.ConnectionSchema == RdbDataProvider.Get(repo).DbSetting) count++; }; Logger.DbAccessed += handler; pbs.Name = "DDDDDDDDDD"; Assert.IsTrue(root.PersistenceStatus == PersistenceStatus.Unchanged); Assert.IsTrue(pbs.PersistenceStatus == PersistenceStatus.Modified); var c = count; repo.Save(root); Assert.IsTrue(root.PersistenceStatus == PersistenceStatus.Unchanged); Assert.IsTrue(pbs.PersistenceStatus == PersistenceStatus.Unchanged); Assert.IsTrue(count == c + 1, "只进行了一次数据访问,即子对象的保存。"); Logger.DbAccessed -= handler; } }
public void CTS_CacheById() { var repo = RF.Concrete<PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var pbsType = new PBSType { Name = "PBSType1" }; repo.Save(pbsType); var id = pbsType.Id; var type1 = repo.CacheById(id) as PBSType; var count = Logger.DbAccessedCount; var type2 = repo.CacheById(id) as PBSType; Assert.IsTrue(Logger.DbAccessedCount == count, "GetById 内存缓存应该命中,不会发生数据层访问。"); Assert.IsTrue(type1 != type2, "虽然是从缓存中获取,但是只缓存数据,所以对象不应该是同一个。"); } }
public void CTS_EntityContext() { var repo = RF.Concrete<PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var pbsType = new PBSType { Name = "PBSType1", PBSList = { new PBS { Name = "PBS1" }, new PBS { Name = "PBS2" }, new PBS { Name = "PBS3" }, new PBS { Name = "PBS4" }, new PBS { Name = "PBS5" }, } }; repo.Save(pbsType); var id = pbsType.Id; using (RF.EnterEntityContext()) { var type1 = repo.CacheById(id) as PBSType; var count = Logger.DbAccessedCount; var type2 = repo.CacheById(id) as PBSType; Assert.IsTrue(Logger.DbAccessedCount == count, "GetById 内存缓存应该命中,不会发生数据层访问。"); Assert.IsTrue(type1 == type2, "由于使用了 EntityContext,从缓存中读取的对象,也应该是同一个。"); var list1 = type1.PBSList; Assert.IsTrue(list1.Count == 5); count = Logger.DbAccessedCount; var list2 = type2.PBSList; Assert.IsTrue(Logger.DbAccessedCount == count, "GetByParentId 内存缓存应该命中,不会发生数据层访问。"); foreach (var pbs1 in list1) { var pbs2 = list2.Find(pbs1.Id); Assert.IsTrue(pbs1 == pbs2, "由于使用了 EntityContext,从缓存中读取的对象,也应该是同一个。"); } } } }
public void CTS_CacheByParentId() { var repo = RF.Concrete<PBSTypeRepository>(); using (RF.TransactionScope(repo)) { var pbsType = new PBSType { Name = "PBSType1", PBSList = { new PBS { Name = "PBS1" }, new PBS { Name = "PBS2" }, new PBS { Name = "PBS3" }, new PBS { Name = "PBS4" }, new PBS { Name = "PBS5" }, } }; repo.Save(pbsType); var id = pbsType.Id; var type1 = repo.CacheById(id) as PBSType; var list1 = type1.PBSList; Assert.IsTrue(list1.Count == 5); var count = Logger.DbAccessedCount; var type2 = repo.CacheById(id) as PBSType;//Cache By Parent Id var list2 = type2.PBSList; Assert.IsTrue(Logger.DbAccessedCount == count, "CacheByParentId 内存缓存应该命中,不会发生数据层访问。"); Assert.IsTrue(list2.Count == 5); } }