Home > Back-end >  Good Architecture in Simple C# Console Application
Good Architecture in Simple C# Console Application

Time:07-28

I'm working on a simple git repo with some C# Console Application Projects that are usually requested in job interviews that I've experienced since I started working with .NET Development.

My issue here is that I have some trouble using good architecture practices. Thanks to that I always end up spending hours trying to picture what would be best in folder and file placements within my solutions.

For example:

job-interview-tests repo folder structure

This is the structure for that repo.

My first project in this job-interview-tests repository is a console application that checks for prime numbers, and after implementing the logic I noticed that I'd like to create some class that would handle the console texts (e.g.: Console.WriteLine()) for title, error and other things.

This is my Program.cs file for the PrimeNumber folder.

using System;

namespace PrimeNumber
{
    class Program
    {
        static void Main(string[] args)
        {            
            try
            {
                Console.WriteLine("\nBem-vindo ao código de Testagem de Números Primos!");
                Console.WriteLine("--------------------------------------------------");
    
                Console.Write("\nInsira um número inteiro natural: ");
                var number = Convert.ToUInt32(Console.ReadLine());

                var result = CheckPrimeNumber(number) ? $"{number} é um número primo" : $"{number} não é um número primo";

                Console.WriteLine("\n"   result);
            }
            catch (FormatException)
            {
                Console.WriteLine("Opa! O valor inserido não é válido");
                //throw;
            }
            catch(OverflowException)
            {
                Console.WriteLine("Opa! Você inseriu um número negativo ou um número muito grande");
                //throw;
            }
            catch(Exception)
            {
                throw;
            }
        }

        public static bool CheckPrimeNumber (uint number)
        {
            bool isZeroOrOne = false, isPrime = false;

            switch (number)
            {
                case 0: 
                case 1:
                    isZeroOrOne = true;
                    isPrime = false; 
                    break;
            }    
                
            if (!isZeroOrOne)
            {
                for (int i = 2; i < number; i  )
                {                
                    if(number % i == 0)
                    {
                        isPrime = false;
                        break;
                    }
                }
            }

            return isPrime;
        }
    }
}

Since the subsequent console application projects will use Console.WriteLine() commands in order to communicate to the user as well, I want to create a class that adapt these to something like TextTools.Title(titleName), TextTools.Error(errorMessage) and so on.

Would this class be placed in a different folder (e.g.: TextTools)? Would it be a static class? Or maybe an abstract one? I'm not really sure about what would be the best way to do this.

I really would like to have some light shed on those kind of doubts so I can start making progress on my own small projects, so thank you in advance if you can leave any piece of advise!

CodePudding user response:

A part for my answer is from the book enter image description here

Visual Studio IDE will allow you create a Solution thats is a set of projects. A project can be of any kind: console, library, winform or WPF etc...

Your project should follow the enter image description here

CodePudding user response:

if you are going to have a few projects than its a good idea to create also a dedicated project that wraps the Console and than reference it from your other projects.

a better way would be if you are using .net core is to use the ILogger [https://docs.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-6.0][1]

  • Related