DataRepository 1.0.1
See the version list below for details.
dotnet add package DataRepository --version 1.0.1
NuGet\Install-Package DataRepository -Version 1.0.1
<PackageReference Include="DataRepository" Version="1.0.1" />
paket add DataRepository --version 1.0.1
#r "nuget: DataRepository, 1.0.1"
// Install DataRepository as a Cake Addin
#addin nuget:?package=DataRepository&version=1.0.1
// Install DataRepository as a Cake Tool
#tool nuget:?package=DataRepository&version=1.0.1
DataRepository
Repository pattern implemented to give a generic component for noncoupled data access, taking advantage of the dependency injection mechanism of dotnet core 2 with Entity Framework code first. Useful for scenarious where AutoMapper is used for translating from models to viewmodels.
Data
IDataRepositoryContext
The base interface all context interfaces must implement for the contexts to be injected to repositories.
IRepository<T>
The base interface of the repository itself, wich defines all basic methods to interacting with databases.
Repository<T>
Generic abstract class that implements IRepository<T> interface with all methods to interacting with databases.
IFacade<TSource, TTarget>
The interface that defines methods of the facade that automatically consumes methods from the Repository class and translates makes translations between models and viewmodel.
Facade<TSource, TTarget>
Generic abstract class that implements all methods that consume Repository methods and automatically translates betweem models and viewmodels.
PageEntity
Class used to send and receive information about pagination to facades and repositories.
ResultEntity<T>
Generic class responsible for encapsulating pagination and result set returns.
GetParameter
Generic class responsible for sending complex lists of parameters to search
ViewModelBase
Base class for viewmodels to be translated
MVC
BaseCRUDController<Entity, ViewModelEntity>
Generic abstract class inherited from Controller which is responsible for define a base controller for interaction with a facade specifically typed.
Usage
You have to create inherited classes from Repository<T> and Facade<TSource, TTarget> for each model, then add your database context, your models and view models, inject the Facade into your controller and use. Remember to add all necessary services to your Startup.cs, like the contexts, Repositories and Facades to setup dependency injection. If you only use models, you only need to inherit from Repository<T>, where you type your repository with the type of your model. If you use the ViewModel → Model → ViewModel pattern, with AutoMapper, for example, you also have to inherit the Facade<TSource, TTarget> class for your entities, so that TSource be the type of your model and TTarget the type of your view model. You also can extend the repository and facade specific classes to add more functionality.
Database context
Create context implementing IDataRepositoryContext or an interface that implements IDataRepositoryContext.
public class ApplicationDbContext : DbContext, IDataRepositoryContext
Add your models and child repository classes
1 - Add your model.
public class Book
{
public int Id { get; set; }
public string Name { get; set; }
}
2 - Add a class inherited from Repository<T>
public class BookRepository : Repository<Book>
{
public BookRepository(IDataRepositoryContext contexto) : base(contexto)
{
}
}
3 - Add your viewmodel
public class BookViewModel : ViewModelBase
{
[Required]
public string Name { get; set; }
}
4 Add a class inherited from Facade<TSource, TTarget>
public class BookFacade : Facade<Book, BookViewModel>
{
public BookFacade(IRepository<Book> repository, IMapper mapper) : base(repository, mapper)
{
}
}
5 - Add a controller receiving the facade by dependency injection
public class BooksController : Controller
{
private readonly IFacade<Book, BookViewModel> localFacade;
public BooksController(IFacade<Book, BookViewModel> facade)
{
localFacade = facade;
}
public IActionResult Index()
{
return View(localFacade.GetList());
//Get filtering with lambda
//return View(localFacade.GetList(b => b.Name.Contains("conhec")));
//Get filterin with lambda with pagination
//PageEntity pe = new PageEntity() { ActualPage = 0, PageSize = 50 };
//return View(localFacade.GetListPaged(b => b.Name.StartsWith("co"), pe));
}
public async Task<IActionResult> Add(BookViewModel viewModel)
{
viewModel = await _facade.Add(viewModel, true);
return View(viewModel);
}
public async Task<IActionResult> Update(BookViewModel viewModel)
{
viewModel = await localFacade.Update(viewModel, true);
return View(viewModel);
}
public async Task<IActionResult> Delete(BookViewModel viewModel)
{
await localFacade.Delete(b => b.Id == viewModel.Id);
return View(viewModel);
}
}
6 - Setup dependency injection at Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
//Context services
services.AddScoped<DbContext, ApplicationDbContext>();
services.AddScoped<IDataRepositoryContext, ApplicationDbContext>();
//Repository services
services.AddScoped<IRepository<Book>, BookRepository>();
//Facade services
services.AddScoped<IFacade<Book, BookViewModel>, BookFacade>();
services.AddAutoMapper();
services.AddMvc().AddJsonOptions(opt => opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);
}
A simple and clear example of how to use the library is in the project repository at https://github.com/irawild/DataRepository.Client
In the BooksController there are samples how to consume the repository persistence methods. You can extend, override or overload the methods, of repository. Just pay attention in the need of creating an interface for defining new methods.
Notice that the ApplicationContext implements IDataRepositoryContext interface directly because it's the only context in the application. If you need to inject more contexts, you need to create child interfaces for each context, implement each one the IDataRepositoryContext, so that you can inject DbContext classes for diferente interfaces, instead of trying injecting DbContext classes for DBConext type, where only one will be injected.
Se all of these details in the example client project linked above.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 is compatible. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
-
.NETCoreApp 2.0
- AutoMapper (>= 6.2.1)
- AutoMapper.Extensions.Microsoft.DependencyInjection (>= 3.2.0)
- Microsoft.AspNetCore.All (>= 2.0.3)
- Microsoft.VisualStudio.Web.CodeGeneration.Design (>= 2.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Add more information to metadata