|
| 1 | +# Usage |
| 2 | + |
| 3 | +## Installation |
| 4 | + |
| 5 | +To install the `WeihanLi.EntityFramework` package, you can use the NuGet Package Manager, .NET CLI, or PackageReference in your project file. |
| 6 | + |
| 7 | +**.NET CLI** |
| 8 | + |
| 9 | +``` |
| 10 | +dotnet add package WeihanLi.EntityFramework |
| 11 | +``` |
| 12 | + |
| 13 | +## Configuration |
| 14 | + |
| 15 | +To configure the `WeihanLi.EntityFramework` package, you need to add the necessary services to your `IServiceCollection` in the `Startup.cs` or `Program.cs` file. |
| 16 | + |
| 17 | +```csharp |
| 18 | +public void ConfigureServices(IServiceCollection services) |
| 19 | +{ |
| 20 | + services.AddDbContext<MyDbContext>(options => |
| 21 | + options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); |
| 22 | + |
| 23 | + services.AddEFRepository(); |
| 24 | + services.AddEFAutoUpdateInterceptor(); |
| 25 | + services.AddEFAutoAudit(builder => |
| 26 | + { |
| 27 | + builder.WithUserIdProvider<CustomUserIdProvider>() |
| 28 | + .EnrichWithProperty("MachineName", Environment.MachineName) |
| 29 | + .WithStore<AuditConsoleStore>(); |
| 30 | + }); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## Examples |
| 35 | + |
| 36 | +### Repository Pattern |
| 37 | + |
| 38 | +The `WeihanLi.EntityFramework` package provides a repository pattern implementation for Entity Framework Core. |
| 39 | + |
| 40 | +```csharp |
| 41 | +public class MyService |
| 42 | +{ |
| 43 | + private readonly IEFRepository<MyDbContext, MyEntity> _repository; |
| 44 | + |
| 45 | + public MyService(IEFRepository<MyDbContext, MyEntity> repository) |
| 46 | + { |
| 47 | + _repository = repository; |
| 48 | + } |
| 49 | + |
| 50 | + public async Task<MyEntity> GetEntityByIdAsync(int id) |
| 51 | + { |
| 52 | + return await _repository.FindAsync(id); |
| 53 | + } |
| 54 | + |
| 55 | + public async Task AddEntityAsync(MyEntity entity) |
| 56 | + { |
| 57 | + await _repository.InsertAsync(entity); |
| 58 | + } |
| 59 | + |
| 60 | + public async Task UpdateEntityAsync(MyEntity entity) |
| 61 | + { |
| 62 | + await _repository.UpdateAsync(entity); |
| 63 | + } |
| 64 | + |
| 65 | + public async Task DeleteEntityAsync(int id) |
| 66 | + { |
| 67 | + await _repository.DeleteAsync(id); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### Unit of Work Pattern |
| 73 | + |
| 74 | +The `WeihanLi.EntityFramework` package also provides a unit of work pattern implementation for Entity Framework Core. |
| 75 | + |
| 76 | +```csharp |
| 77 | +public class MyService |
| 78 | +{ |
| 79 | + private readonly IEFUnitOfWork<MyDbContext> _unitOfWork; |
| 80 | + |
| 81 | + public MyService(IEFUnitOfWork<MyDbContext> unitOfWork) |
| 82 | + { |
| 83 | + _unitOfWork = unitOfWork; |
| 84 | + } |
| 85 | + |
| 86 | + public async Task SaveChangesAsync() |
| 87 | + { |
| 88 | + await _unitOfWork.CommitAsync(); |
| 89 | + } |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Audit |
| 94 | + |
| 95 | +The `WeihanLi.EntityFramework` package provides an audit feature to track changes in your entities. |
| 96 | + |
| 97 | +```csharp |
| 98 | +public class MyDbContext : AuditDbContext |
| 99 | +{ |
| 100 | + public MyDbContext(DbContextOptions<MyDbContext> options, IServiceProvider serviceProvider) |
| 101 | + : base(options, serviceProvider) |
| 102 | + { |
| 103 | + } |
| 104 | + |
| 105 | + public DbSet<MyEntity> MyEntities { get; set; } |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +### Soft Delete |
| 110 | + |
| 111 | +The `WeihanLi.EntityFramework` package provides a soft delete feature to mark entities as deleted without actually removing them from the database. |
| 112 | + |
| 113 | +```csharp |
| 114 | +public class MyEntity : ISoftDeleteEntityWithDeleted |
| 115 | +{ |
| 116 | + public int Id { get; set; } |
| 117 | + public string Name { get; set; } |
| 118 | + public bool IsDeleted { get; set; } |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | +### Auto Update |
| 123 | + |
| 124 | +The `WeihanLi.EntityFramework` package provides an auto update feature to automatically update certain properties, such as `CreatedAt`, `UpdatedAt`, `CreatedBy`, and `UpdatedBy`. |
| 125 | + |
| 126 | +```csharp |
| 127 | +public class MyEntity : IEntityWithCreatedUpdatedAt, IEntityWithCreatedUpdatedBy |
| 128 | +{ |
| 129 | + public int Id { get; set; } |
| 130 | + public string Name { get; set; } |
| 131 | + public DateTimeOffset CreatedAt { get; set; } |
| 132 | + public DateTimeOffset UpdatedAt { get; set; } |
| 133 | + public string CreatedBy { get; set; } |
| 134 | + public string UpdatedBy { get; set; } |
| 135 | +} |
| 136 | +``` |
0 commit comments