当前位置: 首页>>代码示例>>C#>>正文


C# MongoRepository.GetById方法代码示例

本文整理汇总了C#中MongoRepository.GetById方法的典型用法代码示例。如果您正苦于以下问题:C# MongoRepository.GetById方法的具体用法?C# MongoRepository.GetById怎么用?C# MongoRepository.GetById使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在MongoRepository的用法示例。


在下文中一共展示了MongoRepository.GetById方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            string mongoUrl = "mongodb://127.0.0.1:27017/mongodemo";
            string databaseName = "MongoDemo";

            //IMongoClient _client;
            //IMongoDatabase db;

            //_client = new MongoClient("mongodb://127.0.0.1:27017/mongodemo");
            //db = _client.GetDatabase("MongoDemo");

            //var list = db.GetCollection<Restaurant>("restaurants").Find(x => true).ToListAsync<Restaurant>().Result;

            //foreach (var restaurant in list)
            //{
            //    Console.WriteLine(restaurant.Name);
            //}

            MongoRepository<Restaurant> restaurantRepo = new MongoRepository<Restaurant>(mongoUrl, databaseName, "restaurants");
            //var list = restaurantRepo.GetAll<Restaurant>();

            var t1 = restaurantRepo.GetByIdAsync(new ObjectId("55e69a970e6672a2fb2cc624"));
            var restaurant = restaurantRepo.GetById(new ObjectId("55e69a970e6672a2fb2cc624"));

            Console.ReadLine();
        }
开发者ID:asim-khan,项目名称:MongoRepository,代码行数:26,代码来源:Program.cs

示例2: OnActionExecuting

        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            // Run base method to handle Users and Roles filter parameters.
            // Grab the fundId from the request.
            var fundId = actionContext.ControllerContext.RouteData.Values["id"].ToString();

            // Query for the fund.
            var fundRepository = new MongoRepository<Fund>();
            var fund = fundRepository.GetById(fundId);

            // Ensure the supplied fund exists.
            if (fund == null)
            {
                throw new HttpException(404, "NotFound");
            }

            if (this.IsAuthorizedToAccessArea(fund.AreaId))
            {
                // Add the fund to the Items dictionary to avoid duplicating the query.
                HttpContext.Current.Items["fund"] = fund;
            }
            else
            {
                throw new HttpException(401, "Unauthorized. You are not authorized to access this area.");
            }
        }
开发者ID:chrisjsherm,项目名称:FoundationPortal,代码行数:26,代码来源:ReadFundActionFilter.cs

示例3: OnActionExecuting

        public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            // Grab the arguments from the request.
            var id = (string)actionContext.ActionArguments["id"];
            var fundData = (Fund)actionContext.ActionArguments["fund"];

            // Ensure the user has not mismatched the Id property.
            if (id != fundData.Id)
            {
                throw new HttpException(400, "BadRequest: Data does not match item associated with request id.");
            }

            var fundRepository = new MongoRepository<Fund>();
            var fund = fundRepository.GetById(id);

            // Ensure the fund exists.
            if (fund == null)
            {
                throw new HttpException(404, "NotFound. The requested fund does not exist.");
            }

            // Ensure the user has not mismatched the AreaId property.
            if (fundData.AreaId != fund.AreaId)
            {
                throw new HttpException(400, "BadRequest: Data does not match item associated with request id.");
            }

            if (!this.IsAuthorizedToAccessArea(fund.AreaId))
            {
                throw new HttpException(401, "Unauthorized. You are not authorized to access this area.");
            }
        }
开发者ID:chrisjsherm,项目名称:FoundationPortal,代码行数:32,代码来源:UpdateFundActionFilter.cs

示例4: IsAuthorizedToAccessArea

        protected bool  IsAuthorizedToAccessArea(string areaId)
        {
            // Ensure the request contains the areaId
            if (String.IsNullOrEmpty(areaId))
            {
                throw new HttpException(400, "BadRequest. Area not supplied.");
            }

            // Query for the area to match up its number to the associated Role.
            var areaRepository = new MongoRepository<Area>();
            var area = areaRepository.GetById(areaId);

            // Ensure the supplied area exists.
            if (area == null)
            {
                throw new HttpException(404, "NotFound. The requested area does not exist.");
            }

            // Ensure the user is in a role to allow accessing the area.
            foreach (var role in RoleValidator.GetAuthorizedRolesForArea(area))
            {
                if (HttpContext.Current.User.IsInRole(role))
                {
                    // Add the area to the Items dictionary to avoid duplicating the query.
                    HttpContext.Current.Items["area"] = area;
                    return true;
                }
            }
            
            return false;
        }
