0

I have options to use both SQL Server and PostgresSQL databases. In some classes, I have the following attribute

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]

used to decorate to class properties:

public class MyClass 
{
    #if define PostgresSQL
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    #endif
    public DateTime SysStartTime { get; set; }
}

I have this appsettings.json file:

{
    "Database": {
       "DatabaseType": "PostgresSQL"
    }
}

How do I associate the value in appsettings.json to the value in #if define directive?

Charlieface
  • 52,284
  • 6
  • 19
  • 43
user3097695
  • 1,152
  • 2
  • 16
  • 42
  • Make a full copy of class, inherit same interface, use a switch in config, use interface instead of class – eocron Mar 16 '23 at 19:45
  • 6
    you can't, as reading the settings is done at **runtime**, whereas your compiler-switch is evaluated at **compile**-time. But I suppose you read the attribute at runtime anway, so why do you rely on a compile-time-thing in the first place? – MakePeaceGreatAgain Mar 16 '23 at 19:46
  • @MakePeaceGreatAgain I guess it may be possible to write my own attribute to include ' [DatabaseGenerated(DatabaseGeneratedOption.Computed)] if the database type is PostgresSQL. – user3097695 Mar 16 '23 at 19:50
  • Can you do the same without using annotations? If you need RDBMS agnostic classes, you can't have DB specific attributes. So I would say if you can outsource this into db specific code. – Fildor Mar 16 '23 at 20:11
  • Have two set of classes implementing same interfaces. Code the client against interfaces. – Wiktor Zychla Mar 16 '23 at 20:39

0 Answers0