Skip to content

Commit bdf32ea

Browse files
2881028810
authored andcommitted
## v1.0.1 # 174 #170 #162
1 parent f8e4e27 commit bdf32ea

File tree

24 files changed

+290
-33
lines changed

24 files changed

+290
-33
lines changed

Extensions/FreeSql.Extensions.BaseEntity/BaseEntity.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using FreeSql;
1+
#if netcore
2+
3+
using FreeSql;
24
using FreeSql.DataAnnotations;
35
using System;
46
using System.Data;
@@ -142,4 +144,6 @@ public virtual TEntity Save()
142144
return this.Repository.InsertOrUpdate(this as TEntity);
143145
}
144146
}
145-
}
147+
}
148+
149+
#endif

Extensions/FreeSql.Extensions.BaseEntity/BaseEntityAsync.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using FreeSql;
1+
#if netcore
2+
3+
using FreeSql;
24
using FreeSql.DataAnnotations;
35
using System;
46
using System.Threading.Tasks;
@@ -124,4 +126,6 @@ public virtual Task<TEntity> SaveAsync()
124126
return this.Repository.InsertOrUpdateAsync(this as TEntity);
125127
}
126128
}
127-
}
129+
}
130+
131+
#endif

Extensions/FreeSql.Extensions.BaseEntity/BaseEntityReadOnly.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using FreeSql.DataAnnotations;
1+
#if netcore
2+
3+
using FreeSql.DataAnnotations;
24
using System;
35
using System.Collections;
46
using System.Collections.Generic;
@@ -199,4 +201,6 @@ public TEntity Attach()
199201
return item;
200202
}
201203
}
202-
}
204+
}
205+
206+
#endif

Extensions/FreeSql.Extensions.BaseEntity/BaseEntityTree.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using FreeSql;
1+
#if netcore
2+
3+
using FreeSql;
24
using FreeSql.DataAnnotations;
35
using System;
46
using System.Collections.Generic;
@@ -161,4 +163,6 @@ async public override Task<TEntity> SaveAsync()
161163
return ret;
162164
}
163165
}
164-
}
166+
}
167+
168+
#endif

Extensions/FreeSql.Extensions.BaseEntity/FreeSql.Extensions.BaseEntity.csproj

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
5-
<Version>1.0.0</Version>
4+
<TargetFrameworks>netcoreapp31;netcoreapp21;net4.0;</TargetFrameworks>
5+
<Version>1.0.1</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>BaseEntity 是一种极简单的 CodeFirst 开发方式,特别对单表或多表CRUD,利用继承节省了每个实体类的重复属性(创建时间、ID等字段),软件删除等功能,进行 crud 操作时不必时常考虑仓储的使用.</Description>
@@ -31,4 +31,12 @@
3131
<ProjectReference Include="..\..\FreeSql.DbContext\FreeSql.DbContext.csproj" />
3232
</ItemGroup>
3333