开发者ID:chrisjsherm,项目名称:FoundationPortal,代码行数:31,代码来源:AccessAreaActionFilter.cs

示例5: Main

        static void Main(string[] args)
        {
            var personReadModel = new MongoRepository<PersonDTO>("mongodb://localhost", "DDD_Light_MongoEventStore_Example", "Person_ReadModel");
            EventStore.Instance.Configure(new MongoRepository<AggregateEvent>("mongodb://localhost", "DDD_Light_MongoEventStore_Example", "EventStore"), new JsonEventSerializationStrategy());
            EventBus.Instance.Configure(EventStore.Instance, new JsonEventSerializationStrategy(), false);

            EventBus.Instance.Subscribe((PersonCreated personCreated) =>
                {                    
                    var personDTO = new PersonDTO {Id = personCreated.Id};
                    personReadModel.Save(personDTO);
                });
            
            EventBus.Instance.Subscribe((PersonNamed personNamed) =>
                {                    
                    var personDTO = personReadModel.GetById(personNamed.PersonId);
                    personDTO.Name = personNamed.Name;
                    personDTO.WasRenamed = false;
                    personReadModel.Save(personDTO);
                });

            EventBus.Instance.Subscribe((PersonRenamed personRenamed) =>
                {                    
                    var personDTO = personReadModel.GetById(personRenamed.PersonId);
                    personDTO.Name = personRenamed.Name;                    
                    personDTO.WasRenamed = true;                    
                    personReadModel.Save(personDTO);
                });


            NamePerson(personReadModel);
            NameAndRenamePerson(personReadModel);

            // Drop readmodel on mongo and then run this to restore
            //EventBus.Instance.RestoreReadModel(EventBus.Instance);


            Console.ReadLine();
        }
开发者ID:jango2015,项目名称:DDD.Light,代码行数:38,代码来源:Program.cs

示例6: AddAndUpdateTest

        public void AddAndUpdateTest()
        {
            IRepository<Customer> _customerRepo = new MongoRepository<Customer>();
            IRepositoryManager<Customer> _customerMan = new MongoRepositoryManager<Customer>();

            Assert.IsFalse(_customerMan.Exists);

            var customer = new Customer();
            customer.FirstName = "Bob";
            customer.LastName = "Dillon";
            customer.Phone = "0900999899";
            customer.Email = "[email protected]";
            customer.HomeAddress = new Address
            {
                Address1 = "North kingdom 15 west",
                Address2 = "1 north way",
                PostCode = "40990",
                City = "George Town",
                Country = "Alaska"
            };

            _customerRepo.Add(customer);

            Assert.IsTrue(_customerMan.Exists);

            Assert.IsNotNull(customer.Id);

            // fetch it back
            var alreadyAddedCustomer = _customerRepo.Where(c => c.FirstName == "Bob").Single();

            Assert.IsNotNull(alreadyAddedCustomer);
            Assert.AreEqual(customer.FirstName, alreadyAddedCustomer.FirstName);
            Assert.AreEqual(customer.HomeAddress.Address1, alreadyAddedCustomer.HomeAddress.Address1);

            alreadyAddedCustomer.Phone = "10110111";
            alreadyAddedCustomer.Email = "[email protected]";

            _customerRepo.Update(alreadyAddedCustomer);

            // fetch by id now
            var updatedCustomer = _customerRepo.GetById(customer.Id);

            Assert.IsNotNull(updatedCustomer);
            Assert.AreEqual(alreadyAddedCustomer.Phone, updatedCustomer.Phone);
            Assert.AreEqual(alreadyAddedCustomer.Email, updatedCustomer.Email);

            Assert.IsTrue(_customerRepo.Exists(c => c.HomeAddress.Country == "Alaska"));
        }
开发者ID:rodolfofadino,项目名称:MongoRepository-Fork,代码行数:48,代码来源:RepoTests.cs

示例7: CustomIDTypeTest

        public void CustomIDTypeTest()
        {
            var xint = new MongoRepository<IntCustomer, int>();
            xint.Add(new IntCustomer() { Id = 1, Name = "Test A" });
            xint.Add(new IntCustomer() { Id = 2, Name = "Test B" });

            var yint = xint.GetById(2);
            Assert.AreEqual(yint.Name, "Test B");

            xint.Delete(2);
            Assert.AreEqual(1, xint.Count());
        }
开发者ID:rodolfofadino,项目名称:MongoRepository-Fork,代码行数:12,代码来源:RepoTests.cs

