Fungerande version av skiktad lösning Winforms App med SQlite och entity framework

This commit is contained in:
2023-08-23 21:25:09 +02:00
parent 1f39c39d96
commit 40632aa92c
17 changed files with 485 additions and 6 deletions

View File

@ -1,26 +1,44 @@
using DIDemoLib;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using WinFormDiApp.DAL;
namespace WinFormDi
{
public static class ContainerConfig
{
public static IHost? Configure()
public static IHost? Configure(IHostBuilder hostBuilder)
{
var builder = new HostBuilder()
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json")
.Build();
var builder = hostBuilder // new HostBuilder()
.ConfigureServices((_, services) =>
{
var conn = configuration.GetConnectionString("DatabaseConnection");
services
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(conn))
.AddTransient<IMessages, Messages>()
.AddTransient<MainWindow>();
});
return builder.Build();
}
}
}

BIN
WinFormDi/Local.db Normal file

Binary file not shown.

View File

@ -1,6 +1,8 @@
using DIDemoLib;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using WinFormDiApp.DAL;
using WinFormDiApp.DAL.Data;
namespace WinFormDi
{
@ -10,15 +12,19 @@ namespace WinFormDi
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
static void Main(string[] args)
{
var host = ContainerConfig.Configure();
var host = ContainerConfig.Configure(CreateHostBuilder(args));
using var scope = host.Services.CreateScope();
try
{
var services = scope.ServiceProvider;
var context = services.GetRequiredService<ApplicationDbContext>();
DataSeeder.Initialize(context);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.SetHighDpiMode(HighDpiMode.SystemAware);
@ -32,5 +38,11 @@ namespace WinFormDi
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args);
}
}

View File

@ -9,12 +9,29 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DIDemoLib\DIDemoLib.csproj" />
<ProjectReference Include="..\WinFormDiApp.BL\WinFormDiApp.BL.csproj" />
<ProjectReference Include="..\WinFormDiApp.DAL\WinFormDiApp.DAL.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Local.db">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,11 @@
{
"ConnectionStrings": {
"DatabaseConnection": "Data Source=.\\Local.db"
},
"Logging": {
"LogLevel": {
"Default" : "Warning"
}
},
"AllowedHosts": "*"
}

View File

@ -0,0 +1,26 @@
namespace WinFormDiApp.BL.Helpers;
public static class Extensions
{
public static bool IsNumeric(this string value)
{
if (!string.IsNullOrEmpty(value))
{
if (value.All(char.IsDigit)) return true;
else return false;
}
else return false;
}
public static bool IsDate(this string value) {
if (!string.IsNullOrEmpty(value) && value.Length<20)
{
if (DateTime.TryParse(value, out DateTime date))
{
return true;
}
else return false;
}
else return false;
}
}

View File

