Example #1
0
        static Program()
        {
            RepositoryContainer.Register <UserRepo>();
            MongoDB.Repository.RepositoryContainer.Register <UserMongoRepo>();

            RepositoryContainer.Register <MallCardRepo>();
            MongoDB.Repository.RepositoryContainer.Register <MallCardMongoRepo>();

            RepositoryContainer.Register <CardVoucherInfoRepo>();
            MongoDB.Repository.RepositoryContainer.Register <CardVoucherInfoMongoRepo>();
        }
        public void Register()
        {
            UserRepAsync uRep = new UserRepAsync();

            RepositoryContainer.Register(uRep);
            UserRepAsync uRep2 = RepositoryContainer.Resolve <UserRepAsync>();

            Assert.AreEqual(uRep, uRep2);

            RepositoryContainer.Register <UserRepAsync>(() => { return(new UserRepAsync()); });
            var uRepAsync = RepositoryContainer.Resolve <UserRepAsync>();

            Assert.IsNotNull(uRepAsync);
        }
        public async Task CallMongoDB()
        {
            #region 无参公共构造方法
            RepositoryContainer.Register <UserRepAsync>();
            RepositoryContainer.Register <UserRepAsync>();

            var uRep      = RepositoryContainer.Resolve <UserRepAsync>();
            var uRepAsync = RepositoryContainer.Resolve <UserRepAsync>();

            #region  步仓储 新增和读取数据测试
            User entity1 = new User()
            {
                Age   = 18,
                Name  = "uRep测试",
                Stamp = "哈哈"
            };
            await uRep.InsertAsync(entity1);

            Assert.IsTrue(entity1.ID > 0);
            var e1 = await uRep.GetAsync(x => x.ID == entity1.ID);

            Assert.IsNotNull(e1);
            Assert.IsTrue(e1.Name == "uRep测试");
            #endregion

            #region 异步仓储 新增和读取数据测试
            User entity2 = new User()
            {
                Age   = 19,
                Name  = "uRepAsync测试",
                Stamp = "哈哈2"
            };
            await uRepAsync.InsertAsync(entity2);

            Assert.IsTrue(entity2.ID > 0);
            var e2 = await uRepAsync.GetAsync(x => x.ID == entity2.ID);

            Assert.IsNotNull(e2);
            Assert.IsTrue(e2.Name == "uRepAsync测试");
            #endregion
            #endregion

            #region 无参私有构造方法
            RepositoryContainer.Register(typeof(User2RepositoryAsync));
            RepositoryContainer.Register(typeof(User2RepositoryAsync));

            var uRep2      = RepositoryContainer.Resolve <User2RepositoryAsync>();
            var uRepAsync2 = RepositoryContainer.Resolve <User2RepositoryAsync>();

            #region  步仓储 新增和读取数据测试
            User2 entity3 = new User2()
            {
                Age    = 20,
                Name   = "User2Repository测试",
                Remark = "哈哈"
            };
            await uRep2.InsertAsync(entity3);

            Assert.IsNotNull(entity3.ID);
            var e3 = await uRep2.GetAsync(x => x.ID == entity3.ID);

            Assert.IsNotNull(e3);
            Assert.IsTrue(e3.Name == "User2Repository测试");
            #endregion

            #region 异步仓储 新增和读取数据测试
            User2 entity4 = new User2()
            {
                Age    = 21,
                Name   = "User2RepositoryAsync测试",
                Remark = "哈哈"
            };
            await uRepAsync2.InsertAsync(entity4);

            Assert.IsNotNull(entity4.ID);
            var e4 = await uRepAsync2.GetAsync(x => x.ID == entity4.ID);

            Assert.IsNotNull(e4);
            Assert.IsTrue(e4.Name == "User2RepositoryAsync测试");
            #endregion
            #endregion
        }