Skip to content

Commit f413e24

Browse files
Merge branch 'main' into seo/3106-aspnetcore-webapi-best-practices
2 parents 2aeb232 + d1ee56d commit f413e24

11 files changed

Lines changed: 250 additions & 26 deletions

File tree

‎files-csharp/WriteFileToTempFolder/WriteFileToTempFolder.Test/TempFileCreatorTests.cs‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void GivenSystemCreatedFileName_WhenSaving_TheContentIsAsExpected()
3939
public void GivenSystemSpecialFolderAndCustomName_WhenSaving_TheContentIsAsExpected()
4040
{
4141
//Arrange
42-
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
42+
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
4343
var tempFile = Path.Combine(appDataPath, "text.txt");
4444

4545
//Act
@@ -51,4 +51,47 @@ public void GivenSystemSpecialFolderAndCustomName_WhenSaving_TheContentIsAsExpec
5151
//Assert
5252
Assert.Equal("Hello from CodeMaze!", tempFileContent);
5353
}
54-
}
54+
55+
[Fact]
56+
public void GivenTempSubdirectory_WhenSaving_TheContentIsAsExpected()
57+
{
58+
//Arrange
59+
var tempDirectory = Directory.CreateTempSubdirectory("codemaze-");
60+
var tempFile = Path.Combine(tempDirectory.FullName, "text.txt");
61+
62+
//Act
63+
TempFileCreator.CreateTempFile(tempFile);
64+
var tempFileContent = File.ReadAllText(tempFile);
65+
66+
tempDirectory.Delete(recursive: true);
67+
68+
//Assert
69+
Assert.Equal("Hello from CodeMaze!", tempFileContent);
70+
Assert.False(Directory.Exists(tempDirectory.FullName));
71+
}
72+
73+
[Fact]
74+
public void GivenDeleteOnClose_WhenStreamIsDisposed_TheFileIsGone()
75+
{
76+
//Arrange
77+
var tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
78+
var options = new FileStreamOptions
79+
{
80+
Mode = FileMode.CreateNew,
81+
Access = FileAccess.ReadWrite,
82+
Options = FileOptions.DeleteOnClose
83+
};
84+
85+
//Act
86+
bool existedWhileOpen;
87+
using (var stream = new FileStream(tempFile, options))
88+
{
89+
stream.Write("Hello from CodeMaze!"u8);
90+
existedWhileOpen = File.Exists(tempFile);
91+
}
92+
93+
//Assert
94+
Assert.True(existedWhileOpen);
95+
Assert.False(File.Exists(tempFile));
96+
}
97+
}

‎files-csharp/WriteFileToTempFolder/WriteFileToTempFolder.Test/WriteFileToTempFolder.Test.csproj‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net8.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77

@@ -10,13 +10,13 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
14-
<PackageReference Include="xunit" Version="2.4.2" />
15-
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
14+
<PackageReference Include="xunit" Version="2.9.3" />
15+
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
1616
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1717
<PrivateAssets>all</PrivateAssets>
1818
</PackageReference>
19-
<PackageReference Include="coverlet.collector" Version="6.0.0">
19+
<PackageReference Include="coverlet.collector" Version="10.1.0">
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2121
<PrivateAssets>all</PrivateAssets>
2222
</PackageReference>

‎files-csharp/WriteFileToTempFolder/WriteFileToTempFolder/Program.cs‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using WriteFileToTempFolder;
22

3-
var tempFile = Path.Combine(Path.GetTempPath(),"text.txt");
3+
var tempFile = Path.Combine(Path.GetTempPath(), "text.txt");
44
Console.WriteLine($"Creating temp file: '{tempFile}'");
55
TempFileCreator.CreateTempFile(tempFile);
66

@@ -22,12 +22,40 @@
2222

2323
Console.WriteLine();
2424

25-
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
25+
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
2626
tempFile = Path.Combine(appDataPath, "text.txt");
2727
Console.WriteLine($"Creating temp file: '{tempFile}'");
2828
TempFileCreator.CreateTempFile(tempFile);
2929

