public void ShouldSaveArbitraryDocument() { var littleCar = new LittleCar() { docType = "car", Make = "Yugo", Model = "Hell if i know" }; littleCar = db.SaveArbitraryDocument(littleCar); Assert.IsNotNull(littleCar._id); }
public void ShouldLoadArbitraryDocument() { var firstCar = new LittleCar() { docType = "car", Make = "Yugo", Model = "Hell if i know" }; firstCar = db.SaveArbitraryDocument(firstCar); var otherCar = db.GetArbitraryDocument <LittleCar>(firstCar._id, () => new LittleCar()); Assert.IsNotNull(otherCar); Assert.IsNotNull(otherCar._id); }
public void ShouldSaveArbitraryDocuments() { var littleCar1 = new LittleCar { docType = "car", Make = "Make1" }; var littleCar2 = new LittleCar { docType = "car", Make = "Make2" }; var docs = new List <LittleCar> { littleCar1, littleCar2 }; db.SaveArbitraryDocuments(docs, true); var documentIds = db.GetAllDocuments().Select(doc => doc.Id); var loadedCars = db.GetArbitraryDocuments(documentIds, () => new LittleCar()); Assert.AreEqual(littleCar1.Make, loadedCars.ElementAt(0).Make); Assert.AreEqual(littleCar2.Make, loadedCars.ElementAt(1).Make); }
static void Main(string[] args) { string host = ""; int port = 5984; String username = ""; String password = ""; // Lets you see all HTTP requests made by Divan Trace.Listeners.Add(new ConsoleTraceListener()); // Trivial parse of args to get host and port switch (args.Length) { case 0: Console.WriteLine("Using " + host + ":" + port); break; case 1: Console.WriteLine("Using " + args[0] + ":5984"); host = args[0]; break; case 2: Console.WriteLine("Using " + args[0] + ":" + args[1]); host = args[0]; port = int.Parse(args[1]); break; } // Get a server instance. It only holds host, port and a string database prefix. // For non trivial usage of Divan you typically create your own subclass of CouchServer. var server = new CouchServer(host, port,username,password); /* This has issues with the windows build of couch db - something about file locking // a little bit of cleanup if (server.HasDatabase("trivial")) server.DeleteDatabase("trivial"); */ // Get (creates it if needed) a CouchDB database. This call will create the db in CouchDB // if it does not exist, create a CouchDatabase instance and then send Initialize() to it // before returning it. The base class CouchDatabase also has very little state, it knows // only the server that it belongs to and its own name. var db = server.GetDatabase("trivial"); Console.WriteLine("Created database 'trivial'"); // Create and save 10 Cars with automatically allocated Ids by CouchDB. // Divan stores ICouchDocuments and there are several ways you can go: // - Create a subclass of CouchDocument (like Car is here). // - Create a class that implements ICouchDocument. // - Create a subclass of CouchJsonDocument. Car car = null; for (int i = 0; i < 10; i++) { car = new Car("Saab", "93", 170 + i); db.SaveDocument(car); } Console.WriteLine("Saved 10 Cars with 170-180 hps each."); // Load all Cars as CouchJsonDocuments and print one of them Console.WriteLine("Here is the first of the Cars: \n\n" + db.GetAllDocuments().First().ToString()); // Modify the last Car we saved... car.HorsePowers = 400; // ...and save the change. // We could also have used WriteDocument if we knew it was an existing doc db.SaveDocument(car); Console.WriteLine("Modified last Car with id " + car.Id); // Load a Car by known id (we just pick it from car), the class to instantiate is given using generics (Car) var sameCar = db.GetDocument<Car>(car.Id); Console.WriteLine("Loaded last Car " + sameCar.Make + " " + sameCar.Model + " now with " + sameCar.HorsePowers + "hps."); // Load all Cars. If we dwelve into the GetAllDocuments() method we will see that // QueryAllDocuments() gives us a CouchQuery which we can configure. We tell it to IncludeDocuments() // which means that we will get back not only ids but the actual documents too. GetResult() will perform the // HTTP request to CouchDB and return a CouchGenericViewResult which we in turn can ask to produce objects from JSON, // in this case we pick out the actual documents and instantiate them as instances of the class Car. var cars = db.GetAllDocuments<Car>(); Console.WriteLine("Loaded all Cars: " + cars.Count()); // Now try some linq var tempView = db.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);"); var linqCars = tempView.LinqQuery<Car>(); var fastCars = from c in linqCars where c.HorsePowers >= 175 select c;//.Make + " " + c.Model; foreach (var fastCar in fastCars) Console.WriteLine(fastCar); var twoCars = from c in linqCars where c.HorsePowers == 175 || c.HorsePowers == 176 select c;//.Make + " " + c.Model; foreach (var twoCar in twoCars) Console.WriteLine(twoCar); var hps = new int[] { 176, 177 }; var twoMoreCars = from c in linqCars where hps.Contains(c.HorsePowers) select c.Make + " " + c.Model + " with " + c.HorsePowers + "HPs"; foreach (var twoCar in twoMoreCars) Console.WriteLine(twoCar); // cleanup for later db.DeleteDocument(tempView.Doc); // Delete some Cars one by one. CouchDB is an MVCC database which means that for every operation that modifies a document // we need to supply not only its document id, but also the revision that we are aware of. This means that we must supply id/rev // for each document we want to delete. foreach (var eachCar in cars.Where(x => x.HorsePowers > 175)) { db.DeleteDocument(eachCar); Console.WriteLine("Deleted car with id " + eachCar.Id); } cars = db.GetAllDocuments<Car>(); // Get all cars again and see how many are left. Console.WriteLine("Cars left: " + cars.Count()); // We can actually also delete in a single bulk call using DeleteDocuments(). db.DeleteDocuments(cars.ToArray()); Console.WriteLine("Deleted the rest of the cars"); // test out the arbitrary conventions Console.WriteLine("Trying arbitrary documents"); var littleCar = new LittleCar() { docType = "car", Make = "Yugo", Model = "Hell if i know" }; littleCar = db.SaveArbitraryDocument(littleCar); Console.Write("\r\nDelete database (y/n)? "); if (Console.ReadLine().Trim().Equals("y", StringComparison.OrdinalIgnoreCase)) { // Delete the db itself db.Delete(); Console.WriteLine("Deleted database."); } Console.WriteLine("\r\nPress enter to close. "); Console.ReadLine(); }
static void Main(string[] args) { string host = ""; int port = 5984; String username = ""; String password = ""; // Lets you see all HTTP requests made by Divan Trace.Listeners.Add(new ConsoleTraceListener()); // Trivial parse of args to get host and port switch (args.Length) { case 0: Console.WriteLine("Using " + host + ":" + port); break; case 1: Console.WriteLine("Using " + args[0] + ":5984"); host = args[0]; break; case 2: Console.WriteLine("Using " + args[0] + ":" + args[1]); host = args[0]; port = int.Parse(args[1]); break; } // Get a server instance. It only holds host, port and a string database prefix. // For non trivial usage of Divan you typically create your own subclass of CouchServer. var server = new CouchServer(host, port, username, password); /* This has issues with the windows build of couch db - something about file locking * // a little bit of cleanup * if (server.HasDatabase("trivial")) * server.DeleteDatabase("trivial"); */ // Get (creates it if needed) a CouchDB database. This call will create the db in CouchDB // if it does not exist, create a CouchDatabase instance and then send Initialize() to it // before returning it. The base class CouchDatabase also has very little state, it knows // only the server that it belongs to and its own name. var db = server.GetDatabase("trivial"); Console.WriteLine("Created database 'trivial'"); // Create and save 10 Cars with automatically allocated Ids by CouchDB. // Divan stores ICouchDocuments and there are several ways you can go: // - Create a subclass of CouchDocument (like Car is here). // - Create a class that implements ICouchDocument. // - Create a subclass of CouchJsonDocument. Car car = null; for (int i = 0; i < 10; i++) { car = new Car("Saab", "93", 170 + i); db.SaveDocument(car); } Console.WriteLine("Saved 10 Cars with 170-180 hps each."); // Load all Cars as CouchJsonDocuments and print one of them Console.WriteLine("Here is the first of the Cars: \n\n" + db.GetAllDocuments().First().ToString()); // Modify the last Car we saved... car.HorsePowers = 400; // ...and save the change. // We could also have used WriteDocument if we knew it was an existing doc db.SaveDocument(car); Console.WriteLine("Modified last Car with id " + car.Id); // Load a Car by known id (we just pick it from car), the class to instantiate is given using generics (Car) var sameCar = db.GetDocument <Car>(car.Id); Console.WriteLine("Loaded last Car " + sameCar.Make + " " + sameCar.Model + " now with " + sameCar.HorsePowers + "hps."); // Load all Cars. If we dwelve into the GetAllDocuments() method we will see that // QueryAllDocuments() gives us a CouchQuery which we can configure. We tell it to IncludeDocuments() // which means that we will get back not only ids but the actual documents too. GetResult() will perform the // HTTP request to CouchDB and return a CouchGenericViewResult which we in turn can ask to produce objects from JSON, // in this case we pick out the actual documents and instantiate them as instances of the class Car. var cars = db.GetAllDocuments <Car>(); Console.WriteLine("Loaded all Cars: " + cars.Count()); // Now try some linq var tempView = db.NewTempView("test", "test", "if (doc.docType && doc.docType == 'car') emit(doc.Hps, doc);"); var linqCars = tempView.LinqQuery <Car>(); var fastCars = from c in linqCars where c.HorsePowers >= 175 select c; //.Make + " " + c.Model; foreach (var fastCar in fastCars) { Console.WriteLine(fastCar); } var twoCars = from c in linqCars where c.HorsePowers == 175 || c.HorsePowers == 176 select c; //.Make + " " + c.Model; foreach (var twoCar in twoCars) { Console.WriteLine(twoCar); } var hps = new int[] { 176, 177 }; var twoMoreCars = from c in linqCars where hps.Contains(c.HorsePowers) select c.Make + " " + c.Model + " with " + c.HorsePowers + "HPs"; foreach (var twoCar in twoMoreCars) { Console.WriteLine(twoCar); } // cleanup for later db.DeleteDocument(tempView.Doc); // Delete some Cars one by one. CouchDB is an MVCC database which means that for every operation that modifies a document // we need to supply not only its document id, but also the revision that we are aware of. This means that we must supply id/rev // for each document we want to delete. foreach (var eachCar in cars.Where(x => x.HorsePowers > 175)) { db.DeleteDocument(eachCar); Console.WriteLine("Deleted car with id " + eachCar.Id); } cars = db.GetAllDocuments <Car>(); // Get all cars again and see how many are left. Console.WriteLine("Cars left: " + cars.Count()); // We can actually also delete in a single bulk call using DeleteDocuments(). db.DeleteDocuments(cars.ToArray()); Console.WriteLine("Deleted the rest of the cars"); // test out the arbitrary conventions Console.WriteLine("Trying arbitrary documents"); var littleCar = new LittleCar() { docType = "car", Make = "Yugo", Model = "Hell if i know" }; littleCar = db.SaveArbitraryDocument(littleCar); Console.Write("\r\nDelete database (y/n)? "); if (Console.ReadLine().Trim().Equals("y", StringComparison.OrdinalIgnoreCase)) { // Delete the db itself db.Delete(); Console.WriteLine("Deleted database."); } Console.WriteLine("\r\nPress enter to close. "); Console.ReadLine(); }
public void ShouldLoadArbitraryDocument() { var firstCar = new LittleCar() { docType = "car", Make = "Yugo", Model = "Hell if i know" }; firstCar = db.SaveArbitraryDocument(firstCar); var otherCar = db.GetArbitraryDocument<LittleCar>(firstCar._id, () => new LittleCar()); Assert.IsNotNull(otherCar); Assert.IsNotNull(otherCar._id); }
public void ShouldSaveArbitraryDocuments() { var littleCar1 = new LittleCar { docType = "car", Make = "Make1" }; var littleCar2 = new LittleCar { docType = "car", Make = "Make2" }; var docs = new List<LittleCar> { littleCar1, littleCar2 }; db.SaveArbitraryDocuments(docs, true); var documentIds = db.GetAllDocuments().Select(doc => doc.Id); var loadedCars = db.GetArbitraryDocuments(documentIds, () => new LittleCar()); Assert.AreEqual(littleCar1.Make, loadedCars.ElementAt(0).Make); Assert.AreEqual(littleCar2.Make, loadedCars.ElementAt(1).Make); }
public FacadeV() { littleCar = new LittleCar(3, 4, 1200); bigCar = new BigCar(5, 5, 2000); minivan = new Minivan(5, 7, 3000); }
static void Main(string[] args) { //------------------------------------------------------------------------------------------- // PARTE FACADE //------------------------------------------------------------------------------------------- FacadeV facade = new FacadeV(); facade.ShowCars(); facade.ShowVehicles(); //------------------------------------------------------------------------------------------- // PARTE XML y REFLECTION //------------------------------------------------------------------------------------------- // ------> REFLECTION <----- XmlDocument doc = new XmlDocument(); doc.Load("ReflectionConfiguration.xml"); //-------------------- XML OPCION 1 - Xpath ----------------------------------------------------------- //Recogemos del XML el namespace y la clase XmlNodeList elemList = doc.GetElementsByTagName("Type"); string ns = elemList[0].InnerText; XPathNavigator nav = doc.CreateNavigator(); //XmlNode root2 = doc.DocumentElement; XPathNavigator node = nav.SelectSingleNode("/Types/Type[@id='LittleCar']"); Console.WriteLine(node.ToString()); //------------------- XML OPCION 2 LINQ------------------------------------------------------- XElement root = XElement.Load("ReflectionConfiguration.xml"); IEnumerable <XElement> tests = from el in root.Elements("Type") where (string)el.Attribute("id") == "LittleCar" select el; string cadena = tests.First().Value; Console.WriteLine(cadena); //------------------------------------------------------------------------ //Cargas assembly en mmeoria ram Assembly myAssembly = typeof(Program).Assembly; //Cargas clase en memoria ram Type littleCarType = myAssembly.GetType(node.ToString()); //Creas objeto en la Ram object littleCar = Activator.CreateInstance(littleCarType, 3, 5, 1000); //Casteas de objeto a LittleCar en una variable LittleCar coche = (LittleCar)littleCar; Console.WriteLine("Tipo de coche: " + coche.GetType() + "\nCilindrada: " + coche.Cc + "\nPuertas: " + coche.Doors + "\nAsientos: " + coche.Seating); }