-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from Ali-YousefiTelori/develop
Check null exception
- Loading branch information
Showing
13 changed files
with
354 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
<Platforms>AnyCPU;x64;x86</Platforms> | ||
<Authors>EasyMicroservices</Authors> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Version>0.0.0.26</Version> | ||
<Version>0.0.0.27</Version> | ||
<Description>asp core servces.</Description> | ||
<Copyright>[email protected]</Copyright> | ||
<PackageTags>core,cores,base,database,services,asp,aspnet</PackageTags> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
<Platforms>AnyCPU;x64;x86</Platforms> | ||
<Authors>EasyMicroservices</Authors> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Version>0.0.0.7</Version> | ||
<Version>0.0.0.8</Version> | ||
<Description>asp core servces.</Description> | ||
<Copyright>[email protected]</Copyright> | ||
<PackageTags>core,cores,base,database,services,asp,aspnet,aspcore,efcore</PackageTags> | ||
|
@@ -22,8 +22,9 @@ | |
<PackageReference Include="EasyMicroservices.Serialization.Newtonsoft.Json" Version="0.0.0.3" /> | ||
<PackageReference Include="EasyMicroservices.Mapper.SerializerMapper" Version="0.0.0.2" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||
<PackageReference Include="EasyMicroservices.WhiteLabelsMicroservice.Clients" Version="0.0.0.6" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\EasyMicroservices.Cores.AspCoreApi\EasyMicroservices.Cores.AspCoreApi.csproj" /> | ||
<ProjectReference Include="..\EasyMicroservices.Cores.Relational.EntityFrameworkCore\EasyMicroservices.Cores.Relational.EntityFrameworkCore.csproj" /> | ||
|
109 changes: 109 additions & 0 deletions
109
src/CSharp/EasyMicroservices.Cores.AspEntityFrameworkCoreApi/Managers/WhiteLabelManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; | ||
using EasyMicroservices.Cores.Database.Managers; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using WhiteLables.GeneratedServices; | ||
|
||
namespace EasyMicroservices.Cores.AspCoreApi.Managers | ||
{ | ||
internal class WhiteLabelManager | ||
{ | ||
internal WhiteLabelManager(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
private readonly IServiceProvider _serviceProvider; | ||
public static HttpClient HttpClient { get; set; } = new HttpClient(); | ||
|
||
string GetDefaultUniqueIdentity(ICollection<WhiteLabelContract> whiteLables, long? parentId) | ||
{ | ||
var found = whiteLables.FirstOrDefault(x => x.ParentId == parentId); | ||
if (found == null) | ||
{ | ||
return ""; | ||
} | ||
return $"{DefaultUniqueIdentityManager.GenerateUniqueIdentity(found.Id)}-{GetDefaultUniqueIdentity(whiteLables, found.Id)}".Trim('-'); | ||
} | ||
|
||
public async Task Initialize(string microserviceName, string whiteLableRoute, params Type[] dbContextTypes) | ||
{ | ||
if (dbContextTypes.IsNullOrEmpty()) | ||
return; | ||
var whiteLabelClient = new WhiteLables.GeneratedServices.WhiteLabelClient(whiteLableRoute, HttpClient); | ||
var whiteLabels = await whiteLabelClient.GetAllAsync().ConfigureAwait(false); | ||
UnitOfWork.DefaultUniqueIdentity = GetDefaultUniqueIdentity(whiteLabels.Result, null); | ||
|
||
var microserviceClient = new WhiteLables.GeneratedServices.MicroserviceClient(whiteLableRoute, HttpClient); | ||
var microservices = await microserviceClient.GetAllAsync().ConfigureAwait(false); | ||
var foundMicroservice = microservices.Result.FirstOrDefault(x => x.Name.Equals(microserviceName, StringComparison.OrdinalIgnoreCase)); | ||
if (foundMicroservice == null) | ||
{ | ||
foundMicroservice = new WhiteLables.GeneratedServices.MicroserviceContract() | ||
{ | ||
InstanceIndex = 1, | ||
Name = microserviceName, | ||
Description = "Automatically added" | ||
}; | ||
var addMicroservice = await microserviceClient.AddAsync(foundMicroservice).ConfigureAwait(false); | ||
foundMicroservice.Id = addMicroservice.Result; | ||
} | ||
UnitOfWork.MicroserviceId = foundMicroservice.Id; | ||
|
||
var uniqueIdentityManager = new UnitOfWork(_serviceProvider).GetUniqueIdentityManager() as DefaultUniqueIdentityManager; | ||
|
||
var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, HttpClient); | ||
var microserviceContextTables = await microserviceContextTableClient.GetAllAsync().ConfigureAwait(false); | ||
|
||
HashSet<string> addedInWhitLabels = new HashSet<string>(); | ||
foreach (var contextTableContract in microserviceContextTables.Result) | ||
{ | ||
uniqueIdentityManager.InitializeTables(contextTableContract.MicroserviceId, contextTableContract.ContextName, contextTableContract.TableName, contextTableContract.ContextTableId); | ||
addedInWhitLabels.Add(uniqueIdentityManager.GetContextTableName(contextTableContract.MicroserviceId, contextTableContract.ContextName, contextTableContract.TableName)); | ||
} | ||
|
||
foreach (var contextType in dbContextTypes) | ||
{ | ||
var contextTableClient = new WhiteLables.GeneratedServices.ContextTableClient(whiteLableRoute, HttpClient); | ||
var contextTables = await contextTableClient.GetAllAsync().ConfigureAwait(false); | ||
using var insctanceOfContext = _serviceProvider.GetService(contextType) as DbContext; | ||
string contextName = uniqueIdentityManager.GetContextName(contextType); | ||
foreach (var entityType in insctanceOfContext.Model.GetEntityTypes()) | ||
{ | ||
string tableName = entityType.ServiceOnlyConstructorBinding.RuntimeType.Name; | ||
var tableFullName = uniqueIdentityManager.GetContextTableName(foundMicroservice.Id, contextType.Name, tableName); | ||
if (!addedInWhitLabels.Contains(tableFullName)) | ||
{ | ||
if (microserviceContextTables.Result.Any(x => x.ContextName == contextName && x.TableName == tableName && x.MicroserviceId == foundMicroservice.Id)) | ||
continue; | ||
var contextTable = contextTables.Result.FirstOrDefault(x => x.ContextName == contextName && x.TableName == tableName); | ||
if (contextTable == null) | ||
{ | ||
contextTable = new WhiteLables.GeneratedServices.ContextTableContract() | ||
{ | ||
ContextName = contextName, | ||
TableName = tableName, | ||
}; | ||
var contextTableResult = await contextTableClient.AddAsync(contextTable).ConfigureAwait(false); | ||
contextTable.Id = contextTableResult.Result; | ||
} | ||
var addedMicroserviceContextTable = await microserviceContextTableClient.AddAsync(new WhiteLables.GeneratedServices.MicroserviceContextTableContract() | ||
{ | ||
ContextName = contextName, | ||
TableName = tableName, | ||
MicroserviceName = microserviceName, | ||
MicroserviceId = foundMicroservice.Id, | ||
ContextTableId = contextTable.Id | ||
}).ConfigureAwait(false); | ||
uniqueIdentityManager.InitializeTables(foundMicroservice.Id, contextName, tableName, contextTable.Id); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
<Platforms>AnyCPU;x64;x86</Platforms> | ||
<Authors>EasyMicroservices</Authors> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Version>0.0.0.28</Version> | ||
<Version>0.0.0.29</Version> | ||
<Description>core of database.</Description> | ||
<Copyright>[email protected]</Copyright> | ||
<PackageTags>core,cores,base,database</PackageTags> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
<Platforms>AnyCPU;x64;x86</Platforms> | ||
<Authors>EasyMicroservices</Authors> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Version>0.0.0.19</Version> | ||
<Version>0.0.0.20</Version> | ||
<Description>ef core of database.</Description> | ||
<Copyright>[email protected]</Copyright> | ||
<PackageTags>core,cores,base,database,ef,efcore</PackageTags> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
<Platforms>AnyCPU;x64;x86</Platforms> | ||
<Authors>EasyMicroservices</Authors> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Version>0.0.0.16</Version> | ||
<Version>0.0.0.17</Version> | ||
<Description>ef core of Relational database.</Description> | ||
<Copyright>[email protected]</Copyright> | ||
<PackageTags>core,cores,base,database,ef,efcore,Relational</PackageTags> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.