Skip to content

Getting started

Omar Piani edited this page Feb 26, 2020 · 30 revisions

Getting Started

Install from Nuget

SharpRepository.Repository is the core of our application, you need a specific implementation in order to use it.

For Entity Framework 6 repository you have to install SharpRepository.EfRepository with command

Install-Package SharpRepository.EfRepository
  • SharpRepository.EfRepository
  • SharpRepository.EfCoreRepository
  • SharpRepository.MongoDbRepository
  • SharpRepository.InMemoryRepository
  • SharpRepository.CacheRepository -Prerelease
  • SharpRepository.XmlRepository Documentation
  • SharpRepository.RavenDbRepository -Prerelease
  • SharpRepository.Db4oRepository -Prerelease

now you can instatiate your repository, for instance:

new InMemoryRepository<Order>();

Configure

You can create manually your configuration with SharpRepositoryConfiguration class and pass it to RepositoryFactory methods.

We provide a json configuration reader for your configuration. SharpRepository.Repository package puts in your project a file called repository.default.json with an example of all advanced settings, each single implementation package then places an other json file with the specific options of your implementation.

You can get your configuration object with from json file with this code:

var config = new ConfigurationBuilder()
    .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
    .AddJsonFile(jsonConfigurationFileName)
    .Build();

var section = config.GetSection(sharpRepositoryConfigurationSectionName);
ISharpReposiotoryConfiguration sharpConfig = RepositoryFactory.BuildSharpRepositoryConfiguation(section);

The best way is using dependency resolution, using our IoC plugins will register in your containers IRepository<> and ICompoundRepository<> interfaces with the configuration you provide.

But the most common usage is with MVC projects where configuration, as we see, is really simple and automatically your correct implementation will be passed to your Controller constructors!

ASP.NET MVC5 and WebApi

Install-Package SharpRepository.Ioc.Mvc Installs StructureMap as DependencyResolver for Mvc and WebApi.

Edit your Global.asax.cs file, and add in your Application_Start method

a line like MvcDependencyResolver.ForRepositoriesUseSharpRepository("repository.json", "sharpRepository");

where repository.json is your json repository configuration and sharpRepository is the section inside.

Here there's a Sample MVC5 project

ASP.NET Core 2.0 Applications

ASP.NET Core 2.0 applications can use Microsoft.DependencyInjection services, installing with this package you can auto integrate StructureMap in one single row.

Install-Package SharpRepository.Ioc.Microsoft.DependencyInjection

in your Startup.cs file add return services.UseSharpRepository(..) and change ConfigureServices return type from void to IServiceProvider

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    return services.UseSharpRepository(Configuration.GetSection("sharpRepository"));
}

where "sharpRepository" is the SharpRepository name in your "appsettings.json" file.

Here there's a Sample MVC Core project

ASP.NET Core 3.0 Applications

New Microsoft.DependencyInjection 3.0 has some breaking change, you must use a supported DI Resolver, for instance Autofac.

so you need to:

Install-Package Autofac.Extensions.DependencyInjection
Install-Package SharpRepository.Ioc.Autofac

in Program.cs you have to configure with .UseServiceProviderFactory(new AutofacServiceProviderFactory()) your IHostBuilder

in Startup.cs you have to add

        public void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegisterSharpRepository(Configuration.GetSection("sharpRepository"), "efCore"); // for Ef Core
        }

where you can add Autofac rules, including our RegisterSharpRepository configuration

at the end of Configure method you must setup respository dependency resolver adding RepositoryDependencyResolver.SetDependencyResolver(app.ApplicationServices);

Here there's a Sample MVC Core 3.0 project

How do I use SharpRepository?

Start by defining your entity type, e.g. Order, which will be managed by the repository.

public class Order
{
   public int OrderId { get; set; }
   public string Name { get; set; }
}

Once you have your Order type, and a reference to SharpRepository, you can instantiate your generic repository of type Order. In the example here, we're using the InMemoryRepository implementation.

var repo = new InMemoryRepository<Order>();

or using your dependency resolver or our Factory

var repo = RepositoryFactory.GetInstance<User, int>(config);

The most basic repository operations are CRUD (create, read, update, delete).

// Create 
var create = new Order { Name = "Big sale" };
repo.Add(create);

const int expectedOrderId = 1;
create.OrderId.ShouldEqual(expectedOrderId);

// Read 
var read = repo.Get(expectedOrderId);
read.Name.ShouldEqual(create.Name);
        
// Update
read.Name = "Really big sale";
repo.Update(read);
        
var update = repo.Get(expectedOrderId);
update.OrderId.ShouldEqual(expectedOrderId);
update.Name.ShouldEqual(read.Name);

// Delete
repo.Delete(update);
var delete = repo.Get(expectedOrderId);
delete.ShouldBeNull();  

Next you can query your repository to find an entity or multiple entities.

// Find
var find = repo.Find(x => x.Name == "Big sale");
find.OrderId.ShouldEqual(expectedOrderId);

// Add multiple new orders
var newOrders = new List<Order> { 
    new Order { Name = "New Order 2" }, 
    new Order { Name = "New Order 3" }, 
    new Order { Name = "New Order 4" }
};
repo.Add(newOrders);

// FindAll
var orders = repo.FindAll(x => x.Name.StartsWith("New Order"));
orders.Count().ShouldEqual(3);

orders = repo.FindAll(x => x.OrderId < 3);
orders.Count().ShouldEqual(2);

// GetAll
var allOrders = repo.GetAll();
allOrders.Count().ShouldEqual(4);

SharpRepository includes additional sample usages and implementation details in the SharpRepository.Samples, SharpRepository.Tests.Integration and SharpRepository.Tests projects.

Configuration Options

For more information on creating your repository instance and the options that are available, check out this blog post: http://www.fairwaytech.com/2013/02/sharprepository-configuration/

Have Questions?

Please use the GitHub issues:

Clone this wiki locally
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy