System Settings in static class in the Entity Framework

1

I'm trying to create settings for a system. I thought of a class similar to the example:

Class:

public static class Configuracoes
{
     [DisplayName("Casas Decimais")]
     [Description("Informa o número de casas decimais que o sistema irá trabalhar")]
     public static int CasasDecimais {get;set;}


     [DisplayName("Opção A")]
     [Description("Determina o Valor da opção A dentro do sistema")]
     public static int OpcaoA {get;set;}

     //...
     public static int OpcaoX {get;set;}

     //...
     public static string OpcaoY {get;set;}

     //...entre outras, apenas exemplo
}

Obviously, there will only be one setup for the system. So I opted for the static class.

Now, to store this information, I would like to put a table with the following structure:

Table: Settings

      id        |       nome       |  descricao                                 | valor
"CasasDecimais" | "Casas Decimais" | "Informa o número de casas ... trabalhar"  |  2
...

Goal: (Example)

 //...
 decimal pagamento = x+y;
 Console.WriteLine("O pagamento foi de: "+ pagamento.ToString("C"+Configuracoes.CasasDecimais);

Questions:

  • Can this structure be used with entityframework 6 ? If so, how?
  • Is it correct to use static class for this purpose? Another more appropriate way to implement?
      

    Note: This is not a constant configuration as Carlos suggested, and it is necessary to save it as it may be different for each client.

         

    I've done this setting as an object, and each option as a column in the database. In this way, the table is left with only one row. I do not see a problem with that, I'm just trying to see if it's possible to do it this way.

    Thank you

        
    asked by anonymous 31.10.2017 / 02:27

    2 answers

    0

    I could not find a solution to the problem. I solved creating a configuration class normally, and declaring a static object in the application. Any configuration I need, I get this object.

    There is still the possibility of using singleton in the class, but I have not tested it.

        
    11.11.2017 / 03:49
    0

    You can create a configuration class as in the example below using constants:

    public static class Configuracoes
    {
        public const int CASAS_DECIMAIS = 2;
        public const int PRECISION = 16;
    
    }
    

    And then configuring the Context, in the case of the decimal, defining the HasPrecision:

    public class MeuContext : DbContext
    {
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
        modelBuilder.Properties<decimal>()
                .Configure(x => x.HasPrecision(Configuracoes.PRECISION, 
                  Configuracoes.CASAS_DECIMAIS));
    
      }
    }
    

    So you do not need to save the information in the database.

        
    31.10.2017 / 03:18