@ -0,0 +1,19 @@
using WinFormDiApp.BL.Models.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormDiApp.BL.Models
{
public class AccountRecord : BaseEntity
{
public DateTime BetalDatum { get; set; } = DateTime.MinValue;
public string Mottagare { get; set; } = string.Empty;
public string Konto { get; set; } = string.Empty;
public double Belopp { get; set; }
public string Avisering { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,10 @@
using System.ComponentModel.DataAnnotations;
namespace WinFormDiApp.BL.Models.Common
{
public abstract class BaseEntity
{
[Key]
public int Id { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using WinFormDiApp.BL.Models.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WinFormDiApp.BL.Models;
public class Member : BaseEntity
{
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string NickName { get; set; } = string.Empty;
public string PersonType { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\Enums\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using WinFormDiApp.BL.Models;
namespace WinFormDiApp.DAL
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
public virtual DbSet<Member> Members { get; set; }
public virtual DbSet<AccountRecord> AccountRecords { get; set; }
}
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(@Directory.GetCurrentDirectory() + "/../WinFormDi/appsettings.json")
.Build();
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
var connectionString = configuration.GetConnectionString("DatabaseConnection");
builder.UseSqlite(connectionString);
return new ApplicationDbContext(builder.Options);
}
}
}

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinFormDiApp.BL.Models;
namespace WinFormDiApp.DAL.Data
{
public class DataSeeder
{
public static void Initialize(ApplicationDbContext context)
{
if (!context.Members.Any())
{
var members = new List<Member>()
{
new Member { /*Id = 1,*/ FirstName = "John", LastName="Doe", NickName="Doey", PersonType="Normal", Email = "john@john.com" },
new Member { /*Id = 2,*/ FirstName = "Michael", LastName="Jordan", NickName="Joardie", PersonType="Normal", Email = "michael@michael.com" }
};
context.Members.AddRange(members);
context.SaveChanges();
}
if (!context.AccountRecords.Any())
{
var accountRecords = new List<AccountRecord>()
{
new AccountRecord { /* Id = 1 */ Avisering = "EInvoice", Belopp=10.0, BetalDatum=DateTime.Parse("2023-05-05"), Konto="BG 5787-1030", Mottagare="Kalles Bärplockning" },
new AccountRecord { /* Id = 1 */ Avisering = "EInvoice", Belopp = 13.0, BetalDatum = DateTime.Parse("2023-05-07"), Konto = "PG 4158502-7", Mottagare = "Nisses Strumpor" }
};
context.AccountRecords.AddRange(accountRecords);
context.SaveChanges();
}
}
}
}

View File

@ -0,0 +1,85 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WinFormDiApp.DAL;
#nullable disable
namespace WinFormDiApp.DAL.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20230822204611_Initial")]
partial class Initial
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.10");
modelBuilder.Entity("WinFormDiApp.BL.Models.AccountRecord", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Avisering")
.IsRequired()
.HasColumnType("TEXT");
b.Property<double>("Belopp")
.HasColumnType("REAL");
b.Property<DateTime>("BetalDatum")
.HasColumnType("TEXT");
b.Property<string>("Konto")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Mottagare")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("AccountRecords");
});
modelBuilder.Entity("WinFormDiApp.BL.Models.Member", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("NickName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("PersonType")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Members");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,59 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace WinFormDiApp.DAL.Migrations
{
/// <inheritdoc />
public partial class Initial : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "AccountRecords",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
BetalDatum = table.Column<DateTime>(type: "TEXT", nullable: false),
Mottagare = table.Column<string>(type: "TEXT", nullable: false),
Konto = table.Column<string>(type: "TEXT", nullable: false),
Belopp = table.Column<double>(type: "REAL", nullable: false),
Avisering = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AccountRecords", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Members",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
FirstName = table.Column<string>(type: "TEXT", nullable: false),
LastName = table.Column<string>(type: "TEXT", nullable: false),
NickName = table.Column<string>(type: "TEXT", nullable: false),
PersonType = table.Column<string>(type: "TEXT", nullable: false),
Email = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Members", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "AccountRecords");
migrationBuilder.DropTable(
name: "Members");
}
}
}

View File

@ -0,0 +1,82 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WinFormDiApp.DAL;
#nullable disable
namespace WinFormDiApp.DAL.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.10");
modelBuilder.Entity("WinFormDiApp.BL.Models.AccountRecord", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Avisering")
.IsRequired()
.HasColumnType("TEXT");
b.Property<double>("Belopp")
.HasColumnType("REAL");
b.Property<DateTime>("BetalDatum")
.HasColumnType("TEXT");
b.Property<string>("Konto")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Mottagare")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("AccountRecords");
});
modelBuilder.Entity("WinFormDiApp.BL.Models.Member", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Email")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("NickName")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("PersonType")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Members");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.10" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.10">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\WinFormDiApp.BL\WinFormDiApp.BL.csproj" />
</ItemGroup>
</Project>

View File

@ -3,9 +3,13 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33829.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinFormDi", "WinFormDi\WinFormDi.csproj", "{940A0A13-2195-47DD-9FDE-16A0C57AF3BF}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WinFormDi", "WinFormDi\WinFormDi.csproj", "{940A0A13-2195-47DD-9FDE-16A0C57AF3BF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DIDemoLib", "DIDemoLib\DIDemoLib.csproj", "{839E6DE6-4644-4279-A7B1-449BD63897A3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DIDemoLib", "DIDemoLib\DIDemoLib.csproj", "{839E6DE6-4644-4279-A7B1-449BD63897A3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinFormDiApp.BL", "WinFormDiApp.BL\WinFormDiApp.BL.csproj", "{4D49AE4B-26D6-48C6-B1CA-48D428E95BE0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WinFormDiApp.DAL", "WinFormDiApp.DAL\WinFormDiApp.DAL.csproj", "{C6E355AD-C907-45D2-815B-A1B4207656FB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -21,6 +25,14 @@ Global
{839E6DE6-4644-4279-A7B1-449BD63897A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{839E6DE6-4644-4279-A7B1-449BD63897A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{839E6DE6-4644-4279-A7B1-449BD63897A3}.Release|Any CPU.Build.0 = Release|Any CPU
{4D49AE4B-26D6-48C6-B1CA-48D428E95BE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D49AE4B-26D6-48C6-B1CA-48D428E95BE0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D49AE4B-26D6-48C6-B1CA-48D428E95BE0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D49AE4B-26D6-48C6-B1CA-48D428E95BE0}.Release|Any CPU.Build.0 = Release|Any CPU
{C6E355AD-C907-45D2-815B-A1B4207656FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C6E355AD-C907-45D2-815B-A1B4207656FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6E355AD-C907-45D2-815B-A1B4207656FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6E355AD-C907-45D2-815B-A1B4207656FB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE