Skip to content

Commit ed92415

Browse files
authored
Merge pull request #11 from rpaschoal/release/1.2.0
Merge from 1.2.0
2 parents 29b34ae + 07b74a7 commit ed92415

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+868
-465
lines changed

DynamicRepository.EF/App.config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<configSections>
4+
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
5+
</configSections>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="LinqKit" publicKeyToken="bc217f8844052a91" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-1.1.16.0" newVersion="1.1.16.0" />
11+
</dependentAssembly>
12+
</assemblyBinding>
13+
</runtime>
14+
</configuration>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{37056BF7-F7E6-4A5E-ACC7-351B296148A5}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>DynamicRepository.EF</RootNamespace>
11+
<AssemblyName>DynamicRepository.EF</AssemblyName>
12+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="DynamicRepository, Version=1.2.0.0, Culture=neutral, processorArchitecture=MSIL">
35+
<HintPath>..\packages\DynamicRepository.1.2.0\lib\netstandard2.0\DynamicRepository.dll</HintPath>
36+
</Reference>
37+
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
38+
<HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.dll</HintPath>
39+
</Reference>
40+
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
41+
<HintPath>..\packages\EntityFramework.6.2.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
42+
</Reference>
43+
<Reference Include="LinqKit, Version=1.1.16.0, Culture=neutral, PublicKeyToken=bc217f8844052a91, processorArchitecture=MSIL">
44+
<HintPath>..\packages\LinqKit.1.1.16\lib\net45\LinqKit.dll</HintPath>
45+
<Private>True</Private>
46+
</Reference>
47+
<Reference Include="LinqKit.Core, Version=1.1.16.0, Culture=neutral, PublicKeyToken=bc217f8844052a91, processorArchitecture=MSIL">
48+
<HintPath>..\packages\LinqKit.Core.1.1.16\lib\net45\LinqKit.Core.dll</HintPath>
49+
</Reference>
50+
<Reference Include="System" />
51+
<Reference Include="System.ComponentModel.DataAnnotations" />
52+
<Reference Include="System.Core" />
53+
<Reference Include="System.Linq.Dynamic.Core, Version=1.0.16.0, Culture=neutral, PublicKeyToken=0f07ec44de6ac832, processorArchitecture=MSIL">
54+
<HintPath>..\packages\System.Linq.Dynamic.Core.1.0.16\lib\net46\System.Linq.Dynamic.Core.dll</HintPath>
55+
</Reference>
56+
<Reference Include="System.Xml.Linq" />
57+
<Reference Include="System.Data.DataSetExtensions" />
58+
<Reference Include="Microsoft.CSharp" />
59+
<Reference Include="System.Data" />
60+
<Reference Include="System.Net.Http" />
61+
<Reference Include="System.Xml" />
62+
</ItemGroup>
63+
<ItemGroup>
64+
<Compile Include="Properties\AssemblyInfo.cs" />
65+
<Compile Include="Repository.cs" />
66+
</ItemGroup>
67+
<ItemGroup>
68+
<None Include="App.config" />
69+
<None Include="packages.config" />
70+
</ItemGroup>
71+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
72+
</Project>

src/DynamicRepository.EF/Properties/AssemblyInfo.cs renamed to DynamicRepository.EF/Properties/AssemblyInfo.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,32 @@
55
// General Information about an assembly is controlled through the following
66
// set of attributes. Change these attribute values to modify the information
77
// associated with an assembly.
8+
[assembly: AssemblyTitle("DynamicRepository.EF")]
9+
[assembly: AssemblyDescription("https://github.com/rpaschoal/DynamicRepository")]
810
[assembly: AssemblyConfiguration("")]
911
[assembly: AssemblyCompany("")]
1012
[assembly: AssemblyProduct("DynamicRepository.EF")]
13+
[assembly: AssemblyCopyright("Copyright © 2019")]
1114
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
1216

1317
// Setting ComVisible to false makes the types in this assembly not visible
1418
// to COM components. If you need to access a type in this assembly from
1519
// COM, set the ComVisible attribute to true on that type.
1620
[assembly: ComVisible(false)]
1721