示例8: CustomIDTest

        public void CustomIDTest()
        {
            var x = new MongoRepository<CustomIDEntity>();
            var xm = new MongoRepositoryManager<CustomIDEntity>();

            x.Add(new CustomIDEntity() { Id = "aaa" });

            Assert.IsTrue(xm.Exists);
            Assert.IsInstanceOfType(x.GetById("aaa"), typeof(CustomIDEntity));

            Assert.AreEqual("aaa", x.GetById("aaa").Id);

            x.Delete("aaa");
            Assert.AreEqual(0, x.Count());

            var y = new MongoRepository<CustomIDEntityCustomCollection>();
            var ym = new MongoRepositoryManager<CustomIDEntityCustomCollection>();

            y.Add(new CustomIDEntityCustomCollection() { Id = "xyz" });

            Assert.IsTrue(ym.Exists);
            Assert.AreEqual(ym.Name, "MyTestCollection");
            Assert.AreEqual(y.CollectionName, "MyTestCollection");
            Assert.IsInstanceOfType(y.GetById("xyz"), typeof(CustomIDEntityCustomCollection));

            y.Delete("xyz");
            Assert.AreEqual(0, y.Count());
        }
开发者ID:rodolfofadino,项目名称:MongoRepository-Fork,代码行数:28,代码来源:RepoTests.cs

示例9: CollectionNamesTest

        public void CollectionNamesTest()
        {
            var a = new MongoRepository<Animal>();
            var am = new MongoRepositoryManager<Animal>();
            var va = new Dog();
            Assert.IsFalse(am.Exists);
            a.Update(va);
            Assert.IsTrue(am.Exists);
            Assert.IsInstanceOfType(a.GetById(va.Id), typeof(Dog));
            Assert.AreEqual(am.Name, "AnimalsTest");
            Assert.AreEqual(a.CollectionName, "AnimalsTest");

            var cl = new MongoRepository<CatLike>();
            var clm = new MongoRepositoryManager<CatLike>();
            var vcl = new Lion();
            Assert.IsFalse(clm.Exists);
            cl.Update(vcl);
            Assert.IsTrue(clm.Exists);
            Assert.IsInstanceOfType(cl.GetById(vcl.Id), typeof(Lion));
            Assert.AreEqual(clm.Name, "Catlikes");
            Assert.AreEqual(cl.CollectionName, "Catlikes");

            var b = new MongoRepository<Bird>();
            var bm = new MongoRepositoryManager<Bird>();
            var vb = new Bird();
            Assert.IsFalse(bm.Exists);
            b.Update(vb);
            Assert.IsTrue(bm.Exists);
            Assert.IsInstanceOfType(b.GetById(vb.Id), typeof(Bird));
            Assert.AreEqual(bm.Name, "Birds");
            Assert.AreEqual(b.CollectionName, "Birds");

            var l = new MongoRepository<Lion>();
            var lm = new MongoRepositoryManager<Lion>();
            var vl = new Lion();
            //Assert.IsFalse(lm.Exists);   //Should already exist (created by cl)
            l.Update(vl);
            Assert.IsTrue(lm.Exists);
            Assert.IsInstanceOfType(l.GetById(vl.Id), typeof(Lion));
            Assert.AreEqual(lm.Name, "Catlikes");
            Assert.AreEqual(l.CollectionName, "Catlikes");

            var d = new MongoRepository<Dog>();
            var dm = new MongoRepositoryManager<Dog>();
            var vd = new Dog();
            //Assert.IsFalse(dm.Exists);
            d.Update(vd);
            Assert.IsTrue(dm.Exists);
            Assert.IsInstanceOfType(d.GetById(vd.Id), typeof(Dog));
            Assert.AreEqual(dm.Name, "AnimalsTest");
            Assert.AreEqual(d.CollectionName, "AnimalsTest");

            var m = new MongoRepository<Bird>();
            var mm = new MongoRepositoryManager<Bird>();
            var vm = new Macaw();
            //Assert.IsFalse(mm.Exists);
            m.Update(vm);
            Assert.IsTrue(mm.Exists);
            Assert.IsInstanceOfType(m.GetById(vm.Id), typeof(Macaw));
            Assert.AreEqual(mm.Name, "Birds");
            Assert.AreEqual(m.CollectionName, "Birds");

            var w = new MongoRepository<Whale>();
            var wm = new MongoRepositoryManager<Whale>();
            var vw = new Whale();
            Assert.IsFalse(wm.Exists);
            w.Update(vw);
            Assert.IsTrue(wm.Exists);
            Assert.IsInstanceOfType(w.GetById(vw.Id), typeof(Whale));
            Assert.AreEqual(wm.Name, "Whale");
            Assert.AreEqual(w.CollectionName, "Whale");
        }
开发者ID:rodolfofadino,项目名称:MongoRepository-Fork,代码行数:72,代码来源:RepoTests.cs


注:本文中的MongoRepository.GetById方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。