3030
Console.WriteLine($"Reading temp file contents: '{tempFile}'");
3131
tempFileContent = File.ReadAllText(tempFile);
3232
Console.WriteLine(tempFileContent);
33-
File.Delete(tempFile);
33+
File.Delete(tempFile);
34+
35+
Console.WriteLine();
36+
37+
var tempDirectory = Directory.CreateTempSubdirectory("codemaze-");
38+
tempFile = Path.Combine(tempDirectory.FullName, "text.txt");
39+
Console.WriteLine($"Creating temp file: '{tempFile}'");
40+
TempFileCreator.CreateTempFile(tempFile);
41+
42+
Console.WriteLine(File.ReadAllText(tempFile));
43+
tempDirectory.Delete(recursive: true);
44+
45+
Console.WriteLine();
46+
47+
tempFile = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
48+
var options = new FileStreamOptions
49+
{
50+
Mode = FileMode.CreateNew,
51+
Access = FileAccess.ReadWrite,
52+
Options = FileOptions.DeleteOnClose
53+
};
54+
55+
using (var stream = new FileStream(tempFile, options))
56+
{
57+
stream.Write("Hello from CodeMaze!"u8);
58+
Console.WriteLine($"Temp file '{tempFile}' exists? {File.Exists(tempFile)}");
59+
}
60+
61+
Console.WriteLine($"Temp file '{tempFile}' exists? {File.Exists(tempFile)}");

‎files-csharp/WriteFileToTempFolder/WriteFileToTempFolder/WriteFileToTempFolder.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace RetrieveJSONProperty.DTOs;
4+
5+
public class Customer
6+
{
7+
[JsonPropertyName("FirstName")]
8+
public string? Name { get; set; }
9+
10+
[JsonIgnore]
11+
public string? Password { get; set; }
12+
13+
public int Age { get; set; }
14+
}

‎json-csharp/RetrieveJSONProperty/RetrieveJSONProperty/Helper/JsonHelper.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static IEnumerable<string> RetrievalUsingSerialization(object obj)
2222
{
2323
string json = JsonConvert.SerializeObject(obj);
2424

25-
var values = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
25+
var values = JsonConvert.DeserializeObject<Dictionary<string, object?>>(json);
2626

2727
return values != null
2828
? values.Keys

‎json-csharp/RetrieveJSONProperty/RetrieveJSONProperty/Helper/JsonTextImplementation.cs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Reflection;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization.Metadata;
24
using System.Text.Json.Serialization;
35

46
namespace RetrieveJSONProperty.Helper;
@@ -16,4 +18,17 @@ public static string[] GetJsonPropertyNames(object obj)
1618

1719
return propertyNames;
1820
}
19-
}
21+
22+
public static string[] GetSerializedPropertyNames<T>(JsonSerializerOptions? options = null)
23+
{
24+
options ??= JsonSerializerOptions.Default;
25+
options.MakeReadOnly(populateMissingResolver: true);
26+
27+
JsonTypeInfo typeInfo = options.GetTypeInfo(typeof(T));
28+
29+
return typeInfo.Properties
30+
.Where(property => property.Get is not null || property.Set is not null)
31+
.Select(property => property.Name)
32+
.ToArray();
33+
}
34+
}

‎json-csharp/RetrieveJSONProperty/RetrieveJSONProperty/Program.cs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Console.WriteLine("2. ListPropertywithValue");
1212
Console.WriteLine("3. ResolverImplementation");
1313
Console.WriteLine("4. AlternateImplementation();");
14+
Console.WriteLine("5. SerializerMetadataImplementation");
1415

1516
string? option = Console.ReadLine();
1617

@@ -32,6 +33,10 @@
3233
AlternateImplementation();
3334
break;
3435

36+
case "5":
37+
SerializerMetadataImplementation();
38+
break;
39+
3540
case "exit":
3641
continueExecution = false;
3742
break;
@@ -92,6 +97,17 @@ static void AlternateImplementation()
9297
}
9398
}
9499

100+
static void SerializerMetadataImplementation()
101+
{
102+
var propertyNames = JsonTextImplementation.GetSerializedPropertyNames<Sales>();
103+
104+
Console.WriteLine("JSON property names:");
105+
foreach (var propertyName in propertyNames)
106+
{
107+
Console.WriteLine(propertyName);
108+
}
109+
}
110+
95111
static Sales CreateSalesObject()
96112
{
97113
Console.WriteLine("Enter YearlySales:");

‎json-csharp/RetrieveJSONProperty/RetrieveJSONProperty/RetrieveJSONProperty.csproj‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net7.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
11+
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
1212
</ItemGroup>
1313

1414
</Project>

0 commit comments

Comments
 (0)