1822
// The following GUID is for the ID of the typelib if this project is exposed to COM
19-
[assembly: Guid("51f10810-bf5b-4405-a47b-8cf26c459e59")]
23+
[assembly: Guid("37056bf7-f7e6-4a5e-acc7-351b296148a5")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.2.0")]
36+
[assembly: AssemblyFileVersion("1.2.0")]

src/DynamicRepository.EF/Repository.cs renamed to DynamicRepository.EF/Repository.cs

Lines changed: 92 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using DynamicRepository.Extensions;
1111
using System.Data.Entity;
1212
using System.Linq.Dynamic.Core;
13+
using System.Threading.Tasks;
1314

1415
namespace DynamicRepository.EF
1516
{
@@ -32,6 +33,20 @@ namespace DynamicRepository.EF
3233
/// </summary>
3334
protected internal DbSet<Entity> DbSet;
3435

36+
/// <summary>
37+
/// Global filter instance set by <see cref="HasGlobalFilter(Expression{Func{Entity, bool}})" />
38+
/// </summary>
39+
private Expression<Func<Entity, bool>> GlobalFilter { get; set; }
40+
41+
/// <summary>
42+
/// Adds a global filter expression to all operations which query for data.
43+
/// </summary>
44+
/// <remarks>This method was inspired by "HasQueryFilter" found on EF Core.</remarks>
45+
public void HasGlobalFilter(Expression<Func<Entity, bool>> filter)
46+
{
47+
GlobalFilter = filter;
48+
}
49+
3550
/// <summary>
3651
/// Default constructor of main repository.
3752
/// Required dependencies are injected.
@@ -49,30 +64,47 @@ public Repository(DbContext context)
4964
}
5065

5166
/// <summary>
52-
/// Returns an instance of non-filtered IQueryable of all items in a DBSet.
67+
/// Gets an entity instance based on its <see cref="Key"/>.
5368
/// </summary>
54-
/// <returns>IQueryable instance of type <see cref="Entity"/></returns>
55-
internal virtual IQueryable<Entity> List()
69+
/// <param name="key">The desired entity key value.</param>
70+
/// <returns>Persisted entity if found, otherwise NULL.</returns>
71+
public virtual Entity Get(Key key)
5672
{
57-
return DbSet;
73+
Entity queriedEntity;
74+
75+
if (key is Array)
76+
{
77+
// This is to handle entity framework find by composite key
78+
queriedEntity = DbSet.Find((key as IEnumerable).Cast<object>().ToArray());
79+
}
80+
else
81+
{
82+
queriedEntity = DbSet.Find(key);
83+
}
84+
85+
return GlobalFilter != null ? new[] { queriedEntity }.AsQueryable().FirstOrDefault(GlobalFilter) : queriedEntity;
5886
}
5987

6088
/// <summary>
6189
/// Gets an entity instance based on its <see cref="Key"/>.
6290
/// </summary>
6391
/// <param name="key">The desired entity key value.</param>
6492
/// <returns>Persisted entity if found, otherwise NULL.</returns>
65-
public virtual Entity Get(Key key)
93+
public virtual async Task<Entity> GetAsync(Key key)
6694
{
95+
Entity queriedEntity;
96+
6797
if (key is Array)
6898
{
6999
// This is to handle entity framework find by composite key
70-
return DbSet.Find((key as IEnumerable).Cast<object>().ToArray());
100+
queriedEntity = await DbSet.FindAsync((key as IEnumerable).Cast<object>().ToArray());
71101
}
72102
else
73103
{
74-
return DbSet.Find(key);
104+
queriedEntity = await DbSet.FindAsync(key);
75105
}
106+
107+
return GlobalFilter != null ? await new[] { queriedEntity }.AsQueryable().FirstOrDefaultAsync(GlobalFilter) : queriedEntity;
76108
}
77109

78110
/// <summary>
@@ -84,6 +116,15 @@ public virtual void Insert(Entity entity)
84116
DbSet.Add(entity);
85117
}
86118

119+
/// <summary>
120+
/// Persists a new entity model.
121+
/// </summary>
122+
/// <param name="entity">The new <see cref="Entity"/> instance to be persisted.</param>
123+
public virtual Task InsertAsync(Entity entity)
124+
{
125+
return Task.Run(() => DbSet.Add(entity));
126+
}
127+
87128
/// <summary>
88129
/// Updates an existing persisted entity.
89130
/// </summary>
@@ -93,13 +134,31 @@ public virtual void Update(Entity entityToUpdate)
93134
Context.Entry(entityToUpdate).State = EntityState.Modified;
94135
}
95136

