C
C#3mo ago
Beluga XD

Advice about file management in projects

Recently I have started trying to make my code cleaner and I started thinking about the structure of my project. I read and was following the rule that each class should be in a separate file. I applied it to interfaces, abstract classes. But what about enums, structs? They are mostly always not very big. May someone advise me an article or a book where I can read how project files should be managed please. Also, I would like to know more about the right way of splitting project files into different folders. Thank you very much for future response and your time.
6 Replies
Angius
Angius3mo ago
Enums and structs also, usually, go into their own files Far as splitting into folders goes... well, just do what makes sense for you Like, all files related to users go into /Users All files related to orders go to /Orders Alternatively, all interfaces go into /Interfaces and all classes go into /Src or some such The former is more advised
Beluga XD
Beluga XDOP3mo ago
Thank you very much for an answer. I was just thinking that placing all enums into an /Enum folder each in a separate file was a bad Idea. :catpog:
Angius
Angius3mo ago
The only real exception from "one thing per file" is records And some other very situational exceptions, like, it makes sense to have the query class nested in a CQRS handler But those are exceptions from the rule of thumb
Beluga XD
Beluga XDOP3mo ago
But what is with records? Is it a wrong technique to place each record in a separate file? They seemed to me like structs actually, that is why I ask
Angius
Angius3mo ago
Not wrong, it's perfectly fine to do so But most often they're just simple oneliners, and are used fairly locally, so it can feel like a waste For example,
public static class GetBookHandler
{
public sealed record Query(int Id);

private static async Task<BookDto> Handle(Query q)
{
//...
}

public sealed record BookDto(string Title, string Author, string ISBN);
}
public static class GetBookHandler
{
public sealed record Query(int Id);

private static async Task<BookDto> Handle(Query q)
{
//...
}

public sealed record BookDto(string Title, string Author, string ISBN);
}
Query and BookDto are only used here They both take a single line Not much point moving them to individual separate files
Beluga XD
Beluga XDOP3mo ago
I understand now what you meant. Indeed 3-4 lines per record file feels like a waste and may get project filled with them and therefore hard to navigate through. Thank you for explanation
Want results from more Discord servers?
Add your server