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 void GenericRepo_Update_Entry_Is_Updated() { TestPerson findByUpdated; 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"); findBy.Name = "Claudio"; unitOfWorkContext.Update(findBy); unitOfWorkContext.Save(); findByUpdated = unitOfWorkContext.GetSingle<TestPerson>(x => x.Name == "Claudio"); } Assert.IsNotNull(findByUpdated); Assert.IsTrue(findByUpdated.Name == "Claudio"); }
public void GenericRepo_Add_Entry_Exists() { List<TestPerson> testPersons; using (IOsUnitOfWorkContext unitOfWorkContext = new OsUnitOfWorkContext(new TestDatabaseContext())) { unitOfWorkContext.Add(new TestPerson()); 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_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"); }