137+
/// <summary>
138+
/// Updates an existing persisted entity.
139+
/// </summary>
140+
/// <param name="entityToUpdate">The <see cref="Entity"/> instance to be updated.</param>
141+
public virtual Task UpdateAsync(Entity entityToUpdate)
142+
{
143+
return Task.Run(() => Context.Entry(entityToUpdate).State = EntityState.Modified);
144+
}
145+
96146
/// <summary>
97147
/// Deletes an existing entity.
98148
/// </summary>
99149
/// <param name="id">The primary key of the <see cref="Entity"/> to be deleted.</param>
100150
public virtual void Delete(Key id)
101151
{
102-
Delete(this.Get(id));
152+
Delete(Get(id));
153+
}
154+
155+
/// <summary>
156+
/// Deletes an existing entity.
157+
/// </summary>
158+
/// <param name="id">The primary key of the <see cref="Entity"/> to be deleted.</param>
159+
public virtual Task DeleteAsync(Key id)
160+
{
161+
return Task.Run(async () => DeleteAsync(await GetAsync(id)));
103162
}
104163

105164
/// <summary>
@@ -114,12 +173,34 @@ public void Delete(Entity entityToDelete)
114173
}
115174
}
116175

176+
/// <summary>
177+
/// Deletes an existing entity.
178+
/// </summary>
179+
/// <param name="entityToDelete">The <see cref="Entity"/> instance to be deleted.</param>
180+
public Task DeleteAsync(Entity entityToDelete)
181+
{
182+
if (entityToDelete != null)
183+
{
184+
return Task.Run(() => DbSet.Remove(entityToDelete));
185+
}
186+
187+
return Task.CompletedTask;
188+
}
189+
117190
/// <summary>
118191
/// Returns all entries of this entity.
119192
/// </summary>
120193
public IEnumerable<Entity> ListAll()
121194
{
122-
return this.DbSet.ToList();
195+
return GetQueryable();
196+
}
197+
198+
/// <summary>
199+
/// Gets a queryable instance of the current data set.
200+
/// </summary>
201+
public IQueryable<Entity> GetQueryable()
202+
{
203+
return GlobalFilter != null ? DbSet.AsQueryable().Where(GlobalFilter) : DbSet.AsQueryable();
123204
}
124205

125206
/// <summary>
@@ -137,7 +218,7 @@ public IEnumerable<Entity> List(
137218
Func<IQueryable<Entity>, IOrderedQueryable<Entity>> orderBy = null,
138219
params string[] includeProperties)
139220
{
140-
IQueryable<Entity> query = DbSet;
221+
IQueryable<Entity> query = GetQueryable();
141222

142223
if (filter != null)
143224
{
@@ -159,25 +240,14 @@ public IEnumerable<Entity> List(
159240
}
160241
}
161242

162-
/// <summary>
163-
/// Returns <see cref="IQueryable"/> for consumers to shape queries as they need to.
164-
/// </summary>
165-
/// <returns>
166-
/// Plain DbSet as Queryable.
167-
/// </returns>
168-
protected internal IQueryable<Entity> AsQueryable()
169-
{
170-
return DbSet.AsQueryable();
171-
}
172-
173243
/// <summary>
174244
/// Returns a collection of data results that can be paged.
175245
/// </summary>
176246
/// <param name="settings">Settings for the search.</param>
177247
/// <returns>Filled PagedData instance.</returns>
178248
public IPagedDataResult<Entity> GetPagedData(PagedDataSettings settings)
179249
{
180-
return _dataSourcePager.GetPagedData((IQueryable<Entity>)this.List(), settings, this.AddPreConditionsPagedDataFilter(settings), this.AddExtraPagedDataFilter(settings));
250+
return _dataSourcePager.GetPagedData(GetQueryable(), settings, AddPreConditionsPagedDataFilter(settings), AddExtraPagedDataFilter(settings));
181251
}
182252

183253
/// <summary>

DynamicRepository.EF/packages.config

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="DynamicRepository" version="1.2.0" targetFramework="net472" />
4+
<package id="EntityFramework" version="6.2.0" targetFramework="net472" />
5+
<package id="LinqKit" version="1.1.16" targetFramework="net472" />
6+
<package id="LinqKit.Core" version="1.1.16" targetFramework="net472" />
7+
<package id="System.Linq.Dynamic.Core" version="1.0.16" targetFramework="net472" />
8+
</packages>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<Version>1.2.0</Version>
6+
<Authors>Rafael Carvalho</Authors>
7+
<PackageProjectUrl>https://github.com/rpaschoal/DynamicRepository</PackageProjectUrl>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="DynamicRepository" Version="1.2.0" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.2.4" />
13+
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.17" />
14+
</ItemGroup>
15+
16+
</Project>

0 commit comments

Comments
 (0)