diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/Database/Contexts/ContentContext.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/Database/Contexts/ContentContext.cs index 5350d67..2a31c4a 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/Database/Contexts/ContentContext.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/Database/Contexts/ContentContext.cs @@ -1,5 +1,6 @@ using EasyMicroservices.ContentsMicroservice.Database.Entities; using EasyMicroservices.Cores.Relational.EntityFrameworkCore; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; using Microsoft.EntityFrameworkCore; using System; @@ -7,24 +8,14 @@ namespace EasyMicroservices.ContentsMicroservice.Database.Contexts { public class ContentContext : RelationalCoreContext { - IDatabaseBuilder _builder; - public ContentContext(IDatabaseBuilder builder) + public ContentContext(IEntityFrameworkCoreDatabaseBuilder builder) : base(builder) { - _builder = builder; } public DbSet Categories { get; set; } public DbSet Contents { get; set; } public DbSet Languages { get; set; } - - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (_builder != null) - _builder.OnConfiguring(optionsBuilder); - base.OnConfiguring(optionsBuilder); - } - protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/Database/IDatabaseBuilder.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/Database/IDatabaseBuilder.cs deleted file mode 100644 index 62846b0..0000000 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/Database/IDatabaseBuilder.cs +++ /dev/null @@ -1,9 +0,0 @@ -using Microsoft.EntityFrameworkCore; - -namespace EasyMicroservices.ContentsMicroservice.Database -{ - public interface IDatabaseBuilder - { - void OnConfiguring(DbContextOptionsBuilder optionsBuilder); - } -} diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/EasyMicroservices.ContentsMicroservice.Database.csproj b/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/EasyMicroservices.ContentsMicroservice.Database.csproj index a8c211a..1ec3971 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/EasyMicroservices.ContentsMicroservice.Database.csproj +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Database/EasyMicroservices.ContentsMicroservice.Database.csproj @@ -12,8 +12,8 @@ - - + + diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Domain/EasyMicroservices.ContentsMicroservice.Domain.csproj b/src/CSharp/EasyMicroservices.ContentsMicroservice.Domain/EasyMicroservices.ContentsMicroservice.Domain.csproj index f3ed16b..3175ff8 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Domain/EasyMicroservices.ContentsMicroservice.Domain.csproj +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Domain/EasyMicroservices.ContentsMicroservice.Domain.csproj @@ -12,7 +12,7 @@ - + diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/AppUnitOfWork.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/AppUnitOfWork.cs deleted file mode 100644 index a053520..0000000 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/AppUnitOfWork.cs +++ /dev/null @@ -1,25 +0,0 @@ -using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; -using System; -using System.Threading.Tasks; - -namespace EasyMicroservices.ContentsMicroservice -{ - public class AppUnitOfWork : UnitOfWork - { - public AppUnitOfWork(IServiceProvider service) : base(service) - { - } - - - public override void Dispose() - { - base.Dispose(); - } - - - public override ValueTask DisposeAsync() - { - return base.DisposeAsync(); - } - } -} diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/EasyMicroservices.ContentsMicroservice.Logics.csproj b/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/EasyMicroservices.ContentsMicroservice.Logics.csproj index e7c5e29..7f6f97c 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/EasyMicroservices.ContentsMicroservice.Logics.csproj +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/EasyMicroservices.ContentsMicroservice.Logics.csproj @@ -6,7 +6,6 @@ - @@ -14,6 +13,8 @@ + + diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/Helpers/ApplicationManager.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/Helpers/ApplicationManager.cs deleted file mode 100644 index 127f541..0000000 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/Helpers/ApplicationManager.cs +++ /dev/null @@ -1,16 +0,0 @@ -using EasyMicroservices.ContentsMicroservice.Interfaces; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EasyMicroservices.ContentsMicroservice.Helpers -{ - public class ApplicationManager - { - public static ApplicationManager Instance { get; private set; } = new ApplicationManager(); - public IDependencyManager DependencyManager { get; set; } - - } -} diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/Interfaces/IDependencyManager.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/Interfaces/IDependencyManager.cs deleted file mode 100644 index b091767..0000000 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Logics/Interfaces/IDependencyManager.cs +++ /dev/null @@ -1,16 +0,0 @@ -using EasyMicroservices.Configuration.Interfaces; -using EasyMicroservices.Cores.Database.Interfaces; -using EasyMicroservices.Cores.Database.Managers; -using EasyMicroservices.Database.Interfaces; -using EasyMicroservices.Mapper.Interfaces; - -namespace EasyMicroservices.ContentsMicroservice.Interfaces -{ - public interface IDependencyManager - { - IDatabase GetDatabase(); - IMapperProvider GetMapper(); - IConfigProvider GetConfigProvider(); - IUniqueIdentityManager GetUniqueIdentityManager(); - } -} diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/DatabaseBuilder.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/DatabaseBuilder.cs index c2f8088..28d0aee 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/DatabaseBuilder.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/DatabaseBuilder.cs @@ -1,4 +1,5 @@ using EasyMicroservices.ContentsMicroservice.Database; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; @@ -9,16 +10,18 @@ namespace EasyMicroservices.ContentsMicroservice { - public class DatabaseBuilder : IDatabaseBuilder + public class DatabaseBuilder : IEntityFrameworkCoreDatabaseBuilder { - readonly IConfiguration config = new ConfigurationBuilder() - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .Build(); + IConfiguration _configuration; + public DatabaseBuilder(IConfiguration configuration) + { + _configuration = configuration; + } public void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseInMemoryDatabase("ContentDatabase"); - //optionsBuilder.UseSqlServer(config.GetConnectionString("local")); + //optionsBuilder.UseSqlServer(_configuration.GetConnectionString("local")); } } } diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/DependencyManager.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/DependencyManager.cs deleted file mode 100644 index 0acfe0b..0000000 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/DependencyManager.cs +++ /dev/null @@ -1,66 +0,0 @@ -using EasyMicroservices.Configuration.Interfaces; -using EasyMicroservices.ContentsMicroservice.Database.Contexts; -using EasyMicroservices.ContentsMicroservice.Interfaces; -using EasyMicroservices.Cores.Database.Interfaces; -using EasyMicroservices.Cores.Database.Logics; -using EasyMicroservices.Cores.Database.Managers; -using EasyMicroservices.Cores.Interfaces; -using EasyMicroservices.Database.EntityFrameworkCore.Providers; -using EasyMicroservices.Database.Interfaces; -using EasyMicroservices.Mapper.CompileTimeMapper.Interfaces; -using EasyMicroservices.Mapper.CompileTimeMapper.Providers; -using EasyMicroservices.Mapper.Interfaces; -using System; -using System.Linq; - -namespace EasyMicroservices.ContentsMicroservice -{ - public class DependencyManager : IDependencyManager - { - public virtual IConfigProvider GetConfigProvider() - { - throw new NotImplementedException(); - } - - public virtual IContractLogic GetContractLogic() - where TEntity : class, IIdSchema - where TResponseContract : class - { - return new LongIdMappedDatabaseLogicBase(GetDatabase().GetReadableOf(), GetDatabase().GetWritableOf(), GetMapper(), GetUniqueIdentityManager()); - } - - public virtual IDatabase GetDatabase() - { - return new EntityFrameworkCoreDatabaseProvider(new ContentContext(new DatabaseBuilder())); - } - - public static string DefaultUniqueIdentity { get; set; } - public static long MicroserviceId { get; set; } - public static IUniqueIdentityManager UniqueIdentityManager { get; set; } - public virtual IUniqueIdentityManager GetUniqueIdentityManager() - { - if (UniqueIdentityManager == null) - UniqueIdentityManager = new DefaultUniqueIdentityManager(DefaultUniqueIdentity, MicroserviceId); - return UniqueIdentityManager; - } - - public virtual IMapperProvider GetMapper() - { - var mapper = new CompileTimeMapperProvider(new EasyMicroservices.Mapper.SerializerMapper.Providers.SerializerMapperProvider(new EasyMicroservices.Serialization.Newtonsoft.Json.Providers.NewtonsoftJsonProvider(new Newtonsoft.Json.JsonSerializerSettings() - { - ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore - }))); - - foreach (var type in typeof(IDependencyManager).Assembly.GetTypes()) - { - if (typeof(IMapper).IsAssignableFrom(type)) - { - var instance = Activator.CreateInstance(type, mapper); - var returnTypes = type.GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Where(x => x.ReturnType != typeof(object) && x.Name == "Map").Select(x => x.ReturnType).ToArray(); - mapper.AddMapper(returnTypes[0], returnTypes[1], (IMapper)instance); - } - } - return mapper; - } - } -} \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/EasyMicroservices.ContentsMicroservice.StartUp.csproj b/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/EasyMicroservices.ContentsMicroservice.StartUp.csproj index 00668bf..ba845cc 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/EasyMicroservices.ContentsMicroservice.StartUp.csproj +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/EasyMicroservices.ContentsMicroservice.StartUp.csproj @@ -7,7 +7,7 @@ - + diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/StartUp.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/StartUp.cs deleted file mode 100644 index 15abb7e..0000000 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/StartUp.cs +++ /dev/null @@ -1,19 +0,0 @@ -using EasyMicroservices.ContentsMicroservice.Helpers; -using EasyMicroservices.ContentsMicroservice.Interfaces; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace EasyMicroservices.ContentsMicroservice -{ - public class StartUp - { - public Task Run(IDependencyManager dependencyManager) - { - ApplicationManager.Instance.DependencyManager = dependencyManager; - return Task.CompletedTask; - } - } -} diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/WhiteLabelManager.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/WhiteLabelManager.cs deleted file mode 100644 index f674def..0000000 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.StartUp/WhiteLabelManager.cs +++ /dev/null @@ -1,116 +0,0 @@ -using EasyMicroservices.Cores.Database.Managers; -using EasyMicroservices.ContentsMicroservice.Interfaces; -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; -using EasyMicroservices.ContentsMicroservice; - -namespace EasyMicroservices.ContentsMicroservice -{ - public class WhiteLabelManager - { - public WhiteLabelManager(IServiceProvider serviceProvider, IDependencyManager dependencyManager) - { - _serviceProvider = serviceProvider; - _dependencyManager = dependencyManager; - } - - private readonly IServiceProvider _serviceProvider; - private readonly IDependencyManager _dependencyManager; - public static HttpClient HttpClient { get; set; } = new HttpClient(); - - string GetDefaultUniqueIdentity(ICollection 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(); - DependencyManager.DefaultUniqueIdentity = GetDefaultUniqueIdentity(whiteLabels.Result, null); - - var microserviceClient = new WhiteLables.GeneratedServices.MicroserviceClient(whiteLableRoute, HttpClient); - var microservices = await microserviceClient.GetAllAsync(); - 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); - foundMicroservice.Id = addMicroservice.Result; - } - DependencyManager.MicroserviceId = foundMicroservice.Id; - - var uniqueIdentityManager = _dependencyManager.GetUniqueIdentityManager() as DefaultUniqueIdentityManager; - - - AppUnitOfWork.MicroserviceId = DependencyManager.MicroserviceId; - AppUnitOfWork.DefaultUniqueIdentity = DependencyManager.DefaultUniqueIdentity; - - var microserviceContextTableClient = new WhiteLables.GeneratedServices.MicroserviceContextTableClient(whiteLableRoute, HttpClient); - var microserviceContextTables = await microserviceContextTableClient.GetAllAsync(); - - HashSet addedInWhitLabels = new HashSet(); - 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(); - 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); - 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 - }); - uniqueIdentityManager.InitializeTables(foundMicroservice.Id, contextName, tableName, contextTable.Id); - } - } - } - } - } -} diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/CodeReviewerCheckTests.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/CodeReviewerCheckTests.cs index b69a057..88424af 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/CodeReviewerCheckTests.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/CodeReviewerCheckTests.cs @@ -1,12 +1,8 @@ using CodeReviewer.Engine; using EasyMicroservices.ContentsMicroservice.Contracts.Common; using EasyMicroservices.ContentsMicroservice.Database.Entities; -using EasyMicroservices.ContentsMicroservice.Helpers; using EasyMicroservices.ContentsMicroservice.WebApi.Controllers; using EasyMicroservices.Tests; -using System; -using System.IO; -using System.Linq; namespace EasyMicroservices.ContentsMicroservice.Tests { @@ -16,8 +12,8 @@ static CodeReviewerCheckTests() { //types to check (this will check all of types in assembly so no need to add all of types of assembly) AssemblyManager.AddAssemblyToReview( - typeof(ApplicationManager), - typeof(StartUp), + typeof(DatabaseBuilder), + //typeof(CompileTimeClassesMappers), typeof(CategoryEntity), typeof(CategoryContract), typeof(CategoryController)); diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/ContentMemoryLeakTest.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/ContentMemoryLeakTest.cs new file mode 100644 index 0000000..b219e80 --- /dev/null +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/ContentMemoryLeakTest.cs @@ -0,0 +1,89 @@ +using EasyMicroservices.ContentsMicroservice.Contracts.Common; +using EasyMicroservices.ContentsMicroservice.Contracts.Requests; +using EasyMicroservices.ContentsMicroservice.Database.Contexts; +using EasyMicroservices.ContentsMicroservice.Database.Entities; +using EasyMicroservices.ContentsMicroservice.WebApi.Controllers; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; +using Microsoft.AspNetCore.Mvc.ApplicationParts; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System; +using System.Threading.Tasks; + +namespace EasyMicroservices.ContentsMicroservice.Tests +{ + public class ContentMemoryLeakTest + { + static bool IsStarted = false; + + static async Task StartServer() + { + if (IsStarted) + return; + IsStarted = true; + _ = Task.Run(async () => + { + await EasyMicroservices.ContentsMicroservice.WebApi.Program.Run(null, (s) => + { + s.AddControllers().PartManager.ApplicationParts.Add(new AssemblyPart(typeof(CategoryController).Assembly)); + }); + }); + await Task.Delay(2000); + } + + //[Fact] + //public async Task CheckMemoryLeakAddContentTest() + //{ + // await StartServer(); + // Contents.GeneratedServices.CategoryClient client = new Contents.GeneratedServices.CategoryClient("http://localhost:2003", new System.Net.Http.HttpClient()); + // var categoryResult = await client.AddAsync(new Contents.GeneratedServices.CreateCategoryRequestContract() + // { + // Key = $"1-1-Title{Guid.NewGuid()}", + // UniqueIdentity = "1-2" + // }); + // Assert.True(categoryResult.IsSuccess); + // Contents.GeneratedServices.ContentClient contentClient = new Contents.GeneratedServices.ContentClient("http://localhost:2003", new System.Net.Http.HttpClient()); + // for (int i = 0; i < 5000; i++) + // { + // var contentResult = await contentClient.AddAsync(new Contents.GeneratedServices.CreateContentRequestContract() + // { + // CategoryId = categoryResult.Result, + // UniqueIdentity = "1-2", + // Data = i.ToString(), + // LanguageId = 1 + // }); + // Assert.True(contentResult.IsSuccess); + // } + + // while (true) + // { + // GC.Collect(); + // await Task.Delay(1000); + // } + //} + + //[Fact] + //public async Task MemoryLeackTest() + //{ + // HostApplicationBuilder builder = Host.CreateApplicationBuilder(null); + // UnitOfWork.DefaultUniqueIdentity = "1-1"; + // UnitOfWork.MicroserviceId = 10; + // builder.Services.AddTransient(); + // builder.Services.AddTransient(); + // builder.Services.AddTransient(); + // //builder.Services.AddTransient(); + + // using IHost host = builder.Build(); + + // for (int i = 0; i < 5000; i++) + // { + // using var scope = host.Services.CreateScope(); + // using var uow = scope.ServiceProvider.GetService(); + // //using var logic = uow.GetLongContractLogic(); + // } + //} + } +} \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/EasyMicroservices.ContentsMicroservice.Tests.csproj b/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/EasyMicroservices.ContentsMicroservice.Tests.csproj index 2f913ee..69ee562 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/EasyMicroservices.ContentsMicroservice.Tests.csproj +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/EasyMicroservices.ContentsMicroservice.Tests.csproj @@ -7,16 +7,32 @@ - + + + + + + PreserveNewest + true + Never + + + + + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + runtime; build; native; contentfiles; analyzers; buildtransitive all + + + 7.0.11 diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/appsettings.json b/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/appsettings.json new file mode 100644 index 0000000..b1fe191 --- /dev/null +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.Tests/appsettings.json @@ -0,0 +1,16 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*", + "ConnectionStrings": { + "local": "Server=testtt;Database=Contents;User ID=TrustServerCertificate=True;", // Test + }, + "RootAddresses": { + "WhiteLabel": "http://localhost:1041" + }, + "Urls": "http://*:2003" +} \ No newline at end of file diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/CategoryController.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/CategoryController.cs index 12ac62d..d8ce7e6 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/CategoryController.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/CategoryController.cs @@ -2,6 +2,7 @@ using EasyMicroservices.ContentsMicroservice.Contracts.Requests; using EasyMicroservices.ContentsMicroservice.Database.Entities; using EasyMicroservices.Cores.AspCoreApi; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.Cores.Database.Interfaces; using EasyMicroservices.ServiceContracts; using Microsoft.AspNetCore.Mvc; @@ -10,19 +11,17 @@ namespace EasyMicroservices.ContentsMicroservice.WebApi.Controllers { public class CategoryController : SimpleQueryServiceController { - private readonly IContractLogic _contractlogic; + private readonly IUnitOfWork _unitOfWork; - public CategoryController(IContractLogic contractLogic) : base(contractLogic) + public CategoryController(IUnitOfWork unitOfWork) : base(unitOfWork) { - _contractlogic = contractLogic; + _unitOfWork = unitOfWork; } - [HttpPost] public async Task> HasKey(IsKeyExistRequestContract request) { - var isKeyExists = await _contractlogic.GetBy(o => o.Key == request.Key); - + var isKeyExists = await _unitOfWork.GetLongContractLogic().GetBy(o => o.Key == request.Key); return isKeyExists; } } diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/ContentController.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/ContentController.cs index 2909c71..aa367ef 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/ContentController.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/ContentController.cs @@ -13,10 +13,11 @@ namespace EasyMicroservices.QuestionsMicroservice.WebApi.Controllers public class ContentController : SimpleQueryServiceController { readonly IUnitOfWork unitOfWork; - public ContentController(IUnitOfWork _unitOfWork) : base(null) + public ContentController(IUnitOfWork _unitOfWork) : base(_unitOfWork) { unitOfWork = _unitOfWork; } + public override async Task> Add(CreateContentRequestContract request, CancellationToken cancellationToken = default) { using var categorylogic = unitOfWork.GetLongContractLogic(); @@ -27,6 +28,7 @@ public override async Task> Add(CreateContentRequestContra return await base.Add(request, cancellationToken); return (EasyMicroservices.ServiceContracts.FailedReasonType.Incorrect, "Language or Categoryid is incorrect"); } + public override async Task> Update(UpdateContentRequestContract request, CancellationToken cancellationToken = default) { using var categorylogic = unitOfWork.GetLongContractLogic(); diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/LanguageController.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/LanguageController.cs index 76f159d..def0f67 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/LanguageController.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Controllers/LanguageController.cs @@ -2,7 +2,7 @@ using EasyMicroservices.ContentsMicroservice.Contracts.Requests; using EasyMicroservices.ContentsMicroservice.Database.Entities; using EasyMicroservices.Cores.AspCoreApi; -using EasyMicroservices.Cores.Database.Interfaces; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; using EasyMicroservices.ServiceContracts; using Microsoft.AspNetCore.Mvc; @@ -10,18 +10,18 @@ namespace EasyMicroservices.ContentsMicroservice.WebApi.Controllers { public class LanguageController : SimpleQueryServiceController { - private readonly IContractLogic _contractlogic; + private readonly IUnitOfWork _unitOfWork; - public LanguageController(IContractLogic contractLogic) : base(contractLogic) + public LanguageController(IUnitOfWork unitOfWork) : base(unitOfWork) { - _contractlogic = contractLogic; + _unitOfWork = unitOfWork; } [HttpPost] public async Task HasLanguage(HasLanguageRequestContract hasLanguageRequest) { - return await _contractlogic.GetBy(x => x.Name == hasLanguageRequest.Language); + return await _unitOfWork.GetLongContractLogic() + .GetBy(x => x.Name == hasLanguageRequest.Language); } - } } diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/EasyMicroservices.ContentsMicroservice.WebApi.csproj b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/EasyMicroservices.ContentsMicroservice.WebApi.csproj index 6cfd196..1983cb7 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/EasyMicroservices.ContentsMicroservice.WebApi.csproj +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/EasyMicroservices.ContentsMicroservice.WebApi.csproj @@ -6,7 +6,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Program.cs b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Program.cs index 3eb73f1..0e709b3 100644 --- a/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Program.cs +++ b/src/CSharp/EasyMicroservices.ContentsMicroservice.WebApi/Program.cs @@ -1,165 +1,37 @@ -using EasyMicroservices.ContentsMicroservice.Contracts.Common; -using EasyMicroservices.ContentsMicroservice.Contracts.Requests; -using EasyMicroservices.ContentsMicroservice.Database; using EasyMicroservices.ContentsMicroservice.Database.Contexts; -using EasyMicroservices.ContentsMicroservice.Database.Entities; -using EasyMicroservices.ContentsMicroservice.Interfaces; -using EasyMicroservices.Cores.AspEntityFrameworkCoreApi.Interfaces; -using EasyMicroservices.Cores.Relational.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.OpenApi.Any; -using Microsoft.OpenApi.Models; -using Swashbuckle.AspNetCore.SwaggerGen; -using System.Reflection; +using EasyMicroservices.Cores.AspEntityFrameworkCoreApi; +using EasyMicroservices.Cores.Relational.EntityFrameworkCore.Intrerfaces; namespace EasyMicroservices.ContentsMicroservice.WebApi { public class Program { - public static async Task Main(string[] args) { - var builder = WebApplication.CreateBuilder(args); - - IConfiguration config = new ConfigurationBuilder() - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .Build(); - - // Add services to the container. - //builder.Services.AddAuthorization(); - builder.Services.AddControllers(); - // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle - builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddSwaggerGen(options => - { - options.SchemaFilter(); - options.SchemaFilter(); - }); - - builder.Services.AddDbContext(options => - options.UseSqlServer(builder.Configuration.GetConnectionString(config.GetConnectionString("local"))) - ); - - //builder.Services.AddScoped((serviceProvider) => new DependencyManager().GetContractLogic()); - string webRootPath = @Directory.GetCurrentDirectory(); - - builder.Services.AddHttpContextAccessor(); - builder.Services.AddTransient((serviceProvider) => new DependencyManager().GetContractLogic()); - builder.Services.AddTransient((serviceProvider) => new DependencyManager().GetContractLogic()); - builder.Services.AddTransient((serviceProvider) => new DependencyManager().GetContractLogic()); - builder.Services.AddTransient(serviceProvider => new DatabaseBuilder()); - builder.Services.AddScoped(service => new AppUnitOfWork(service)); - builder.Services.AddTransient(service => new DependencyManager()); - builder.Services.AddTransient(service => new WhiteLabelManager(service, service.GetService())); - builder.Services.AddTransient(serviceProvider => new ContentContext(serviceProvider.GetService())); - builder.Services.AddTransient(serviceProvider => new ContentContext(serviceProvider.GetService())); - - //builder.Services.AddScoped(serviceProvider => new FileManagerProvider()); - //builder.Services.AddScoped(); - - //builder.Services.AddScoped(serviceProvider => new FileManager()); - //builder.Services.AddScoped(); - - var app = builder.Build(); - app.UseDeveloperExceptionPage(); - // Configure the HTTP request pipeline. - app.UseSwagger(); - app.UseSwaggerUI(); - - app.UseHttpsRedirection(); - app.UseAuthorization(); - app.MapControllers(); - - - //CreateDatabase(); - - using (var scope = app.Services.CreateScope()) - { - using var context = scope.ServiceProvider.GetService(); - await context.Database.EnsureCreatedAsync(); - //await context.Database.MigrateAsync(); - await context.DisposeAsync(); - var service = scope.ServiceProvider.GetService(); - await service.Initialize("Content", config.GetValue("RootAddresses:WhiteLabel"), typeof(ContentContext)); - } - - StartUp startUp = new StartUp(); - await startUp.Run(new DependencyManager()); - app.Run(); - } - - static void CreateDatabase() - { - using (var context = new ContentContext(new DatabaseBuilder())) - { - if (context.Database.EnsureCreated()) - { - //auto migration when database created first time - - //add migration history table - - string createEFMigrationsHistoryCommand = $@" -USE [{context.Database.GetDbConnection().Database}]; -SET ANSI_NULLS ON; -SET QUOTED_IDENTIFIER ON; -CREATE TABLE [dbo].[__EFMigrationsHistory]( - [MigrationId] [nvarchar](150) NOT NULL, - [ProductVersion] [nvarchar](32) NOT NULL, - CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY CLUSTERED -( - [MigrationId] ASC -)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] -) ON [PRIMARY]; -"; - context.Database.ExecuteSqlRaw(createEFMigrationsHistoryCommand); - - //insert all of migrations - var dbAssebmly = context.GetType().Assembly; - foreach (var item in dbAssebmly.GetTypes()) - { - if (item.BaseType == typeof(Migration)) - { - string migrationName = item.GetCustomAttributes().First().Id; - var version = typeof(Migration).Assembly.GetName().Version; - string efVersion = $"{version.Major}.{version.Minor}.{version.Build}"; - context.Database.ExecuteSqlRaw("INSERT INTO __EFMigrationsHistory(MigrationId,ProductVersion) VALUES ({0},{1})", migrationName, efVersion); - } - } - } - context.Database.Migrate(); - } + var app = CreateBuilder(args); + var build = await app.Build(); + build.MapControllers(); + build.Run(); } - } - public class GenericFilter : ISchemaFilter - { - public void Apply(OpenApiSchema schema, SchemaFilterContext context) + static WebApplicationBuilder CreateBuilder(string[] args) { - var type = context.Type; - - if (type.IsGenericType == false) - return; - - schema.Title = $"{type.Name[0..^2]}<{type.GenericTypeArguments[0].Name}>"; + var app = StartUpExtensions.Create(args); + app.Services.Builder(); + app.Services.AddTransient((serviceProvider) => new UnitOfWork(serviceProvider)); + app.Services.AddTransient(serviceProvider => new ContentContext(serviceProvider.GetService())); + app.Services.AddTransient(); + StartUpExtensions.AddWhiteLabel("Content", "RootAddresses:WhiteLabel"); + return app; } - } - public class XEnumNamesSchemaFilter : ISchemaFilter - { - private const string NAME = "x-enumNames"; - public void Apply(OpenApiSchema model, SchemaFilterContext context) + public static async Task Run(string[] args, Action use) { - var typeInfo = context.Type; - // Chances are something in the pipeline might generate this automatically at some point in the future - // therefore it's best to check if it exists. - if (typeInfo.IsEnum && !model.Extensions.ContainsKey(NAME)) - { - var names = Enum.GetNames(context.Type); - var arr = new OpenApiArray(); - arr.AddRange(names.Select(name => new OpenApiString(name))); - model.Extensions.Add(NAME, arr); - } + var app = CreateBuilder(args); + use?.Invoke(app.Services); + var build = await app.Build(); + build.MapControllers(); + build.Run(); } } } \ No newline at end of file