34+
<ItemGroup>
35+
<Folder Include="Net40\" />
36+
</ItemGroup>
37+
38+
<PropertyGroup Condition="'$(TargetFramework)' == 'netcoreapp31' OR '$(TargetFramework)' == 'netcoreapp21'">
39+
<DefineConstants>netcore</DefineConstants>
40+
</PropertyGroup>
41+
3442
</Project>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#if netcore
2+
#else
3+
4+
using FreeSql;
5+
using FreeSql.DataAnnotations;
6+
using System;
7+
using System.Data;
8+
using System.Diagnostics;
9+
using System.Linq.Expressions;
10+
using System.Threading;
11+
using System.Threading.Tasks;
12+
13+
namespace FreeSql
14+
{
15+
/// <summary>
16+
/// 包括 CreateTime/UpdateTime/IsDeleted、CRUD 方法、以及 ID 主键定义 的实体基类
17+
/// <para></para>
18+
/// 当 TKey 为 int/long 时,Id 主键被设为自增值主键
19+
/// </summary>
20+
/// <typeparam name="TEntity"></typeparam>
21+
/// <typeparam name="TKey"></typeparam>
22+
[Table(DisableSyncStructure = true)]
23+
public abstract class BaseEntity<TEntity, TKey> : BaseEntity<TEntity> where TEntity : class
24+
{
25+
static BaseEntity()
26+
{
27+
var tkeyType = typeof(TKey)?.NullableTypeOrThis();
28+
if (tkeyType == typeof(int) || tkeyType == typeof(long))
29+
Orm.CodeFirst.ConfigEntity(typeof(TEntity),
30+
t => t.Property("Id").IsIdentity(true));
31+
}
32+
33+
/// <summary>
34+
/// 主键
35+
/// </summary>
36+
[Column(Position = 1)]
37+
public virtual TKey Id { get; set; }
38+
39+
/// <summary>
40+
/// 根据主键值获取数据
41+
/// </summary>
42+
/// <param name="id"></param>
43+
/// <returns></returns>
44+
public static TEntity Find(TKey id)
45+
{
46+
var item = Select.WhereDynamic(id).First();
47+
(item as BaseEntity<TEntity>)?.Attach();
48+
return item;
49+
}
50+
}
51+
52+
/// <summary>
53+
/// 包括 CreateTime/UpdateTime/IsDeleted、以及 CRUD 异步和同步方法的实体基类
54+
/// </summary>
55+
/// <typeparam name="TEntity"></typeparam>
56+
[Table(DisableSyncStructure = true)]
57+
public abstract class BaseEntity<TEntity> : BaseEntityReadOnly<TEntity> where TEntity : class
58+
{
59+
bool DeletedPrivate(bool value)
60+
{
61+
if (this.Repository == null)
62+
return Orm.Delete<TEntity>(this as TEntity)
63+
.ExecuteAffrows() == 1;
64+
65+
return this.Repository.Delete(this as TEntity) == 1;
66+
}
67+
/// <summary>
68+
/// 删除数据
69+
/// </summary>
70+
/// <returns></returns>
71+
public virtual bool Delete() => this.DeletedPrivate(true);
72+
73+
/// <summary>
74+
/// 更新数据
75+
/// </summary>
76+
/// <returns></returns>
77+
public virtual bool Update()
78+
{
79+
if (this.Repository == null)
80+
return Orm.Update<TEntity>()
81+
.SetSource(this as TEntity).ExecuteAffrows() == 1;
82+
83+
return this.Repository.Update(this as TEntity) == 1;
84+
}
85+
/// <summary>
86+
/// 插入数据
87+
/// </summary>
88+
public virtual TEntity Insert()
89+
{
90+
if (this.Repository == null)
91+
this.Repository = Orm.GetRepository<TEntity>();
92+
93+
return this.Repository.Insert(this as TEntity);
94+
}
95+
96+
/// <summary>
97+
/// 更新或插入
98+
/// </summary>
99+
/// <returns></returns>
100+
public virtual TEntity Save()
101+
{
102+
if (this.Repository == null)
103+
this.Repository = Orm.GetRepository<TEntity>();
104+
105+
return this.Repository.InsertOrUpdate(this as TEntity);
106+
}
107+
}
108+
}
109+
110+
#endif
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#if netcore
2+
#else
3+
4+
using FreeSql.DataAnnotations;
5+
using System;
6+
using System.Collections;
7+
using System.Collections.Generic;
8+
using System.Data;
9+
using System.Diagnostics;
10+
using System.Linq;
11+
using System.Linq.Expressions;
12+
using System.Threading;
13+
14+
namespace FreeSql
15+
{
16+
/// <summary>
17+
/// 包括 CreateTime/UpdateTime/IsDeleted 的实体基类
18+
/// </summary>
19+
[Table(DisableSyncStructure = true)]
20+
public abstract class BaseEntity
21+
{
22+
static IFreeSql _ormPriv;
23+
/// <summary>
24+
/// 全局 IFreeSql orm 对象
25+
/// </summary>
26+
public static IFreeSql Orm => _ormPriv ?? throw new Exception(@"使用前请初始化 BaseEntity.Initialization(new FreeSqlBuilder()
27+
.UseAutoSyncStructure(true)
28+
.UseConnectionString(DataType.Sqlite, ""data source=test.db;max pool size=5"")
29+
.Build());");
30+
31+
/// <summary>
32+
/// 初始化BaseEntity
33+
/// BaseEntity.Initialization(new FreeSqlBuilder()
34+
/// <para></para>
35+
/// .UseAutoSyncStructure(true)
36+
/// <para></para>
37+
/// .UseConnectionString(DataType.Sqlite, "data source=test.db;max pool size=5")
38+
/// <para></para>
39+
/// .Build());
40+
/// </summary>
41+
/// <param name="fsql">IFreeSql orm 对象</param>
42+
public static void Initialization(IFreeSql fsql)
43+
{
44+
_ormPriv = fsql;
45+
_ormPriv.Aop.CurdBefore += (s, e) => Trace.WriteLine(e.Sql + "\r\n");
46+
}
47+
}
48+
49+
public abstract class BaseEntityReadOnly<TEntity> : BaseEntity where TEntity : class
50+
{
51+
/// <summary>
52+
/// 查询数据
53+
/// </summary>
54+
/// <returns></returns>
55+
public static ISelect<TEntity> Select
56+
{
57+
get
58+
{
59+
var select = Orm.Select<TEntity>().TrackToList(TrackToList); //自动为每个元素 Attach;
60+
return select;
61+
}
62+
}
63+
64+
static void TrackToList(object list)
65+
{
66+
if (list == null) return;
67+
var ls = list as IList<TEntity>;
68+
if (ls == null)
69+
{
70+
var ie = list as IEnumerable;
71+
if (ie == null) return;
72+
var isFirst = true;
73+
foreach (var item in ie)
74+
{
75+
if (item == null) return;
76+
if (isFirst)
77+
{
78+
isFirst = false;
79+
var itemType = item.GetType();
80+
if (itemType == typeof(object)) return;
81+
if (itemType.FullName.StartsWith("Submission#")) itemType = itemType.BaseType;
82+
if (Orm.CodeFirst.GetTableByEntity(itemType)?.Primarys.Any() != true) return;
83+
if (item is BaseEntity<TEntity> == false) return;
84+
}
85+
(item as BaseEntity<TEntity>)?.Attach();
86+
}
87+
return;
88+
}
89+
if (ls.Any() == false) return;
90+
if (ls.FirstOrDefault() is BaseEntity<TEntity> == false) return;
91+
if (Orm.CodeFirst.GetTableByEntity(typeof(TEntity))?.Primarys.Any() != true) return;
92+
foreach (var item in ls)
93+
(item as BaseEntity<TEntity>)?.Attach();
94+
}
95+
96+
/// <summary>
97+
/// 查询条件,Where(a => a.Id > 10),支持导航对象查询,Where(a => a.Author.Email == "[email protected]")
98+
/// </summary>
99+
/// <param name="exp">lambda表达式</param>
100+
/// <returns></returns>
101+
public static ISelect<TEntity> Where(Expression<Func<TEntity, bool>> exp) => Select.Where(exp);
102+
/// <summary>
103+
/// 查询条件,Where(true, a => a.Id > 10),支导航对象查询,Where(true, a => a.Author.Email == "[email protected]")
104+
/// </summary>
105+
/// <param name="condition">true 时生效</param>
106+
/// <param name="exp">lambda表达式</param>
107+
/// <returns></returns>
108+
public static ISelect<TEntity> WhereIf(bool condition, Expression<Func<TEntity, bool>> exp) => Select.WhereIf(condition, exp);
109+
110+
/// <summary>
111+
/// 仓储对象
112+
/// </summary>
113+
protected IBaseRepository<TEntity> Repository { get; set; }
114+
115+
/// <summary>
116+
/// 附加实体,在更新数据时,只更新变化的部分
117+
/// </summary>
118+
public TEntity Attach()
119+
{
120+
if (this.Repository == null)
121+
this.Repository = Orm.GetRepository<TEntity>();
122+
123+
var item = this as TEntity;
124+
this.Repository.Attach(item);
125+
return item;
126+
}
127+
}
128+
}
129+
130+
#endif

Extensions/FreeSql.Extensions.EfCoreFluentApi/FreeSql.Extensions.EfCoreFluentApi.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0</TargetFrameworks>
5-
<Version>1.0.0</Version>
5+
<Version>1.0.1</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql 扩展包实现,使用 FluentApi 方式配置实体模型,使用习惯接近 EFCore,方便过渡.</Description>

Extensions/FreeSql.Extensions.JsonMap/FreeSql.Extensions.JsonMap.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
5-
<Version>1.0.0</Version>
5+
<Version>1.0.1</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql 扩展包,可实现实体类属性为对象时,以JSON形式映射存储.</Description>

Extensions/FreeSql.Extensions.LazyLoading/FreeSql.Extensions.LazyLoading.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;net45;net40</TargetFrameworks>
5-
<Version>1.0.0</Version>
5+
<Version>1.0.1</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql 扩展包,可实现【延时加载】属性.</Description>

0 commit comments

Comments
 (0)