big OOF
big OOF
CC#
Created by big OOF on 4/11/2023 in #help
❔ QuestionController
Hello, i have a question regarding API structure. I have two classses, Item and Store. I created CRUD endpoints for both. Lets say i want to be able to return a store including all the items related to that store, and also be able to to POST a store with items. Would you create a seperate controller for these endpoints, for example StoreIemController containing PostStoreWithItems/GetAllStoresWithItems or use existing Store/ItemController? Thanks in advance 🙂
9 replies
CC#
Created by big OOF on 3/28/2023 in #help
❔ Dev/prod databse
Hello, Me and a friend wants to create a webb-app project with a react frontend and C# backend. The idea was to use AWS RDS with mySQL for the final product. To my question - what are the pros and cons/best practise of: Set up and use a RDS(public so we both can acess it from our local) database from the start. VS We both use local mySQL for development and configure a "private" RDS at a later stage when the app is deployed on AWS? Whats the most common way to approach this? Thanks in advance! 🙂
6 replies
CC#
Created by big OOF on 3/14/2023 in #help
❔ Dezerialise JSON response
Example: person { 18370101 (changing social security number) {
name: name, age: age description: "<p> This is a person with name and age </p>" } }
I want to dezerialise the json response into <Person> but the social security number dosent belong to a json property. From what i understand you can use something called Jtoken to solve this but it dosent feel quite right. Are there any alternatives? Also the field description contains html-elements, when im trying to dezerialise it throws an error, one again - any ideas? 🙂 Thanks in advance!
10 replies
CC#
Created by big OOF on 2/24/2023 in #help
❔ Retrieve API data in C#
What structure would you use if you want to create an API that retrieves data from another API and returns it to the user. Im thinking: 1. API controller takes the GET request from user and calls the service class 2. Service class that makes the API call At the moment im instantiating a httpClient in the service class but ideally i would want the functions in the service class to be static, but then i cant instantiate httpclient. Is this the right "way/structure" to do it and/or do any tips regarding the static service class ? 🙂 Thanks!
20 replies
CC#
Created by big OOF on 2/16/2023 in #help
✅ API result
Hello, I have two tables, group and person. These are created in Sqlite using EntityFramework and therefore the i have two classes like this:
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Group
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Group> Group{ get; set; }
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Group> Group{ get; set; }
}
From what i understand the line: "public ICollection<Group> Group{ get; set; }" creates a relation between the two tables(the id) when created by EF. So to my question - when i try to retrieve Person via API(postman) i only get Id and Name. The goal is to get The person object but with Group nested in the JSON response. Is there a step im missing? 🙂
Thanks in advance!
86 replies
CC#
Created by big OOF on 1/9/2023 in #help
❔ Architecture
Hello, If you have a API-controller class with a GET method inside. Lets say the GET should return a math calculation that is done in the function ExampleFunction(). The GET inside the api-controller needs to call the ExampleFunction. So to my question, where should you place ExmapleFunction()? From what i understand: - Not in the API controller - Maybe inside a service class? - Create a "general methods class"? Thanks in advance 🙂
6 replies
CC#
Created by big OOF on 11/25/2022 in #help
✅ MS webshop example
Hello, Im trying to understand the different parts of a webshop example from Microsoft. I have a question about routing, the view looks like this(see attached). My question is regarding this line:
<form asp-action="OrderProcess" id="orderForm+@item.OrderNumber" method="post">
<form asp-action="OrderProcess" id="orderForm+@item.OrderNumber" method="post">
I find the function OrderProcess in OrderManagementController and it takes a POST-request:
[HttpPost]
public async Task<IActionResult> OrderProcess(string orderId, string actionCode)
{
if (OrderProcessAction.Ship.Code == actionCode)
{
await _orderSvc.ShipOrder(orderId);
}

return RedirectToAction("Index");
}
[HttpPost]
public async Task<IActionResult> OrderProcess(string orderId, string actionCode)
{
if (OrderProcessAction.Ship.Code == actionCode)
{
await _orderSvc.ShipOrder(orderId);
}

return RedirectToAction("Index");
}
To my question - how does it know in which cs file the function is located? I cant see any "connection" in the view. I cant imagine that it looks into every class to see wheter it has a OrderProcess and takes a POST-argument, but maybe im wrong? 🙂 Thanks in advance!
8 replies
CC#
Created by big OOF on 11/9/2022 in #help
❔ Understanding inheritance
Hello, im trying to understand a webshop example from microsoft. Im trying see what GetCardTypesAsync() does. (Attatched is the code from OrdersController)
When peeking the definition i only get to the interface "IOrderQueries" used by the OrdersController. I find the original function in another cs file "OrderQueries" (by searching for the function name) which inherits the interface but i really cant understand the connection between Orderqueries, the interface IOrderQueries and OrdersController. How does GetCardTypesAsync() in OrdersController know to use the function declared in OrderQueries just by using a interface? Anyone see what i dont? Let me know if you need additional info 🙂 Thanks in advance!
20 replies
CC#
Created by big OOF on 10/6/2022 in #help
Create a app
Hi, Me and a friend wanted to try and build a app for fun. The first problem we faced was regarding choice of tech. We are both beginners, im more into backend with C# and hes a frontend developer. This is that we have come up with: Hosting service Azure/AWS Database MongoDB? MySQL? DBAAS on Azure or locally or have it on a separate server? Frontend React native Backend C# or node Is this a good start or is there something vital that we have missed/forgotten? Happy for any input 🙂 Thanks in advance!
20 replies
CC#
Created by big OOF on 10/5/2022 in #help
Yield return
6 replies
CC#
Created by big OOF on 10/5/2022 in #help
Declare method using T and where [Answered]
This is some code from a reddit post i saw and there are some thing i dont understand. This is the method:
public static T CreateStudentFromEntity<T>(Student entity) where T : IStudent
{
var student = Activator.CreateInstance<T>();

student.Id = entity.Id;
student.FirstName = entity.Name;
student.Surname = entity.Surname;
student.DateOfBirth = DateTime.Parse(entity.DateOfBirth);
student.Sex = (Sex)entity.Sex;
student.Address = CreateAddressFromEntity(entity.Address);

return student;
}
public static T CreateStudentFromEntity<T>(Student entity) where T : IStudent
{
var student = Activator.CreateInstance<T>();

student.Id = entity.Id;
student.FirstName = entity.Name;
student.Surname = entity.Surname;
student.DateOfBirth = DateTime.Parse(entity.DateOfBirth);
student.Sex = (Sex)entity.Sex;
student.Address = CreateAddressFromEntity(entity.Address);

return student;
}
This is the interface that the method uses:
public interface IStudent
{
long Id { get; set; }
string? FirstName { get; set; }
string? Surname { get; set; }
DateTime DateOfBirth { get; set; }
Sex Sex { get; set; }
string? TelephoneNumber { get; set; }
Address? Address { get; set; }
}
public interface IStudent
{
long Id { get; set; }
string? FirstName { get; set; }
string? Surname { get; set; }
DateTime DateOfBirth { get; set; }
Sex Sex { get; set; }
string? TelephoneNumber { get; set; }
Address? Address { get; set; }
}
From what i understand, T is used to be able to make the method more "flixble", not restricting it to a specific return type. But when adding "where T : IStudent" - it restricts it to only return a object that inherits the IStudent interface? Could you not just replace T with IStudent and skip the where? Plase let me know if i got in wrong 🙂 Thanks in advance!
38 replies
CC#
Created by big OOF on 9/12/2022 in #help
Call public method
Hello, Im trying to create a "config" which retrieves values from the appsettings.json file. Ive created an example below:
public class ConfigModel : PageModel
{
private IConfiguration _configuration;
public ConfigModel(IConfiguration iconfig)
{
_configuration = iconfig;
}

public string UserName
{
get
{
return _configuration.GetValue<string>("Data:UserName");
}
set { }
}
public class ConfigModel : PageModel
{
private IConfiguration _configuration;
public ConfigModel(IConfiguration iconfig)
{
_configuration = iconfig;
}

public string UserName
{
get
{
return _configuration.GetValue<string>("Data:UserName");
}
set { }
}
How would you retrieve "UserName" from a different class? Something like?
public class TestClass
{
private static IConfiguration _configuration;
public static ConfigModel Config = new ConfigModel(_configuration);
public void ExampleLogin()
{
var a = Config.UserName;
var b = Config.Password;


}
}
public class TestClass
{
private static IConfiguration _configuration;
public static ConfigModel Config = new ConfigModel(_configuration);
public void ExampleLogin()
{
var a = Config.UserName;
var b = Config.Password;


}
}
Or what would you say is best practice? Thanks in advance! 🙂
3 replies
CC#
Created by big OOF on 9/3/2022 in #help
Api question
Hi, I want to create a API that return data from another API, my question is regarding how to structure the code. You have the API controller with endpoints that gets called and returns a value. Where should i place the function that makes the call towards the second api? 1. It dosent sound too good to have it inside the api controller? 2. Should i create another class that hold all functions needed? 3. Add another class library project? 4. Include methods in the model classes? Thanks in advance 🙂
15 replies
CC#
Created by big OOF on 9/2/2022 in #help
Display base64 img on Razor page
Hello, Im trying to display a base 64 image on a razor page. Pagemodel:
public string imagedata { get; set; } = "";
public void OnPostGetSelectedImage()
{
string myBase64 = "examplebase64";

imagedata = string.Format($"data:image/png;base64", myBase64);
}

public string imagedata { get; set; } = "";
public void OnPostGetSelectedImage()
{
string myBase64 = "examplebase64";

imagedata = string.Format($"data:image/png;base64", myBase64);
}

Page:
<div class="container">
<div class="form-group">
<img id="UserImage" src="@Model.imagedata" alt="your image" />
</div>
<div class="container">
<div class="form-group">
<img id="UserImage" src="@Model.imagedata" alt="your image" />
</div>
The image is not showing and im woundering if this is the correct way of thinking or should i change it from void to something that reloads the page?
Thanks in advance! 🙂
9 replies
CC#
Created by big OOF on 8/25/2022 in #help
Exception vs Logic
Hello, im going to read a file with File.Readlines(). I read that its better to use logic(like a if-statement) rather then try/catch. Which of these two would you say is better practice? Option 1. try{ fileLines = File.ReadAllLines(fileName); }catch { throw; } Option 2. if (File.Exists(fileName)) { try { fileLines = File.ReadLines(fileName); } catch { throw; } }else { throw new FileNotFoundException(); } Im thinking option 1 will throw filenotfoundexception either way, and therefore its no reason to check it like in option 2? Thanks in advance!
76 replies