Skip to content

Latest commit

 

History

History
99 lines (66 loc) · 3.47 KB

File metadata and controls

99 lines (66 loc) · 3.47 KB

Generic Repository Pattern for Entity Framework

!! IMPORTANT

Find the version for Entity Framework Core here

https://github.com/OfferingSolutions/Entity-Framework-Core-Generic-Repository


NuGet

Offering you a complete abstraction of the UnitOfWork-Pattern with the basic CRUD-Operations, the Repository Pattern and extended functions like CustomRepositores all in one small lib. Made for the Entity Framework.

See the Sample-Project how this works.

See Nuget to load this package:

https://www.nuget.org/packages/OfferingSolutions.UnitOfWork.Structure/

Install-Package OfferingSolutions.UnitOfWork.Structure

Have fun. Hope this helps :)

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 allPersons = unitOfWorkContext.GetAll().ToList();

    // Get all Persons with the age of 35
    List allPersonsOnAge35 = unitOfWorkContext.GetAll(x => x.Age == 35).ToList();

    // Get all Persons with the age of 35 ordered by Name
    List allPersonsOnAge35Ordered = unitOfWorkContext.GetAll(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 allPersonsOnAge35OrderedAndWithThings = unitOfWorkContext.GetAll(
        x => x.Age == 35,
        orderBy: q => q.OrderBy(d => d.Name),
        includeProperties: "Things").ToList();

    // Get all Persons and include its properties
    List allPersonsWithThings = unitOfWorkContext.GetAll(includeProperties: "Things").ToList();

    // Find a single Person with a specific name
    Person findBy = unitOfWorkContext.GetSingle(x => x.Name == "Fabian");

    // Find a single Person with a specific name and include its siblings
    Person findByWithThings = unitOfWorkContext.GetSingle(x => x.Name == "Fabian", includeProperties: "Things");

    // Find a person by id 
    unitOfWorkContext.GetSingleById(6);

    //Update an existing person
    unitOfWorkContext.Update(person);

    //Add or Update a Person
    unitOfWorkContext.AddOrUpdate(person);

    //Deleting a Person by Id or by entity
    //unitOfWorkContext.Delete(person.Id);
    unitOfWorkContext.Delete(person);
}

or you can make repositories for each entity

DatabaseContext databaseContext = new DatabaseContext();
IPersonRepository personRepository = new PersonRepository(databaseContext);
IThingRepository thingRepository = new ThingRepository(databaseContext);

personRepository.Add(new Person());
thingRepository.Add(new Thing());

personRepository.Save();

List persons = await personRepository.GetAllASync().Result.ToListAsync();

Console.WriteLine(persons.Count);
personRepository.MyNewFunction(6);
await personRepository.SaveASync();
List allASync = await personRepository.GetAllASync().Result.ToListAsync();

thingRepository.Dispose();
personRepository.Dispose();