valet 2.0.1
See the version list below for details.
dotnet add package valet --version 2.0.1
NuGet\Install-Package valet -Version 2.0.1
<PackageReference Include="valet" Version="2.0.1" />
<PackageVersion Include="valet" Version="2.0.1" />
<PackageReference Include="valet" />
paket add valet --version 2.0.1
#r "nuget: valet, 2.0.1"
#addin nuget:?package=valet&version=2.0.1
#tool nuget:?package=valet&version=2.0.1
VALET
Valet is a simple library designed to help developers streamline their application setup. It consists of two main modules:
INTRODUCTION
Valet simplifies authentication and data persistence in .NET applications by providing pre-configured tools for repositories, unit of work, and authentication management. It reduces boilerplate code, enforces best practices, and integrates seamlessly with Entity Framework Core.
INSTALLATION
To install Valet, add the NuGet package:
Install-Package valet
Or via .NET CLI:
dotnet add package valet
π NuGet Package: Valet on NuGet
VALET.CORE
Valet Core provides two key features:
Generic Repository and Unit of Work
Developers can extend the generic repository to access common repository operations. A Unit of Work repository is also available, pre-configured with a commit operation.
Base Entity
A default base entity is provided for extension, including essential properties for any entity. This base entity can be utilized within your application, just as it is in all entities of the "Auth" module.
VALET.AUTH
Valet Auth offers a comprehensive authentication toolkit, including the following features:
Password Hasher
A password hasher that securely encrypts user passwords using BCrypt.
Token Generator
A JWT token generator that includes relevant user information.
AuthDbContext
An authentication database context that can be extended, with pre-configured user logic.
Entities
Three entities are provided: User
, Role
, and UserRole
. These entities can be extended and customized as needed.
TECHNICAL SPECIFICATIONS
Configuration Setup
All necessary configuration options for using Valet's modules are provided in the configuration module.
Example Usage
Below is an example of how to configure Valet services in your application:
builder.Services.AddValet<AppDbContext>(builder.Configuration, true)
.UsePasswordHasher()
.UseTokenGenerator(builder.Configuration)
.UseValetJwt(builder.Configuration)
.UseValetSwaggerGen();
DbContext Setup
Your DbContext
must extend AuthDbContext
:
public class AppDbContext(DbContextOptions options) : AuthDbContext(options)
OnModelCreating
To enable Valet's features, such as entity mapping for authentication module classes, override OnModelCreating
and call the base implementation:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(AppDbContext).Assembly);
}
After applying the base configurations, you can add your custom configurations, including new mappings. Note that ApplyConfigurationsFromAssembly
automatically applies configuration mappings.
Customizing DbContext
You can add new DbSet
properties to your DbContext
and override existing ones when extending Valet's classes:
public DbSet<Wallet> Wallets { get; set; }
public DbSet<Transaction> Transactions { get; set; }
public DbSet<Recurrency> Recurrencies { get; set; }
Example of extending User
with additional properties:
public DbSet<LocalUser> LocalUsers { get; set; } // Adds new persistent properties
Configuration Settings
For secure setup, define your secret key using the following pattern:
"Settings": {
"Secret": "YOURSECRETKEY"
}
This allows Valet to access your secret key through builder.Configuration
.
API Reference
Repositories
UserRepository
β Extends the generic repository with additional operations:UserExists(string email)
: Checks if a user with the given email exists.GetUserWithRoleAsync(string email)
: Retrieves a user along with their assigned roles.
RoleRepository
β Provides default repository operations plus:RoleExistsAsync(string name)
: Checks if a role with the given name exists.
UserRoleRepository
β Manages user-role assignments with standard repository operations.
Interfaces
IRepository<T>
β Base interface for repositoriesIUnitOfWork
β Manages transactionsIPasswordHasher
β Handles password hashingITokenGenerator
β Generates JWT tokens
Classes
Repository<T>
β Implements repository operationsUnitOfWork
β Manages commitsPasswordHasher
β Implements password hashingTokenGenerator
β Generates JWT
Dependency Injection
All these repositories are automatically registered in the Dependency Injection container when you configure AddValet()
with true for UseAuthentication
. If you set it to false, only the UnitOfWork will be available in the DI container.
Similarly, other modules can be added individually to the DI container, such as UsePasswordHasher()
.
Database Commit
Nothing in the repositories uses EF Coreβs SaveChanges()
method to commit database operations. The responsibility of committing changes lies with the user, providing greater flexibility to manage database transactions.
With this pattern, operations do not automatically force a database commit, allowing users to control when changes are persisted.
FAQs & Troubleshooting
Why is my token not being generated?
Ensure you have configured the secret key correctly in appsettings.json
.
How can I override default User properties?
Extend the User
entity and update your DbContext
to use the new class.
Can I use Valet without Entity Framework?
Yes, but you must implement custom repository and UnitOfWork
patterns. You can replace the default GenericRepository<T>
and UnitOfWork
with your own implementations that use another data access approach, such as Dapper or raw SQL queries.
What if I forget to call Commit()?
Since repositories do not automatically call SaveChanges()
, you must explicitly invoke Commit()
on the UnitOfWork to persist changes. Failing to do so will result in data modifications not being saved to the database.
Conclusion
Valet simplifies authentication and data management in .NET applications. With its generic repositories, authentication tools, and extensible architecture, it reduces development time and enforces best practices.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- BCrypt.Net-Next (>= 4.0.3)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.14)
- Microsoft.EntityFrameworkCore (>= 9.0.3)
- Microsoft.EntityFrameworkCore.SqlServer (>= 9.0.3)
- Swashbuckle.AspNetCore.SwaggerGen (>= 7.3.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.