Exemple #1
0
        public static void InitializeRetroDRY(bool integrationTestMode = false)
        {
            //load configuration (for this sample, we are using a separate settings file for retrodry features)
            var configBuilder = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                                .AddJsonFile("appsettings_dev.json");
            var config       = configBuilder.Build();
            var dbConnection = config["Database"];

            Globals.ConnectionString = dbConnection;

            //This will tell RetroDRY how to access your database
            DbConnection dbResolver(int databaseNumber)
            {
                var db = new Npgsql.NpgsqlConnection(dbConnection);

                db.Open();
                return(db);
            }

            //build data dictionary from annotations
            var ddict = new DataDictionary();

            ddict.AddDatonsUsingClassAnnotation(typeof(Startup).Assembly);

            //sample to override data dictionary. In a real app the overrides might come from setup tables or be hardcoded, and this process
            //could be moved to a separate class
            //ddict.DatonDefs["Customer"] = new DatonDef
            //{
            //  ...
            //};
            //ddict.DatonDefs["Customer"].MainTableDef.Prompt["de"] = "..,";

            //sample custom values (dynamic columns that are not declared in the database or at compile time)
            ddict.DatonDefs["Sale"].MainTableDef.AddCustomColum("CouponCode", typeof(string), Constants.TYPE_NSTRING);
            ddict.DatonDefs["Sale"].MainTableDef.AddCustomColum("IsRushOrder", typeof(bool?), Constants.TYPE_NBOOL).SetPrompt("", "Is Rush Order");
            ddict.DatonDefs["Sale"].MainTableDef.AddCustomColum("IsInternalSale", typeof(bool), Constants.TYPE_BOOL);

            //sample default values initializer
            ddict.DatonDefs["Customer"].Initializer = Initializers.InitializeCustomer;

            //start up RetroDRY
            ddict.FinalizeInheritance();
            Globals.Retroverse?.Dispose();
            Globals.Retroverse = new Retroverse(SqlFlavorizer.VendorKind.PostgreSQL, ddict, dbResolver, integrationTestMode: integrationTestMode)
            {
                ViewonPageSize = 50
            };

            //error reporting; In a real app you would send this to your logging destinations
            Globals.Retroverse.Diagnostics.ReportClientCallError = msg => Console.WriteLine(msg);

            //sample SQL overide
            Globals.Retroverse.OverrideSql("Customer", new CustomerSql());

            //sample exception text rewriter
            Globals.Retroverse.CleanUpSaveException = (user, ex) =>
            {
                if (ex.Message.Contains("violates foreign key constraint"))
                {
                    return("Cannot delete record because other records depend on it.");
                }
                return(ex.Message);
            };

            //only for integration testing
            if (integrationTestMode)
            {
                InitializeRetroDRYIntegrationTesting(ddict, dbResolver);
            }
        }