PI06 i PI06-1. Docker definitions for MSSQL and Postgres. Data seeder/generator for countries and people. Entity Framework example with variants for Postgres and MSSQL

This commit is contained in:
Boris Milašinović
2026-04-19 16:49:07 +02:00
parent 44a663e170
commit 6f56d107a2
89 changed files with 7305 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Inheritance\Inheritance.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,49 @@
using Inheritance;
namespace CovarianceContravariance;
class Program
{
static void Main(string[] args)
{
List<Car> cars = new List<Car>{
new Car("Coupe", 200, 3),
new Car("Wagon", 100, 5),
new Car("Limousine", 150, 4),
new ElectricCar("Electric Hatchback", 148, 5, 40)
};
PrintVehicles(cars);
Comparison<Vehicle> comparisonFunction = (a, b) => -a.HorsePower.CompareTo(b.HorsePower);
IComparer<Vehicle> comparer = Comparer<Vehicle>.Create(comparisonFunction);
PrintBetterCar(cars[2], cars[3], comparer);
PrintBetterCar(cars.Skip(2).First(), cars.Last(), comparisonFunction);
PrintBetterCar(cars[2], cars[3], (a, b) => b.Doors - a.Doors);
}
static void PrintVehicles(IEnumerable<Vehicle> vehicles)
{
foreach (var vehicle in vehicles)
{
Console.WriteLine("\t " + vehicle.Model);
}
}
static void PrintBetterCar(Car a, Car b, IComparer<Car> comparer)
{
int result = comparer.Compare(a, b);
string betterModel = result <= 0 ? a.Model : b.Model;
string worseModel = result <= 0 ? b.Model : a.Model;
Console.WriteLine($"\t{betterModel} is better or equal than {worseModel}");
}
static void PrintBetterCar(Car a, Car b, Comparison<Car> comparer)
{
int result = comparer(a, b);
string betterModel = result <= 0 ? a.Model : b.Model;
string worseModel = result <= 0 ? b.Model : a.Model;
Console.WriteLine($"\t{betterModel} is better or equal than {worseModel}");
}
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,26 @@
using System;
namespace Delegates;
public class MathTool
{
public static int Sum(int x, int y)
{
return x + y;
}
public static int Diff(int x, int y)
{
return x - y;
}
public static void PrintSquare(int x)
{
Console.WriteLine("x^2 = " + x * x);
}
public static void PrintSquareRoot(int x)
{
Console.WriteLine("sqrt(x) = " + Math.Sqrt(x));
}
}

View File

@@ -0,0 +1,31 @@
namespace Delegates;
class Program
{
public delegate int MathFunction(int a, int b);
public delegate void PrintFunction(int n);
static void Main(string[] args)
{
int x = 16, y = 2;
MathFunction mf = MathTool.Sum;
Console.WriteLine("mf({0}, {1}) = {2}", x, y, mf(x, y));
mf = MathTool.Diff;
Console.WriteLine("mf({0}, {1}) = {2}", x, y, mf(x, y));
PrintFunction pf = MathTool.PrintSquare;
pf += MathTool.PrintSquareRoot;
pf(x);
#pragma warning disable CS8601 // Possible null reference assignment.
pf -= MathTool.PrintSquare;
#pragma warning restore CS8601 // Possible null reference assignment.
Console.WriteLine();
#pragma warning disable CS8602 // Dereference of a possibly null reference.
pf(y); //instead of pragma pf?.Invoke(y); can be used
#pragma warning restore CS8602 // Dereference of a possibly null reference.
Func<int, int, int> func = MathTool.Sum;
Console.WriteLine("func({0}, {1}) = {2}", x, y, func(x, y));
Action<int> action = MathTool.PrintSquare;
action(x);
}
}

View File

@@ -0,0 +1,16 @@
using System;
namespace Extensions;
[Flags]
public enum DayPeriod
{
Morning = 1, Evening = 2, Afternoon = 4, Night = 8
}
public class Activity
{
public required string Person { get; set; }
public int Hours { get; set; }
public DayPeriod Period { get; set; }
}

View File

@@ -0,0 +1,18 @@
using System.Collections.Generic;
namespace Extensions;
public static class Extensions
{
public static V GetOrCreate<K, V>(this Dictionary<K, V> dict, K key)
where V : new()
where K: notnull
{
if (!dict.TryGetValue(key, out V? value))
{
value = new V();
dict[key] = value;
}
return value;
}
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,61 @@
namespace Extensions;
public enum Days
{
Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday
}
class Program
{
static void Main(string[] args)
{
var dict = new Dictionary<Days, List<Activity>>();
Activity a = new Activity
{
Person = "John",
Hours = 3,
Period = DayPeriod.Morning
};
dict.GetOrCreate(Days.Monday).Add(a);
a = new Activity
{
Person = "Mary",
Hours = 12,
Period = DayPeriod.Evening | DayPeriod.Night
};
dict.GetOrCreate(Days.Monday).Add(a);
a = new Activity
{
Person = "Peter",
Hours = 8,
Period = DayPeriod.Night
};
dict.GetOrCreate(Days.Tuesday).Add(a);
a = new Activity
{
Person = "Lisa",
Hours = 5,
Period = DayPeriod.Night | DayPeriod.Morning
};
dict.GetOrCreate(Days.Monday).Add(a);
int sum = SumNightActivity(dict);
Console.WriteLine(sum);
}
private static int SumNightActivity(Dictionary<Days, List<Activity>> dict)
{
int sum = 0;
foreach(var list in dict.Values)
{
sum += list.Where(a => (a.Period & DayPeriod.Night) == DayPeriod.Night)
.Sum(a => a.Hours);
}
return sum;
}
}

View File

@@ -0,0 +1,31 @@
using System;
namespace Inheritance;
public class Car : Vehicle
{
public int Doors { get; set; }
public string Color { get; set; } = "White";
public Car(string model, double horsePower, int doors)
: base(model, horsePower)
{
Console.WriteLine($"Creating car {model}");
this.Doors = doors;
}
public void BuckleSeatBelt()
{
Console.WriteLine("Car {0} - seat belt buckled", Model);
}
public override void Start()
{
Console.WriteLine("Start Car " + Model);
}
public new void Stop()
{
Console.WriteLine("Stop Car " + Model);
}
}

View File

@@ -0,0 +1,13 @@
namespace Inheritance;
public class ElectricCar : Car
{
public int BatteryCapacity { get; set; }
public int? Range { get; set; }
public ElectricCar(string model, double horsePower, int doors, int batteryCapacity) :
base(model, horsePower, doors)
{
this.BatteryCapacity = batteryCapacity;
}
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,18 @@
using System;
namespace Inheritance;
public class Motorbike : Vehicle
{
public bool HasSidecar { get; set; }
public Motorbike(string model, double horsePower, bool sidecar)
: base(model, horsePower)
{
Console.WriteLine($"Creating car {model}");
this.HasSidecar = sidecar;
}
public override void Start()
{
Console.WriteLine("Start Motorbike " + Model);
}
}

View File

@@ -0,0 +1,39 @@
using Inheritance;
Vehicle v = new Vehicle("BMW Isseta", 9.5);
v.Start();
Car car = new ElectricCar("Nissan Leaf", 148, 5, 40)
{
Range = 270
};
Console.WriteLine("car model = " + car.Model);
Console.WriteLine("car horse power = " + car.HorsePower);
Console.WriteLine("car number of doors = " + car.Doors);
if (car is ElectricCar electric)
{
Console.WriteLine("car battery = " + electric.BatteryCapacity);
if (electric.Range.HasValue)
{
Console.WriteLine("car range = " + electric.Range);
}
}
car.BuckleSeatBelt();
car.Start();
car.Stop();
((Vehicle)car).Start();
((Vehicle)car).Stop();
// objekt tipa Motocikl
Motorbike moto = new Motorbike("Ural", 41, true);
Console.WriteLine("motorbike model {0} ", moto.Model);
Console.WriteLine("motorbike horse power = " + moto.HorsePower);
Console.WriteLine($"motorbike has sidecar? = {moto.HasSidecar}");
moto.Start();
moto.Stop();
((Vehicle)moto).Start();
((Vehicle)moto).Stop();

View File

@@ -0,0 +1,26 @@
using System;
namespace Inheritance;
public class Vehicle
{
public string Model { get; private set; }
public double HorsePower { get; set; }
public Vehicle(string model, double horsePower)
{
Console.WriteLine("Creating vehicle " + model);
this.Model = model;
this.HorsePower = horsePower;
}
public void Stop()
{
Console.WriteLine("Stop vehicle " + Model);
}
public virtual void Start()
{
Console.WriteLine("Start vehicle " + Model);
}
}

View File

@@ -0,0 +1,48 @@
using PropertiesIndexersRefOut;
Triple t1 = new Triple
{
First = 456,
Second = 789
};
Triple t2 = new Triple
{
First = 1000,
Second = 2000
};
Console.WriteLine("t1 + t2 = " + (t1 + t2));
CreateRandomTriple(t1, ref t2, out Triple t3);
Console.WriteLine("t1 = " + t1);
Console.WriteLine("t2 = " + t2);
Console.WriteLine("t3 = " + t3);
Console.WriteLine(t1["A", 1]);
Console.WriteLine(t1["B", 2]);
Console.WriteLine(t1["B", 3]);
PrintTriples(t1, t2, t3);
void PrintTriples(params Triple[] triples)
{
foreach (var t in triples)
{
Console.WriteLine(t);
}
}
void CreateRandomTriple(Triple t1, ref Triple t2, out Triple t3)
{
Random r = new Random();
t3 = new Triple
{
First = r.Next(t1.First, t2.Second),
Second = r.Next(maxValue: t2.Second, minValue: t1.First)
};
t1 = t3;
t2 = t3;
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,73 @@
namespace PropertiesIndexersRefOut;
public class Triple
{
private int first;
public int First
{
get
{
return first;
}
set
{
first = value;
}
}
public int Second { get; set; }
public int Third => First + Second;
public override string ToString()
{
return $"({First}, {Second}, {Third})";
}
#region Indexer
public int this[string s, int position]
{
get
{
int num;
switch (s)
{
case "A":
num = this.First;
break;
case "B":
num = this.Second;
break;
case "C":
num = this.Third;
break;
default:
throw new ArgumentException("Invalid position in triple (should be A, B, or C)");
}
int digit = 0;
while(num > 0 && position > 0)
{
digit = num % 10;
num /= 10;
position--;
}
return digit;
}
}
#endregion
#region Operator overloading
public static Triple operator +(Triple x, Triple y)
{
return new Triple
{
First = x.First + y.First,
Second = x.Second + y.Second
};
}
#endregion
}

View File

@@ -0,0 +1,55 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33424.131
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Delegates", "Delegates\Delegates.csproj", "{EABA8FC4-42FB-4DAD-96E9-47A5B1057CFB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Extensions", "Extensions\Extensions.csproj", "{47B09D5D-50E4-4827-B954-A40B96052B83}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CovarianceContravariance", "CovarianceContravariance\CovarianceContravariance.csproj", "{007533F4-FE60-4E0C-AF11-004BF8A2C8F8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Inheritance", "Inheritance\Inheritance.csproj", "{BEB733F2-BBB4-4170-B530-EFF7C5797559}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PropertiesIndexersRefOut", "PropertiesIndexersRefOut\PropertiesIndexersRefOut.csproj", "{C6F8925F-2947-4A78-8E1A-543D13B7ED6B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ValueTuplesDeconstruction", "ValueTuplesDeconstruction\ValueTuplesDeconstruction.csproj", "{2518859E-D8D5-418A-A92B-411277F86479}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{EABA8FC4-42FB-4DAD-96E9-47A5B1057CFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EABA8FC4-42FB-4DAD-96E9-47A5B1057CFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EABA8FC4-42FB-4DAD-96E9-47A5B1057CFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EABA8FC4-42FB-4DAD-96E9-47A5B1057CFB}.Release|Any CPU.Build.0 = Release|Any CPU
{47B09D5D-50E4-4827-B954-A40B96052B83}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{47B09D5D-50E4-4827-B954-A40B96052B83}.Debug|Any CPU.Build.0 = Debug|Any CPU
{47B09D5D-50E4-4827-B954-A40B96052B83}.Release|Any CPU.ActiveCfg = Release|Any CPU
{47B09D5D-50E4-4827-B954-A40B96052B83}.Release|Any CPU.Build.0 = Release|Any CPU
{007533F4-FE60-4E0C-AF11-004BF8A2C8F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{007533F4-FE60-4E0C-AF11-004BF8A2C8F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{007533F4-FE60-4E0C-AF11-004BF8A2C8F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{007533F4-FE60-4E0C-AF11-004BF8A2C8F8}.Release|Any CPU.Build.0 = Release|Any CPU
{BEB733F2-BBB4-4170-B530-EFF7C5797559}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BEB733F2-BBB4-4170-B530-EFF7C5797559}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEB733F2-BBB4-4170-B530-EFF7C5797559}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEB733F2-BBB4-4170-B530-EFF7C5797559}.Release|Any CPU.Build.0 = Release|Any CPU
{C6F8925F-2947-4A78-8E1A-543D13B7ED6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C6F8925F-2947-4A78-8E1A-543D13B7ED6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6F8925F-2947-4A78-8E1A-543D13B7ED6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6F8925F-2947-4A78-8E1A-543D13B7ED6B}.Release|Any CPU.Build.0 = Release|Any CPU
{2518859E-D8D5-418A-A92B-411277F86479}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2518859E-D8D5-418A-A92B-411277F86479}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2518859E-D8D5-418A-A92B-411277F86479}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2518859E-D8D5-418A-A92B-411277F86479}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {88C158C1-838B-42EB-A159-F5F83BF2DDEA}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,22 @@
//ValueTuples https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/deconstruct
(int a, int b, int _) t1 = CreateRandomTriple();
Console.WriteLine(t1);
Console.WriteLine(t1.b);
var (_, b, _) = CreateRandomTriple();
Console.WriteLine(b);
var t2 = CreateRandomTriple();
Console.WriteLine(t2);
Console.WriteLine(t2.Item2);
(int, int, int) CreateRandomTriple()
{
Random r = new Random();
var triple = (r.Next(100), r.Next(100), r.Next(100));
Console.WriteLine($"Created {triple}");
return triple;
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>