Static class system settings in the Entity Framework

Asked

Viewed 61 times

1

I am 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 configuration for the system. So I opted for the static class.

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

Table: Configurations

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

Objective: (Example)

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

Questions:

  1. It is possible to use this structure with the entityframework 6 ? If yes, how ?
  2. Is it correct to use static class for this purpose ? Another more appropriate way to implement ?

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

I have already made this configuration as an object, and each option as a column in the database. This way, the table has only one row. I don’t see a problem with that, I’m just trying to see if it’s possible to do it that way.

Thank you

  • Do you want to use this table in your Entity Framework settings equal to the class created by Carlos' answer? type when the Entity framework goes up it looks for this information in the table and sets up the Entity Framework?

  • @Virgilionovic does not... would be system settings on the client, for example, set up environment of issuing bills, url, decimals that would be displayed on the client screen...account plan for certain movements, etc... Entity only enters the time to save these settings to the bank

  • "Entity only enters the time to save these settings in the database, "it must also enter the information recovery, so there must be your model to be used on the screens that need configuration. It cannot be a Static class in the Entity Framework, and also if it is web systems this Static class will be more disruptive than helping. Work with Entity Framework normally and recover these settings every time.

  • yes, missed the recovery part, the system is local, not web, as I had put, in the form of entity I’ve even done, the same doubt is whether it is possible to save class properties as rows and not as columns.

  • Using a Dictionary? help ???

  • never used, I will search =]

  • 1

    if it is Form you can use the Static class with Dictionary<string,string>

Show 2 more comments

2 answers

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 setting Context, in the case of decimal, setting Hasprecision:

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

  }
}

So it is not necessary to save the information in the database.

  • Thanks Carlos, but the need is to save in the database. This decimal setting is just one example, would have several other settings.

0


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

There is still the possibility to use singleton in class, but not tested.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.