Ejemplo n.º 1
0
        private static void TestAllOperations()
        {
            // Repository
            var repository = new DbRepository <SqlConnection>(RepoDbConnectionString, ConnectionPersistency.Instance);

            // Truncate
            repository.Truncate <Animal>();

            // Count
            Console.WriteLine($"Counting Person Records: {repository.Count<Person>()}");
            Console.WriteLine($"Counting Animal Records: {repository.Count<Animal>()}");

            // BatchQuery
            Console.WriteLine("BatchQuery Person");
            var batchQueryResult = repository.BatchQuery <Person>(0, 1000, OrderField.Parse(new { Id = Order.Descending }));

            // Query 100K
            Console.WriteLine("Query Person: 100K");
            var queryResult = repository.Query <Person>(new
            {
                Id = new
                {
                    Operation = Operation.GreaterThan,
                    Value     = 100
                }
            }, top: 100000);

            // BulkInsert
            Console.WriteLine("BulkInsert Person: 100K");
            var bulkInsertResult = repository.BulkInsert(queryResult);

            // Insert with Guid Primary Key
            Console.WriteLine("Insert with Guid PrimaryKey");
            var animalId = repository.Insert(new Animal()
            {
                Id           = Guid.NewGuid(),
                Name         = $"Name: {Guid.NewGuid().ToString()}",
                Address      = $"Address: {Guid.NewGuid().ToString()}",
                DateInserted = DateTime.UtcNow,
                DateUpdated  = DateTime.UtcNow
            });

            // Verify
            Console.WriteLine($"Verify Insert with Guid PrimaryKey: {animalId}");
            var animal = repository.Query <Animal>(animalId).FirstOrDefault();

            if (animal == null)
            {
                throw new NullReferenceException("Animal is null.");
            }

            // Insert with Identity PrimaryKey
            Console.WriteLine("Insert with Identity PrimaryKey");
            var personId = repository.Insert(new Person()
            {
                Name         = $"Name: {Guid.NewGuid().ToString()}",
                Address      = $"Address: {Guid.NewGuid().ToString()}",
                DateInserted = DateTime.UtcNow,
                DateOfBirth  = DateTime.UtcNow.Date.AddYears(-32),
                DateUpdated  = DateTime.UtcNow,
                Worth        = new Random().Next(30000, 60000)
            });

            // Verify
            Console.WriteLine($"Verify Insert with Identity PrimaryKey: {personId}");
            var person = repository.Query <Person>(personId).FirstOrDefault();

            if (person == null)
            {
                throw new NullReferenceException("Person is null.");
            }

            // Check InlineInsert with Guid
            Console.WriteLine($"InlineInsert Animal");
            animalId = repository.InlineInsert <Animal>(new
            {
                Id      = Guid.NewGuid(),
                Name    = $"NAME-{Guid.NewGuid().ToString()} - InlineInsert",
                Address = $"ADDR-{Guid.NewGuid().ToString()} - InlineInsert"
            });

            // Verify
            Console.WriteLine($"Verify InlineInsert with Guid PrimaryKey: {animalId}");
            animal = repository.Query <Animal>(animalId).FirstOrDefault();
            if (animal == null)
            {
                throw new NullReferenceException("Animal is null.");
            }

            // Check InlineInsert with Identity
            Console.WriteLine($"InlineInsert with Identity PrimaryKey");
            personId = repository.InlineInsert <Person>(new
            {
                Name    = $"NAME-{Guid.NewGuid().ToString()} - InlineInsert",
                Address = $"ADDR-{Guid.NewGuid().ToString()} - InlineInsert"
            });

            // Verify
            Console.WriteLine($"Verify Insert with Identity PrimaryKey: {personId}");
            person = repository.Query <Person>(personId).FirstOrDefault();
            if (person == null)
            {
                throw new NullReferenceException("Person is null.");
            }

            // InlineUpdate
            Console.WriteLine("InlineUpdate with Guid PrimaryKey");
            var affectedRows = repository.InlineUpdate <Animal>(new
            {
                Name    = $"NAME-{Guid.NewGuid().ToString()} - InlineUpdate",
                Address = $"ADDR-{Guid.NewGuid().ToString()} - InlineUpdate"
            },
                                                                new
            {
                Id = animalId
            });

            // Verify
            Console.WriteLine($"Verify InlineUpdate with Guid PrimaryKey: {personId}");
            if (affectedRows <= 0)
            {
                throw new Exception("No rows has been affected by the inline update.");
            }
            animal = repository.Query <Animal>(animalId).FirstOrDefault();
            if (animal == null)
            {
                throw new NullReferenceException("Animal is null.");
            }

            // InlineUpdate
            Console.WriteLine("InlineUpdate with Identity PrimaryKey");
            affectedRows = repository.InlineUpdate <Person>(new
            {
                Name    = $"NAME-{Guid.NewGuid().ToString()} - InlineUpdate",
                Address = $"ADDR-{Guid.NewGuid().ToString()} - InlineUpdate"
            },
                                                            new
            {
                Id = personId
            });

            // Verify
            Console.WriteLine($"Verify InlineUpdate with Identity PrimaryKey: {personId}");
            if (affectedRows <= 0)
            {
                throw new Exception("No rows has been affected by the inline update.");
            }
            person = repository.Query <Person>(personId).FirstOrDefault();
            if (person == null)
            {
                throw new NullReferenceException("Person is null.");
            }

            // Check InlineMerge
            Console.WriteLine($"InlineMerge with Guid PrimaryKey: {animalId}");
            affectedRows = repository.InlineMerge <Animal>(new
            {
                Id      = animalId,
                Name    = $"{animal.Name} - InlineMerge",
                Address = $"{animal.Name} - InlineMerge"
            });

            // Verify
            Console.WriteLine($"Verify InlineMerge with Guid PrimaryKey: {animalId}");
            if (affectedRows <= 0)
            {
                throw new Exception("No rows has been affected by the inline merge.");
            }
            animal = repository.Query <Animal>(animalId).FirstOrDefault();
            if (animal == null)
            {
                throw new NullReferenceException("Animal is null.");
            }

            // Check InlineMerge
            Console.WriteLine($"InlineMerge with Identity PrimaryKey: {personId}");
            affectedRows = repository.InlineMerge <Person>(new
            {
                Id      = personId,
                Name    = $"{person.Name} - InlineMerge",
                Address = $"{person.Name} - InlineMerge"
            });

            // Verify
            Console.WriteLine($"Verify InlineMerge with Identity PrimaryKey: {personId}");
            if (affectedRows <= 0)
            {
                throw new Exception("No rows has been affected by the inline merge.");
            }
            person = repository.Query <Person>(personId).FirstOrDefault();
            if (person == null)
            {
                throw new NullReferenceException("Person is null.");
            }

            // Update
            Console.WriteLine($"Update Person: {personId}");
            person.Name        = $"Name: {Guid.NewGuid().ToString()} (Updated)";
            person.Address     = $"Address: {Guid.NewGuid().ToString()} (Updated)";
            person.DateUpdated = DateTime.UtcNow;
            person.DateOfBirth = DateTime.UtcNow;
            affectedRows       = repository.Update(person);

            // Verify
            Console.WriteLine($"Verify Person after Update: {personId}");
            if (affectedRows <= 0)
            {
                throw new Exception("No rows has been affected by the update.");
            }
            person = repository.Query <Person>(personId).FirstOrDefault();
            if (person == null)
            {
                throw new NullReferenceException("Person is null.");
            }

            // Merge
            Console.WriteLine($"Merge: {personId}");
            person.Name        = $"{Guid.NewGuid().ToString()} (Merged)";
            person.Address     = $"Address: {Guid.NewGuid().ToString()} (Merged)";
            person.DateUpdated = DateTime.UtcNow;
            affectedRows       = repository.Merge(person, Field.Parse(new { person.Id }));

            // Verify
            Console.WriteLine($"Query Person After Merge: {personId}");
            if (affectedRows <= 0)
            {
                throw new Exception("No rows has been affected by the merge.");
            }
            person = repository.Query <Person>(personId).FirstOrDefault();
            if (person == null)
            {
                throw new NullReferenceException("Person is null.");
            }

            // Delete
            Console.WriteLine($"Delete Person: {personId}");
            affectedRows = repository.Delete <Person>(personId);

            // Verify
            Console.WriteLine($"Verify Person After Delete: {personId}");
            if (affectedRows <= 0)
            {
                throw new Exception("No rows has been affected by the delete.");
            }
            person = repository.Query <Person>(personId).FirstOrDefault();
            if (person != null)
            {
                throw new NullReferenceException("Person should be null. We have just deleted it.");
            }

            // Count
            Console.WriteLine($"Person Records: {repository.Count<Person>()}");
            Console.WriteLine($"Animal Records: {repository.Count<Animal>()}");

            // Dispose
            repository.Dispose();
        }
Ejemplo n.º 2
0
 public long Count()
 {
     return(repository.Count <T>("*", QueryBuilder.WhereStatement));
 }