mirror of
https://github.com/dotnetcore/FreeSql.git
synced 2026-02-03 23:10:54 +08:00
1
@@ -31,7 +31,8 @@ static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||
//Be sure to define as singleton mode
|
||||
.Build();
|
||||
|
||||
public class Song {
|
||||
public class Song
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
@@ -52,7 +53,8 @@ var curd = fsql.GetRepository<Song>();
|
||||
Method 2. Inheritance
|
||||
|
||||
```csharp
|
||||
public class SongRepository : BaseRepository<Song, int> {
|
||||
public class SongRepository : BaseRepository<Song, int>
|
||||
{
|
||||
public SongRepository(IFreeSql fsql) : base(fsql, null, null) {}
|
||||
|
||||
//Do something except CURD.
|
||||
@@ -62,19 +64,16 @@ public class SongRepository : BaseRepository<Song, int> {
|
||||
Method 3: Dependency Injection
|
||||
|
||||
```csharp
|
||||
public void ConfigureServices(IServiceCollection services) {
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
|
||||
services.AddSingleton<IFreeSql>(Fsql);
|
||||
services.AddFreeRepository(filter => filter
|
||||
.Apply<ISoftDelete>("SoftDelete", a => a.IsDeleted == false)
|
||||
.Apply<ITenant>("Tenant", a => a.TenantId == 1)
|
||||
,
|
||||
this.GetType().Assembly
|
||||
);
|
||||
services.AddFreeRepository(null, this.GetType().Assembly);
|
||||
}
|
||||
|
||||
//Use in the controller
|
||||
public SongsController(IBaseRepository<Song> repos1) {
|
||||
public SongsController(IBaseRepository<Song> songRepository)
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
@@ -176,6 +175,7 @@ Please view the documentation of [Cascade Saving](Cascade-Saving).
|
||||
| Orm | IFreeSql | ORM currently in use |
|
||||
| DbContextOptions | DbContextOptions | DbContext settings currently in use,modifying the DbContextOptions will not affect other repository. |
|
||||
| DataFilter | IDataFilter\<TEntity\> | Repository Filter, valid in this object |
|
||||
| UpdateDiy | IUpdate\<TEntity\>| Prepare update, same transaction as warehousing |
|
||||
| Select | ISelect\<TEntity\> | Prepare to query data |
|
||||
|
||||
| Method | Return | Parameter | Description |
|
||||
|
||||
@@ -29,7 +29,8 @@ static IFreeSql fsql = new FreeSql.FreeSqlBuilder()
|
||||
.UseAutoSyncStructure(true) //自动迁移实体的结构到数据库
|
||||
.Build(); //请务必定义成 Singleton 单例模式
|
||||
|
||||
public class Song {
|
||||
public class Song
|
||||
{
|
||||
[Column(IsIdentity = true)]
|
||||
public int Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
@@ -50,7 +51,8 @@ var curd = fsql.GetRepository<Song>();
|
||||
方法2、继承实现;
|
||||
|
||||
```csharp
|
||||
public class SongRepository : BaseRepository<Song, int> {
|
||||
public class SongRepository : BaseRepository<Song, int>
|
||||
{
|
||||
public SongRepository(IFreeSql fsql) : base(fsql, null, null) {}
|
||||
|
||||
//在这里增加 CURD 以外的方法
|
||||
@@ -60,19 +62,16 @@ public class SongRepository : BaseRepository<Song, int> {
|
||||
方法3、依赖注入;
|
||||
|
||||
```csharp
|
||||
public void ConfigureServices(IServiceCollection services) {
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
|
||||
services.AddSingleton<IFreeSql>(Fsql);
|
||||
services.AddFreeRepository(filter => filter
|
||||
.Apply<ISoftDelete>("SoftDelete", a => a.IsDeleted == false)
|
||||
.Apply<ITenant>("Tenant", a => a.TenantId == 1)
|
||||
,
|
||||
this.GetType().Assembly
|
||||
);
|
||||
services.AddFreeRepository(null, this.GetType().Assembly);
|
||||
}
|
||||
|
||||
//在控制器使用
|
||||
public SongsController(IBaseRepository<Song> repos1) {
|
||||
public SongsController(IBaseRepository<Song> songRepository)
|
||||
{
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user