public void GenericRepo_Added_TestPersons_Get_Ordered_Descending() { List <TestPerson> testPersons; using (IOsUnitOfWorkContext unitOfWorkContext = new OsUnitOfWorkContext(new TestDatabaseContext())) { unitOfWorkContext.Add(new TestPerson { Age = 28, Name = "Zebra" }); unitOfWorkContext.Add(new TestPerson { Age = 28, Name = "Adam" }); unitOfWorkContext.Add(new TestPerson { Age = 28, Name = "Fabian" }); unitOfWorkContext.Save(); testPersons = unitOfWorkContext.GetAll <TestPerson>(orderBy: q => q.OrderByDescending(x => x.Name)).ToList(); } Assert.IsNotNull(testPersons); Assert.IsTrue(testPersons[2].Name == "Adam"); Assert.IsTrue(testPersons[1].Name == "Fabian"); Assert.IsTrue(testPersons[0].Name == "Zebra"); }
public void GenericRepo_Add_Without_Save_Entry_Does_Not_Exists() { List <TestPerson> testPersons; using (IOsUnitOfWorkContext unitOfWorkContext = new OsUnitOfWorkContext(new TestDatabaseContext())) { unitOfWorkContext.Add(new TestPerson()); testPersons = unitOfWorkContext.GetAll <TestPerson>().ToList(); } Assert.IsTrue(testPersons.Count == 0); }
public void GenericRepo_AddOrUpdate_Add_Entry_Exists() { List <TestPerson> testPersons; using (IOsUnitOfWorkContext unitOfWorkContext = new OsUnitOfWorkContext(new TestDatabaseContext())) { unitOfWorkContext.AddOrUpdate(new TestPerson { Age = 28, Name = "Fabian" }); unitOfWorkContext.Save(); testPersons = unitOfWorkContext.GetAll <TestPerson>().ToList(); } Assert.IsTrue(testPersons.Count == 1); }
public void GenericRepo_Delete_Entry_With_Id_Is_Deleted() { List <TestPerson> testPersons; using (IOsUnitOfWorkContext unitOfWorkContext = new OsUnitOfWorkContext(new TestDatabaseContext())) { unitOfWorkContext.Add(new TestPerson { Age = 28, Name = "Fabian" }); unitOfWorkContext.Save(); TestPerson findBy = unitOfWorkContext.GetSingle <TestPerson>(x => x.Name == "Fabian"); unitOfWorkContext.Delete <TestPerson>(findBy.Id); unitOfWorkContext.Save(); testPersons = unitOfWorkContext.GetAll <TestPerson>().ToList(); } Assert.IsTrue(testPersons.Count == 0); }
public void GenericRepo_AddOrUpdate_Update_Entry_Is_Updated() { TestPerson findByUpdated; using (IOsUnitOfWorkContext unitOfWorkContext = new OsUnitOfWorkContext(new TestDatabaseContext())) { unitOfWorkContext.AddOrUpdate(new TestPerson { Age = 28, Name = "Fabian" }); unitOfWorkContext.Save(); TestPerson findBy = unitOfWorkContext.GetSingle <TestPerson>(x => x.Name == "Fabian"); findBy.Name = "Claudio"; unitOfWorkContext.AddOrUpdate(findBy); unitOfWorkContext.Save(); findByUpdated = unitOfWorkContext.GetSingle <TestPerson>(x => x.Name == "Claudio"); } Assert.IsNotNull(findByUpdated); Assert.IsTrue(findByUpdated.Name == "Claudio"); }
public void GenericRepo_Stopwatch_Generate_Generic_Repo() { Stopwatch stopwatch = new Stopwatch(); TimeSpan timeSpan1, timeSpan2; using (IOsUnitOfWorkContext unitOfWorkContext = new OsUnitOfWorkContext(new TestDatabaseContext())) { stopwatch.Start(); unitOfWorkContext.GetAll <TestPerson>(); stopwatch.Stop(); timeSpan1 = stopwatch.Elapsed; stopwatch.Reset(); stopwatch.Start(); unitOfWorkContext.GetAll <TestPerson>(); stopwatch.Stop(); timeSpan2 = stopwatch.Elapsed; stopwatch.Reset(); } Console.WriteLine(timeSpan1); Console.WriteLine(timeSpan2); Assert.IsTrue(timeSpan2 < timeSpan1); }
static void Main(string[] args) { Program program = new Program(); program.InitializeDataBase(); using (IOsUnitOfWorkContext unitOfWorkContext = new OsUnitOfWorkContext(new DatabaseContext())) { Person person = new Person() { Age = 28, Name = "Fabian" }; //Adding a new Entity, for example "Person" unitOfWorkContext.Add(person); //Savechanges unitOfWorkContext.Save(); //or... unitOfWorkContext.SaveASync(); // Get all Persons List <Person> allPersons = unitOfWorkContext.GetAll <Person>().ToList(); // Get all Persons with the age of 35 List <Person> allPersonsOnAge35 = unitOfWorkContext.GetAll <Person>(x => x.Age == 35).ToList(); // Get all Persons with the age of 35 ordered by Name List <Person> allPersonsOnAge35Ordered = unitOfWorkContext.GetAll <Person>(x => x.Age == 35, orderBy: q => q.OrderBy(d => d.Name)).ToList(); // Get all Persons with the age of 35 ordered by Name and include its properties List <Person> allPersonsOnAge35OrderedAndWithThings = unitOfWorkContext.GetAll <Person>( x => x.Age == 35, orderBy: q => q.OrderBy(d => d.Name), includeProperties: "Things").ToList(); // Get all Persons and include its properties List <Person> allPersonsWithThings = unitOfWorkContext.GetAll <Person>(includeProperties: "Things").ToList(); // Find a single Person with a specific name Person findBy = unitOfWorkContext.GetSingle <Person>(x => x.Name == "Fabian"); // Find a single Person with a specific name and include its siblings Person findByWithThings = unitOfWorkContext.GetSingle <Person>(x => x.Name == "Fabian", includeProperties: "Things"); // Find a person by id unitOfWorkContext.GetSingleById <Person>(6); //Update an existing person unitOfWorkContext.Update(person); //Add or Update a Person unitOfWorkContext.AddOrUpdate <Person>(person); //Deleting a Person by Id or by entity //unitOfWorkContext.Delete<Person>(person.Id); unitOfWorkContext.Delete(person); } program.PerformDatabaseOperations(); Console.ReadLine(); }
public static void Main(string[] args) { try { using (IOsUnitOfWorkContext unitOfWorkContext = new OsUnitOfWorkContext(new DataBaseContext())) { Person person = new Person() { Age = 35, Name = "Fabian", Things = new List <Thing>() { new Thing() { Name = "A Thing" } } }; // Adding a new Entity, for example "Person" unitOfWorkContext.Add(person); // Savechanges unitOfWorkContext.Save(); unitOfWorkContext.SaveASync(); // Get all Persons var persons = unitOfWorkContext.GetAll <Person>().ToList(); var personsAsync = unitOfWorkContext.GetAllAsync <Person>(); foreach (var item in persons) { Console.WriteLine(item.Name + " " + item.Age); } List <Person> allPersonsOnAge35 = unitOfWorkContext.GetAll <Person>(x => x.Age == 35).ToList(); var countAll = unitOfWorkContext.Count <Person>(); var countAllWithpredicate = unitOfWorkContext.Count <Person>(x => x.Age == 35); Console.WriteLine(allPersonsOnAge35.Count); // Get all Persons with the age of 35 ordered by Name List <Person> allPersonsOnAge35Ordered = unitOfWorkContext.GetAll <Person>(x => x.Age == 35, orderBy: q => q.OrderBy(d => d.Name)).ToList(); // Get all Persons with the age of 35 ordered by Name and include its properties Func <IQueryable <Person>, IIncludableQueryable <Person, object> > include = source => source.Include(y => y.Things); List <Person> allPersonsOnAge35OrderedAndWithThings = unitOfWorkContext.GetAll <Person>( x => x.Age == 35, orderBy: q => q.OrderBy(d => d.Name), include: include) .ToList(); // Get all Persons and include its properties List <Person> allPersonsWithThings = unitOfWorkContext.GetAll <Person>(include: include).ToList(); // Find a single Person with a specific name, is null if not found Person findBy = unitOfWorkContext.GetSingle <Person>(x => x.Id == 6); var findByASync = unitOfWorkContext.GetSingleAsync <Person>(x => x.Id == 6); // Find a single Person with a specific name and include its siblings Person findByWithThings = unitOfWorkContext.GetSingle <Person>(x => x.Name == "Fabian", include: include); // Find a person by id but throws exception if not found // var personWithIdMayThrowException = unitOfWorkContext.GetSingleBy<Person>(x => x.Id == 6); // var personWithIdMayThrowExceptionASync = unitOfWorkContext.GetSingleByASync<Person>(x => x.Id == 6); // Update an existing person Person findOneToUpdate = unitOfWorkContext.GetSingle <Person>(x => x.Name == "Fabian"); findOneToUpdate.Name = "Fabian2"; unitOfWorkContext.Update(person); unitOfWorkContext.Save(); Person findOneAfterUpdate = unitOfWorkContext.GetSingle <Person>(x => x.Name == "Fabian2"); // Deleting a Person by Id or by entity unitOfWorkContext.Delete(person); } /////////////////////////////////////////////////////////////////// // Custom repositories /////////////////////////////////////////////////////////////////// using (IPersonRepository personRepository = new PersonRepository(new DataBaseContext())) { personRepository.Add(new Person() { Name = "John Doe" }); personRepository.Save(); var receivedPerson = personRepository.GetSingle(x => x.Name == "John Doe", source => source.Include(y => y.Things)); var allPersons = personRepository.GetAll(x => x.Name == "John Doe"); var allPersonCountWithPredicate = personRepository.Count(x => x.Name == "John Doe"); var allPersonsWithThings = personRepository.GetAll(x => x.Name == "John Doe", source => source.Include(y => y.Things), q => q.OrderBy(d => d.Name)); Console.WriteLine(receivedPerson); // Does something good ... personRepository.MyNewFunction(6); } Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex); throw; } }