From 1a90e4003baa5931538f23bba82d33950ff9fa9a Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Fri, 28 May 2021 19:08:23 +0200 Subject: [PATCH 001/151] Inizio versione gestione DB --- GWMS.Data/Constants.cs | 12 ++++ GWMS.Data/Controllers/GWMSController.cs | 42 +++++++++++ GWMS.Data/DatabaseModels/AnagArticoli.cs | 33 +++++++++ GWMS.Data/DatabaseModels/Config.cs | 32 +++++++++ GWMS.Data/{Class1.cs => Enum.cs} | 4 +- GWMS.Data/GWMS.Data.csproj | 12 ++++ GWMS.Data/GWMSContext.cs | 78 ++++++++++++++++++++ GWMS.Data/Resources/Readme.md | 26 +++++++ GWMS.UI/Data/GWMSDataService.cs | 92 ++++++++++++++++++++++++ Resources/ChangeLog.html | 2 +- Resources/VersNum.txt | 2 +- Resources/manifest.xml | 2 +- 12 files changed, 332 insertions(+), 5 deletions(-) create mode 100644 GWMS.Data/Constants.cs create mode 100644 GWMS.Data/Controllers/GWMSController.cs create mode 100644 GWMS.Data/DatabaseModels/AnagArticoli.cs create mode 100644 GWMS.Data/DatabaseModels/Config.cs rename GWMS.Data/{Class1.cs => Enum.cs} (67%) create mode 100644 GWMS.Data/GWMSContext.cs create mode 100644 GWMS.Data/Resources/Readme.md create mode 100644 GWMS.UI/Data/GWMSDataService.cs diff --git a/GWMS.Data/Constants.cs b/GWMS.Data/Constants.cs new file mode 100644 index 0000000..84ddf50 --- /dev/null +++ b/GWMS.Data/Constants.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GWMS.Data +{ + public class Constants + { + } +} \ No newline at end of file diff --git a/GWMS.Data/Controllers/GWMSController.cs b/GWMS.Data/Controllers/GWMSController.cs new file mode 100644 index 0000000..bf163f0 --- /dev/null +++ b/GWMS.Data/Controllers/GWMSController.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Configuration; +using NLog; + +namespace GWMS.Data.Controllers +{ + public class GWMSController : IDisposable + { + #region Private Fields + + private static IConfiguration _configuration; + private static GWMSContext dbCtx; + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + #endregion Private Fields + + #region Public Constructors + + public GWMSController(IConfiguration configuration) + { + _configuration = configuration; + dbCtx = new GWMSContext(configuration); + Log.Info("Avviata classe GWMSController"); + } + + #endregion Public Constructors + + #region Public Methods + + public void Dispose() + { + // Clear database context + dbCtx.Dispose(); + } + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/GWMS.Data/DatabaseModels/AnagArticoli.cs b/GWMS.Data/DatabaseModels/AnagArticoli.cs new file mode 100644 index 0000000..060b7c2 --- /dev/null +++ b/GWMS.Data/DatabaseModels/AnagArticoli.cs @@ -0,0 +1,33 @@ +using System; +using System.Linq; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace GWMS.Data.DatabaseModels +{ + /// + /// Tabella Articoli + /// + [Table("AnagArticoli")] + public class AnagArticoli + { + #region Public Properties + + [Key, Column("ArtId", Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int ArtId { get; set; } + + [Column("CodArticolo", Order = 1)] + public string CodArticolo { get; set; } = ""; + + [Column("DescArticolo", Order = 2)] + public string DescArticolo { get; set; } = ""; + + [Column("Tipo", Order = 3)] + public string Tipo { get; set; } = ""; + + [Column("UM", Order = 4)] + public string UM { get; set; } = ""; + + #endregion Public Properties + } +} \ No newline at end of file diff --git a/GWMS.Data/DatabaseModels/Config.cs b/GWMS.Data/DatabaseModels/Config.cs new file mode 100644 index 0000000..63b354c --- /dev/null +++ b/GWMS.Data/DatabaseModels/Config.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +#nullable disable + +namespace GWMS.Data.DatabaseModels +{ + /// + /// Tabella Articoli + /// + [Table("Config")] + public class Config + { + #region Public Properties + + [Key, Column("Chiave", Order = 0), MaxLength(50)] + public string Chiave { get; set; } + + [Column("Note", Order = 3)] + public string Note { get; set; } = ""; + + [Column("Valore", Order = 1)] + public string Valore { get; set; } = ""; + + [Column("ValoreStd", Order = 2)] + public string ValoreStd { get; set; } = ""; + + #endregion Public Properties + } +} \ No newline at end of file diff --git a/GWMS.Data/Class1.cs b/GWMS.Data/Enum.cs similarity index 67% rename from GWMS.Data/Class1.cs rename to GWMS.Data/Enum.cs index 0a2133b..1981f42 100644 --- a/GWMS.Data/Class1.cs +++ b/GWMS.Data/Enum.cs @@ -2,7 +2,7 @@ namespace GWMS.UI.Data { - public class Class1 + public class Enum { } -} +} \ No newline at end of file diff --git a/GWMS.Data/GWMS.Data.csproj b/GWMS.Data/GWMS.Data.csproj index 2a01bfc..4641e33 100644 --- a/GWMS.Data/GWMS.Data.csproj +++ b/GWMS.Data/GWMS.Data.csproj @@ -4,8 +4,16 @@ net5.0 + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -15,4 +23,8 @@ + + + + \ No newline at end of file diff --git a/GWMS.Data/GWMSContext.cs b/GWMS.Data/GWMSContext.cs new file mode 100644 index 0000000..ced122c --- /dev/null +++ b/GWMS.Data/GWMSContext.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.Extensions.Configuration; +using GWMS.Data.DatabaseModels; +using NLog; + +namespace GWMS.Data +{ + public partial class GWMSContext : DbContext + { + #region Private Fields + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + private IConfiguration _configuration; + + #endregion Private Fields + + #region Public Constructors + + public GWMSContext() + { + } + + public GWMSContext(IConfiguration configuration) + { + _configuration = configuration; + } + + public GWMSContext(DbContextOptions options) : base(options) + { + } + + #endregion Public Constructors + + #region Public Properties + + public virtual DbSet Configs { get; set; } + public virtual DbSet DbSetArticoli { get; set; } + + #endregion Public Properties + + #region Private Methods + + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); + + #endregion Private Methods + + #region Protected Methods + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + //string connString = ""; + if (!optionsBuilder.IsConfigured) + { + //connString = _configuration.GetConnectionString("GWMS.Data"); + //optionsBuilder.UseSqlServer(connString); + optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=GWMS;Trusted_Connection=True;"); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS"); + + modelBuilder.Entity(entity => + { + entity.Property(e => e.ValoreStd) + .HasComment("Valore di default/riferimento per la variabile"); + }); + + OnModelCreatingPartial(modelBuilder); + } + + #endregion Protected Methods + } +} \ No newline at end of file diff --git a/GWMS.Data/Resources/Readme.md b/GWMS.Data/Resources/Readme.md new file mode 100644 index 0000000..fad15f4 --- /dev/null +++ b/GWMS.Data/Resources/Readme.md @@ -0,0 +1,26 @@ +# Appunti gestione GWMS DB + + +Per la gestione dell'accesso al DB statistiche si opera con EFCore --> app blazor server + +## Scaffolding + +Per generare le classi da un DB esistente con cui operare EFCore CodeFirst usare lo scaffolding coi seguenti comandi. +Attenzione: la classe DbCOntext viene creata INSIEME alle viste nella folder DatabaseModel (nell'esempio seguente...) + +### DB iniziale + + Scaffold-DbContext "Server=SQL2016DEV;Database=GWMS;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DatabaseModels + +### SOLO di tabelle/viste selezionate (con force update) + +Scaffold-DbContext "Server=SQL2016DEV;Database=GWMS;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DatabaseModels -Tables nome_tabella, nome_vista + +## Approfondimenti + +Qualche link di approfondimento: + + - https://docs.microsoft.com/en-us/ef/core/ + - https://docs.microsoft.com/en-us/ef/core/extensions/ + - https://www.entityframeworktutorial.net/efcore/create-model-for-existing-database-in-ef-core.aspx + - https://entityframework.net/ef-code-first \ No newline at end of file diff --git a/GWMS.UI/Data/GWMSDataService.cs b/GWMS.UI/Data/GWMSDataService.cs new file mode 100644 index 0000000..fcc47ed --- /dev/null +++ b/GWMS.UI/Data/GWMSDataService.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Configuration; +using System.Text; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using GWMS.Data; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.Memory; +using Newtonsoft.Json; +using System.Diagnostics; +using NLog; + +namespace GWMS.UI.Data +{ + public class GWMSDataService + { + #region Private Fields + + private static IConfiguration _configuration; + private static ILogger _logger; + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + private readonly IDistributedCache distributedCache; + private readonly IMemoryCache memoryCache; + + /// + /// Durata assoluta massima della cache + /// + private int chAbsExp = 15; + + /// + /// Durata della cache in modalità inattiva (non acceduta) prima di venire rimossa + /// NON estende oltre il tempo massimo di validità della cache (chAbsExp) + /// + private int chSliExp = 5; + + #endregion Private Fields + + #region Protected Fields + + protected static string connStringBBM = ""; + + #endregion Protected Fields + + #region Public Fields + + public static GWMS.Data.Controllers.GWMSController dbController; + + #endregion Public Fields + + #region Public Constructors + + public GWMSDataService(IConfiguration configuration, ILogger logger, IMemoryCache memoryCache, IDistributedCache distributedCache) + { + _logger = logger; + _configuration = configuration; + // conf cache + this.memoryCache = memoryCache; + this.distributedCache = distributedCache; + // conf DB + string connStr = _configuration.GetConnectionString("GWMS.Data"); + if (string.IsNullOrEmpty(connStr)) + { + _logger.LogError("ConnString empty!"); + } + else + { + dbController = new GWMS.Data.Controllers.GWMSController(configuration); + StringBuilder sb = new StringBuilder(); + sb.AppendLine($"DbController OK"); + _logger.LogInformation(sb.ToString()); + } + } + + #endregion Public Constructors + + #region Private Properties + + private DistributedCacheEntryOptions cacheOpt + { + get + { + return new DistributedCacheEntryOptions().SetAbsoluteExpiration(DateTime.Now.AddMinutes(chAbsExp)).SetSlidingExpiration(TimeSpan.FromMinutes(chSliExp)); + } + } + + #endregion Private Properties + } +} \ No newline at end of file diff --git a/Resources/ChangeLog.html b/Resources/ChangeLog.html index 7726ee2..ad4448a 100644 --- a/Resources/ChangeLog.html +++ b/Resources/ChangeLog.html @@ -1,6 +1,6 @@ Modulo statistiche MAPO -

Versione: 1.0.2105.2811

+

Versione: 1.0.2105.2818


Note di rilascio: