Exemple #1
0
        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,从缓存中读取的对象,也应该是同一个。");
                    }
                }
            }
        }