mellowzippy
mellowzippy
CC#
Created by mellowzippy on 10/1/2024 in #help
Unit Testing ASP.NET async api service
Okay. I am trying to make a unit test for my Customer Service. I tried starting with the Read operation which looks like this in my service:
public async Task<Customer> Read(int customerId)
{
return await _context.Customers
.FirstOrDefaultAsync(c => c.CustomerId == customerId);
}
public async Task<Customer> Read(int customerId)
{
return await _context.Customers
.FirstOrDefaultAsync(c => c.CustomerId == customerId);
}
I tried using this as a guide: https://learn.microsoft.com/en-us/ef/ef6/fundamentals/testing/mocking#testing-query-scenarios
33 replies
CC#
Created by mellowzippy on 9/14/2024 in #help
✅ ASP.NET 'web' application
My exercise says the following: The path of the folder where we store the addresses should be customized: extend your program to read it from the appsettings.. I think I should just make the path (which is a json file) an environment variable in the appsettings. Does that make sense to do or should I just ask the teacher? If there are any other options that might work with the use of appsettings then please tell me.
14 replies
CC#
Created by mellowzippy on 5/27/2024 in #help
✅ Readonly methods
My VSCode is telling me to change my methods to readonly. I can't find a good explanation anywhere as to what this does and why it's recommended. So I'd really like an explanation from someone who knows.
public struct Note(Book book, List<Entry> entries) : IEnumerable<Entry>
{
public Book book = book;
public List<Entry> entries = entries;

public Note(Book book) : this(book, []) { }

public Note(Book book, Entry entry) : this(book, [entry]) { }

public readonly void Add(Entry entry)
{
entries.Add(entry);
}

public readonly void Remove(string title)
{
foreach (Entry entry in entries)
{
if (entry.Title == title)
{
entries.Remove(entry);
}
}
}

public IEnumerator<Entry> GetEnumerator()
{
return entries.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
public struct Note(Book book, List<Entry> entries) : IEnumerable<Entry>
{
public Book book = book;
public List<Entry> entries = entries;

public Note(Book book) : this(book, []) { }

public Note(Book book, Entry entry) : this(book, [entry]) { }

public readonly void Add(Entry entry)
{
entries.Add(entry);
}

public readonly void Remove(string title)
{
foreach (Entry entry in entries)
{
if (entry.Title == title)
{
entries.Remove(entry);
}
}
}

public IEnumerator<Entry> GetEnumerator()
{
return entries.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
10 replies
CC#
Created by mellowzippy on 4/23/2024 in #help
✅ Mysterious byte problem
No description
45 replies
CC#
Created by mellowzippy on 1/29/2024 in #help
Having trouble understanding patterns
Hello, I`m a second year student and I'm reading about patterns like a factory pattern. I have trouble understanding why I should use patterns and when to use which pattern.
254 replies
CC#
Created by mellowzippy on 2/13/2023 in #help
❔ ✅ Trouble printing values
class Program
{
public static void Main()
{
var square = new Square(5);
Console.Write($"Side: {square.Side}");
Console.WriteLine($"Area: {square.Area()}");
Console.WriteLine($"Perimeter: {square.Perimeter()}");

var square2 = new Square(10);
Console.Write($"Side: {square2.Side}");
Console.WriteLine($"Area: {square2.Area()}");
Console.WriteLine($"Perimeter: {square2.Perimeter()}");
}
}

class Square
{
public int Side;

public Square(int side)
{ this.Side = side; }

public int Area() => this.Side * this.Side;
public int Perimeter() => this.Side * 4;
}
class Program
{
public static void Main()
{
var square = new Square(5);
Console.Write($"Side: {square.Side}");
Console.WriteLine($"Area: {square.Area()}");
Console.WriteLine($"Perimeter: {square.Perimeter()}");

var square2 = new Square(10);
Console.Write($"Side: {square2.Side}");
Console.WriteLine($"Area: {square2.Area()}");
Console.WriteLine($"Perimeter: {square2.Perimeter()}");
}
}

class Square
{
public int Side;

public Square(int side)
{ this.Side = side; }

public int Area() => this.Side * this.Side;
public int Perimeter() => this.Side * 4;
}
Expected output:
Side: 5
Area: 25
Perimeter: 20
Side: 10
Area: 100
Perimeter: 40
Side: 5
Area: 25
Perimeter: 20
Side: 10
Area: 100
Perimeter: 40
Actual output:
Side: 5Area: 25
Perimeter: 20
Side: 10Area: 100
Perimeter: 40
Side: 5Area: 25
Perimeter: 20
Side: 10Area: 100
Perimeter: 40
How do I get it to print under each other and not next to each other?
7 replies