Skip to content

Getting Started

Johelvis Guzman edited this page Dec 22, 2021 · 16 revisions

To get started with DotNetToolkit.Repository, please follow these steps.

Installation

This will install an in-memory database for the repositories to use.

PM> Install-Package DotNetToolkit.Repository.InMemory

How to use

First, we need to define an entity type which will be managed by the repository.

namespace MyApplicationDemo
{
    public class Customer
    {
	public int Id { get; set; }
        public string Name { get; set; }
    }
}

Create a RepositoryOptions object with a registered ORM provider to be used (in this example we will be using the in-memory database). The RepositoryOptions object is built using the RepositoryOptionsBuilder. For more configuration details, please check out the Configuration guide.

Additionally, the repositories can also be configured using the RepositoryDependencyResolver to use an IOC container.

using DotNetToolkit.Repository;
using DotNetToolkit.Repository.Configuration.Options;
using DotNetToolkit.Repository.InMemory;

namespace MyApplicationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // **** Configure Repository ****
	    
            var options = new RepositoryOptionsBuilder()
                .UseInMemoryDatabase()
                .Options;

            // **** Create the repository for a Customer entity ****

            var repo = new Repository<Customer>(options);
        }
    }
}

Now we can start using the functionality from the repository to create, update, delete, and find entities.

using DotNetToolkit.Repository;
using DotNetToolkit.Repository.Configuration.Options;
using DotNetToolkit.Repository.InMemory;

namespace MyApplicationDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // **** Configure Repository ****
	    
            var options = new RepositoryOptionsBuilder()
                .UseInMemoryDatabase()
                .Options;

            // **** Create the repository for a Customer entity ****

            var repo = new Repository<Customer>(options);
			
            // **** Create Items **** 

            var customer = new Customer() { Id = 1, Name = "Random Name" }
			
            repo.Add(customer);

            // **** Find Items **** 
            
           var customerInDb = repo.Find(c=> c.Name.Equals("Random Name"));
			
            // **** Update Items **** 

            customerInDb.Name = "New Random Name";
			
            repo.Update(customerInDb);
			
            // **** Delete Items **** 
            
            repo.Delete(customerInDb);
        }
    }
}

Please check out the following guide on how to apply filtering, pagination, and even sorting using query options.

Clone this wiki locally