You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
ASP.NET Core 2.0 applications can use Microsoft.DependencyInjection services, installing with this package you can auto integrate StructureMap in one single row.
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);
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.