public void InteractWithTwoRepositories() { // Creamos dos entidades var vehiculo = new Vehiculo { Matricula = "3376-DFF", Propietario = new Persona { Dni = "53728312S" } }; var servicio = new Servicio { Descripcion = "Robo de piruletas." }; // Los añadimos al contexto _repositoryVehiculo.Create(vehiculo); _repositoryServicio.Create(servicio); // Comprobamos la inserción Assert.AreEqual(1, _context.Vehiculos.Count); Assert.AreEqual(1, _context.Servicios.Count); }
public void CanRunPostMappingEvents() { IRepository <Product> repo = new RepositoryInMemory <Product>() { OnRowsMappedCallBack = (products) => { foreach (var product in products) { product.Model = "after mapped " + product.Model; } } }; Action <IList <Product> > reset = (products) => { foreach (var p in products) { p.Model = p.Model.Replace("after mapped ", ""); } }; var allproducts = GetSampleProducts(); foreach (var product in allproducts) { repo.Create(product); } var first = repo.Get(1); Assert.IsTrue(first.Model.StartsWith("after mapped")); reset(allproducts); var all = repo.GetAll(); foreach (var item in all) { Assert.IsTrue(item.Model.StartsWith("after mapped")); } reset(allproducts); var lookup = repo.ToLookUp(); foreach (var pair in lookup) { Assert.IsTrue(pair.Value.Model.StartsWith("after mapped")); } reset(allproducts); var matches = repo.Find(Query <Product> .New().Where(p => p.Cost).Is(15)); foreach (var match in matches) { Assert.IsTrue(match.Model.StartsWith("after mapped")); } }
public void CanLoadFromRepo() { var repo = new RepositoryInMemory <ConfigItem>(); var items = new List <ConfigItem>() { new ConfigItem() { App = "stockapp", Name = "dev.config", Section = "app", Key = "name", ValType = "System.String", Val = "my app name" }, new ConfigItem() { App = "stockapp", Name = "dev.config", Section = "app", Key = "pagesize", ValType = "System.Int32", Val = "10" }, new ConfigItem() { App = "stockapp", Name = "dev.config", Section = "app", Key = "enableEmails", ValType = "System.Boolean", Val = "False" }, new ConfigItem() { App = "stockapp", Name = "dev.config", Section = "app", Key = "maxAmount", ValType = "System.Double", Val = "20.5" }, new ConfigItem() { App = "stockapp", Name = "dev.config", Section = "app", Key = "businessdate", ValType = "System.DateTime", Val = DateTime.Today.Date.ToString() }, }; // Create in repo. foreach (var item in items) { repo.Create(item); } // Load the items into the config source. var config = new ConfigSourceDb("stockapp", "dev.config", repo, true); // Load from the repo/datasource. Assert.AreEqual("my app name", config["app", "name"]); Assert.AreEqual(10, config["app", "pagesize"]); Assert.AreEqual(false, config["app", "enableEmails"]); Assert.AreEqual(20.5, config["app", "maxAmount"]); Assert.AreEqual(DateTime.Today.Date, config["app", "businessdate"]); }
/// <summary> /// Run the application. /// </summary> public override BoolMessageItem Execute() { // Create In-Memory repository. IRepository <Product> repo = new RepositoryInMemory <Product>("Id,Name,Model,IsInStock,Cost"); var products = GetSampleProducts(); products.ForEach(p => repo.Create(p)); // NOTE: I will not show the Sum, Min, Max, Distinct methods here, please refer to base class // example Example_Repository1_Queryable. Product product = new Product("Porsche", "boxster", 10, true); // 1. Create repo.Create(product); // 2. Get product = repo.Get(product.Id); // 3. Update product.Name = "Ford2"; repo.Update(product); // 4. Delete repo.Delete(product.Id); // 5. Get all IList <Product> all = repo.GetAll(); // 6. Get first using filter. Product prod = repo.First("name = 'Honda'"); // 7. Get by page using sql PagedList <Product> page = repo.Find("cost = 20", 1, 3); // 8. Get by page using criteria. var criteria1 = Query <Product> .New().Where("cost").Is(20); PagedList <Product> page2 = repo.Find(criteria1, 1, 3); // 9. Delete using criteria. var criteria2 = Query <Product> .New().Where(p => p.Name).Is("Skateboard"); repo.Delete(criteria2); // 10. Aggregate using column name, expression, and filter. var t = repo.Count(Query <Product> .New().Where(p => p.Name).Null()); // GROUPING var groups1 = repo.Group <int>(e => e.Cost); // DISTINCT var names = repo.Distinct <string>(e => e.Name); // MIN, MAX, AVG, SUM using the Entity properties. double min = repo.Min(e => e.Cost); double max = repo.Max(e => e.Cost); double sum = repo.Sum(e => e.Cost); double avg = repo.Avg(e => e.Cost); return(BoolMessageItem.True); }
public void CreateTest() { var servicio = _servicioRepository.Create(new Servicio()); Assert.AreEqual(typeof(Servicio), servicio.GetType()); }
public void AddEntityTest() { var servicio = new Servicio(); _repositoryServicio.Create(servicio); Assert.AreEqual(1, _repositoryServicio.Find(s => s.Id == servicio.Id).Count); }
/// <summary> /// Run the application. /// </summary> public override BoolMessageItem Execute() { // First create some test data for the queries. IRepository <Product> repoCrud = new RepositoryInMemory <Product>(); var products = GetSampleProducts(); products.ForEach(p => repoCrud.Create(p)); // NOTES: // 1. The Repository<T>'s base class is RepositoryQueryable. // 2. We are only using a IRepository<T> here initially for adding in-memory test data. // 3. But this could be initialized via new RepositoryQuerable(...) constructors against a real Database. // 4. Notice that IRepositoryQueryable does not know anything about the entity(Product). IRepositoryQueryable repo = (IRepositoryQueryable)repoCrud; // 0. Named filters first, to show their example in each section min, max, group, etc. repo.AddNamedFilter("InStock Filter", Query <object> .New().Where("IsInStock").Is(true)); repo.AddNamedFilter("Expensive", Query <object> .New().Where("Cost").MoreThan(20)); // 1. GROUPING var groups = repo.Group <int>("Cost"); var groups2 = repo.Group <int>("Cost", Query <object> .New().Where("Make").Not("Honda")); var groups3 = repo.Group <int>("Cost", "Expensive"); // same as groups2. // 2. DISTINCT var names = repo.Distinct <string>("Make"); var names2 = repo.Distinct <string>("Make", Query <object> .New().Where("Cost").MoreThan(30).OrderByDescending("Make")); var names3 = repo.Distinct <string>("Make", "Expensive"); // 3. COUNT var count1 = repo.Count(); var count2 = repo.Count(Query <object> .New().Where("IsInStock").Is(true)); var count3 = repo.Count("InStock Filter"); // 4. MIN, MAX, AVG, SUM int count = repo.Count(); double min = repo.Min("Cost"); double min2 = repo.Min("Cost", Query <object> .New().Where("Make").In <string>("Bmw", "Lexus")); double min3 = repo.Min("Cost", "InStock Filter"); double max = repo.Max("Cost"); double max2 = repo.Max("Cost", Query <object> .New().Where("Make").NotIn <string>("Bmw", "Lexus")); double max3 = repo.Max("Cost", "InStock Filter"); double sum = repo.Sum("Cost"); double sum2 = repo.Sum("Cost", Query <object> .New().Where("Make").In <string>("Honda", "Toyota").And("IsInStock").Is(true)); double sum3 = repo.Sum("Cost", "InStock Filter"); double avg = repo.Avg("Cost"); double avg2 = repo.Avg("Cost", Query <object> .New().Where("Make").Like("Nissan")); double avg3 = repo.Avg("Cost", "InStock Filter"); // 5. To Table DataTable all = repo.ToTable(); DataTable all2 = repo.ToTable(Query <object> .New().Where("Make").Is("Honda")); DataTable all3 = repo.ToTable("InStock Filter"); // 6. Exists bool any1 = repo.Any(Query <object> .New().Where("IsInStock").Is(true)); bool any2 = repo.Any("InStock Filter"); // 7. Limit number of records. repo.ToTable(Query <object> .New().Limit(2)); var table1 = repo.ToTable(Query <object> .New().Select("Make", "Model").Where("Cost").MoreThan(30).Limit(2)); return(BoolMessageItem.True); }