Skip to content

Commit

Permalink
refactor(Orm): Masa.Contrib.Data.Contracts.EFCore -> Masa.Contrib.Dat…
Browse files Browse the repository at this point in the history
…a.Contracts (#346)
  • Loading branch information
zhenlei520 authored Nov 25, 2022
1 parent 4b7fe58 commit 0ceb50b
Show file tree
Hide file tree
Showing 60 changed files with 166 additions and 212 deletions.
4 changes: 2 additions & 2 deletions Masa.Framework.sln
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Orm", "Orm", "{343A61CC-45B
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "UoW", "UoW", "{E8B3FB42-C450-4B12-9C2C-7B9A02B616BB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.Contracts.EFCore", "src\Contrib\Data\Contracts\Masa.Contrib.Data.Contracts.EFCore\Masa.Contrib.Data.Contracts.EFCore.csproj", "{8353A1CE-B6F0-42A2-B7F1-BC87707719AD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.Contracts", "src\Contrib\Data\Contracts\Masa.Contrib.Data.Contracts\Masa.Contrib.Data.Contracts.csproj", "{8353A1CE-B6F0-42A2-B7F1-BC87707719AD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{83E5A183-56D6-4CAC-95A6-FB280AA7AE3D}"
EndProject
Expand Down Expand Up @@ -477,7 +477,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Dispatcher.Eve
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.EFCore.PostgreSql", "src\Contrib\Data\Orm\EFCore\Masa.Contrib.Data.EFCore.PostgreSql\Masa.Contrib.Data.EFCore.PostgreSql.csproj", "{6616A9E2-66F3-4AEB-BE0D-77346B3F3B06}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.Contracts.EFCore.Tests", "src\Contrib\Data\Contracts\Tests\Masa.Contrib.Data.Contracts.EFCore.Tests\Masa.Contrib.Data.Contracts.EFCore.Tests.csproj", "{979FBAA7-EC54-4183-B2A7-3C5BE5510F16}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.Contracts.Tests", "src\Contrib\Data\Contracts\Tests\Masa.Contrib.Data.Contracts.Tests\Masa.Contrib.Data.Contracts.Tests.csproj", "{979FBAA7-EC54-4183-B2A7-3C5BE5510F16}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Masa.Contrib.Data.UoW.EFCore", "src\Contrib\Data\UoW\Masa.Contrib.Data.UoW.EFCore\Masa.Contrib.Data.UoW.EFCore.csproj", "{44DB6ED1-71DC-487B-8DD7-3F872CB23254}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.BuildingBlocks.Data;

public interface IMasaDbContextBuilder
{
public bool EnableSoftDelete { get; set; }

public IServiceCollection Services { get; }
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.DataFiltering;
namespace Masa.Contrib.Data.Contracts.DataFiltering;

public class DataFilter : IDataFilter
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Internal;
namespace Masa.Contrib.Data.Contracts.Internal;

internal class DataFilterState
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Internal;
namespace Masa.Contrib.Data.Contracts.Internal;

internal sealed class DisposeAction : IDisposable
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Internal;
namespace Masa.Contrib.Data.Contracts.Internal;

[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class NullDisposable : IDisposable
{
public static NullDisposable Instance { get; } = new();

/// <summary>
/// no need to release resources
/// </summary>
public void Dispose()
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\..\BuildingBlocks\Data\Masa.BuildingBlocks.Data\Masa.BuildingBlocks.Data.csproj" />
<ProjectReference Include="..\..\..\..\Utils\Caching\Masa.Utils.Caching.Memory\Masa.Utils.Caching.Memory.csproj" />
<ProjectReference Include="..\..\Orm\EFCore\Masa.Contrib.Data.EFCore\Masa.Contrib.Data.EFCore.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

// ReSharper disable once CheckNamespace

namespace Microsoft.EntityFrameworkCore;

public static class MasaDbContextBuilderExtensions
{
public static IMasaDbContextBuilder UseFilter(
this IMasaDbContextBuilder masaDbContextBuilder,
Action<FilterOptions>? options = null)
=> masaDbContextBuilder.UseFilterCore(options);

private static IMasaDbContextBuilder UseFilterCore(
this IMasaDbContextBuilder masaDbContextBuilder,
Action<FilterOptions>? options = null)
{
var filterOptions = new FilterOptions();
options?.Invoke(filterOptions);

masaDbContextBuilder.Services.TryAddScoped(typeof(DataFilter<>));
masaDbContextBuilder.Services.TryAddScoped<IDataFilter, DataFilter>();

masaDbContextBuilder.EnableSoftDelete = filterOptions.EnableSoftDelete;

return masaDbContextBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Options;
namespace Masa.Contrib.Data.Contracts.Options;

public class FilterOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[](README.zh-CN.md) | EN

## Masa.Contrib.Data.Contracts.EFCore
## Masa.Contrib.Data.Contracts

Example:

``` powershelll
Install-Package Masa.Contrib.Data.EFCore.Sqlite
Install-Package Masa.Contrib.Data.Contracts.EFCore //Use the data filtering and soft delete capabilities provided by the protocol
Install-Package Masa.Contrib.Data.Contracts //Use the data filtering and soft delete capabilities provided by the protocol
```

### Usage 1
Expand All @@ -26,8 +26,8 @@ Install-Package Masa.Contrib.Data.Contracts.EFCore //Use the data filtering and
``` C#
builder.Services.AddMasaDbContext<CustomDbContext>(optionsBuilder =>
{
optionsBuilder.UseFilter(); //Enable filtering
optionsBuilder.UseSqlite(); //Using Sqlite database
optionsBuilder.UseFilter(); //Enable filtering
});
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
中 | [EN](README.md)

## Masa.Contrib.Data.Contracts.EFCore
## Masa.Contrib.Data.Contracts

提供的数据过滤(用于禁用租户、环境或软删除)、软删除能力

用例:

``` powershelll
Install-Package Masa.Contrib.Data.EFCore.Sqlite //以Sqlite数据库为例
Install-Package Masa.Contrib.Data.Contracts.EFCore //使用规约提供的数据过滤、软删除能力
Install-Package Masa.Contrib.Data.Contracts //使用规约提供的数据过滤、软删除能力
```

1. 配置appsettings.json
Expand All @@ -26,8 +26,8 @@ Install-Package Masa.Contrib.Data.Contracts.EFCore //使用规约提供的数据
``` C#
builder.Services.AddMasaDbContext<CustomDbContext>(optionsBuilder =>
{
optionsBuilder.UseFilter();//启用过滤
optionsBuilder.UseSqlite();//使用Sqlite数据库
optionsBuilder.UseFilter();//启用过滤
});
```

Expand Down
12 changes: 12 additions & 0 deletions src/Contrib/Data/Contracts/Masa.Contrib.Data.Contracts/_Imports.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.Data;
global using Masa.Contrib.Data.Contracts.DataFiltering;
global using Masa.Contrib.Data.Contracts.Internal;
global using Masa.Contrib.Data.Contracts.Options;
global using Masa.Utils.Caching.Memory;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.DependencyInjection.Extensions;
global using System.Linq.Expressions;
global using System.Reflection;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests;
namespace Masa.Contrib.Data.Contracts.Tests;

public class CustomDbContext : MasaDbContext<CustomDbContext>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

using System.Reflection;

namespace Masa.Contrib.Data.Contracts.EFCore.Tests;
namespace Masa.Contrib.Data.Contracts.Tests;

[TestClass]
public class DataFilterTest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.EntityConfigurations;
namespace Masa.Contrib.Data.Contracts.Tests.EntityConfigurations;

public class AddressEntityTypeConfiguration : IEntityTypeConfiguration<Address>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.EntityConfigurations;
namespace Masa.Contrib.Data.Contracts.Tests.EntityConfigurations;

public class StudentEntityTypeConfiguration : IEntityTypeConfiguration<Student>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\Orm\EFCore\Masa.Contrib.Data.EFCore.Sqlite\Masa.Contrib.Data.EFCore.Sqlite.csproj" />
<ProjectReference Include="..\..\Masa.Contrib.Data.Contracts.EFCore\Masa.Contrib.Data.Contracts.EFCore.csproj" />
<ProjectReference Include="..\..\Masa.Contrib.Data.Contracts\Masa.Contrib.Data.Contracts.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
namespace Masa.Contrib.Data.Contracts.Tests.Models;

public class Address : ISoftDelete
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
namespace Masa.Contrib.Data.Contracts.Tests.Models;

public class Hobby
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
namespace Masa.Contrib.Data.Contracts.Tests.Models;

public class LogItem
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
namespace Masa.Contrib.Data.Contracts.Tests.Models;

public class Student : IAuditEntity<int>, ISoftDelete
{
Expand Down
Loading

0 comments on commit 0ceb50b

Please sign in to comment.