Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Best way to store methods in a database?

Best way to store methods in a database?

Scheduled Pinned Locked Moved C#
databasesql-serversysadminquestion
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnold_w
    wrote on last edited by
    #1

    Let's assume we have methods that calculates something:

        public int calculateSomething1(int param)
        {
            int calculatedValue;
            ...Calculations...
            return calculatedValue;
        }
    
        public int calculateSomething2(int param)
        {
            int calculatedValue;
            ...Calculations...
            return calculatedValue;
        }
    

    What would be the best way to store these in a database (we can assume SQL Server)? Would it be to put the methods in separate dll:s and then insert all the bytes inside the dll into the database and then load them as dynamic dll:s in RAM? Or something else? Inserting the raw source code as varchar and then compile it in runtime is too slow.

    L Richard DeemingR 3 Replies Last reply
    0
    • A arnold_w

      Let's assume we have methods that calculates something:

          public int calculateSomething1(int param)
          {
              int calculatedValue;
              ...Calculations...
              return calculatedValue;
          }
      
          public int calculateSomething2(int param)
          {
              int calculatedValue;
              ...Calculations...
              return calculatedValue;
          }
      

      What would be the best way to store these in a database (we can assume SQL Server)? Would it be to put the methods in separate dll:s and then insert all the bytes inside the dll into the database and then load them as dynamic dll:s in RAM? Or something else? Inserting the raw source code as varchar and then compile it in runtime is too slow.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What exactly are you trying to do? Code should go in your program not the database.

      A K 2 Replies Last reply
      0
      • A arnold_w

        Let's assume we have methods that calculates something:

            public int calculateSomething1(int param)
            {
                int calculatedValue;
                ...Calculations...
                return calculatedValue;
            }
        
            public int calculateSomething2(int param)
            {
                int calculatedValue;
                ...Calculations...
                return calculatedValue;
            }
        

        What would be the best way to store these in a database (we can assume SQL Server)? Would it be to put the methods in separate dll:s and then insert all the bytes inside the dll into the database and then load them as dynamic dll:s in RAM? Or something else? Inserting the raw source code as varchar and then compile it in runtime is too slow.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        arnold_w wrote:

        Inserting the raw source code as varchar and then compile it in runtime is too slow.

        Compile it on first execution (or save), save the resulting assembly in a database-blob. Could easily be timestamped, to see if the source has been altered. Do consider that sometimes compilations fail due to errors in code.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

        1 Reply Last reply
        0
        • L Lost User

          What exactly are you trying to do? Code should go in your program not the database.

          A Offline
          A Offline
          arnold_w
          wrote on last edited by
          #4

          It's for register maps and register value loop-up for different products and it would be nice if only the database (not the .exe-file) needs to be updated as we add new products and registers. For example, let's assume a register called MONTH, this register would need this table to convert from an integer to a user-friendly string: 1 January 2 February 3 March 4 April 5 May 6 June 7 July 8 August 9 September 10 October 11 November 12 December Now, let's a assume a different register called YEAR_MONTH_AND_DATE, all stored within the same integer, image how big that lookup table would be. Instead, it would be much easier to just a have method to do the conversion:

          public string convertYearMonthAndDate(UInt32 yearMonthAndDate)
          {
          UInt16 year = (UInt16)(yearMonthAndDate >> 16);
          byte month = (byte) (yearMonthAndDate >> 8);
          byte date = (byte) (yearMonthAndDate >> 0);
          string monthString = "";
          switch (month)
          {
          case 1: monthString = "January"; break;
          case 2: monthString = "February"; break;
          case 3: monthString = "March"; break;
          case 4: monthString = "April"; break;
          case 5: monthString = "May"; break;
          case 6: monthString = "June"; break;
          case 7: monthString = "July"; break;
          case 8: monthString = "August"; break;
          case 9: monthString = "September"; break;
          case 10: monthString = "October"; break;
          case 11: monthString = "November"; break;
          case 12: monthString = "December"; break;
          }
          return monthString + " " + date + ", " + year;
          }

          L 1 Reply Last reply
          0
          • A arnold_w

            It's for register maps and register value loop-up for different products and it would be nice if only the database (not the .exe-file) needs to be updated as we add new products and registers. For example, let's assume a register called MONTH, this register would need this table to convert from an integer to a user-friendly string: 1 January 2 February 3 March 4 April 5 May 6 June 7 July 8 August 9 September 10 October 11 November 12 December Now, let's a assume a different register called YEAR_MONTH_AND_DATE, all stored within the same integer, image how big that lookup table would be. Instead, it would be much easier to just a have method to do the conversion:

            public string convertYearMonthAndDate(UInt32 yearMonthAndDate)
            {
            UInt16 year = (UInt16)(yearMonthAndDate >> 16);
            byte month = (byte) (yearMonthAndDate >> 8);
            byte date = (byte) (yearMonthAndDate >> 0);
            string monthString = "";
            switch (month)
            {
            case 1: monthString = "January"; break;
            case 2: monthString = "February"; break;
            case 3: monthString = "March"; break;
            case 4: monthString = "April"; break;
            case 5: monthString = "May"; break;
            case 6: monthString = "June"; break;
            case 7: monthString = "July"; break;
            case 8: monthString = "August"; break;
            case 9: monthString = "September"; break;
            case 10: monthString = "October"; break;
            case 11: monthString = "November"; break;
            case 12: monthString = "December"; break;
            }
            return monthString + " " + date + ", " + year;
            }

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Both of those methods look broadly similar but redundant. If you store dates as proper date types in the database then you can use the standard .NET classes to get them printed in clear text.

            A 1 Reply Last reply
            0
            • L Lost User

              Both of those methods look broadly similar but redundant. If you store dates as proper date types in the database then you can use the standard .NET classes to get them printed in clear text.

              A Offline
              A Offline
              arnold_w
              wrote on last edited by
              #6

              It was just an example and I choose a simple example to make it easy to understand. If you really want to look at what the registers look like in real life then have a look in this reference manual: https://www.st.com/resource/en/reference_manual/dm00031020-stm32f405-415-stm32f407-417-stm32f427-437-and-stm32f429-439-advanced-arm-based-32-bit-mcus-stmicroelectronics.pdf[^]

              1 Reply Last reply
              0
              • L Lost User

                What exactly are you trying to do? Code should go in your program not the database.

                K Offline
                K Offline
                kalberts
                wrote on last edited by
                #7

                About 40 years ago, when I was still a student, we had this database system where you could define triggers in the database for doing all sorts of integrity checks etc. on the database, independently of the application. This was a realational database, but using an extended variant of Pascal to address the tables: What would be WHERE criteria in SQL was indexing expressions in the database tables, etc. You didn't adrress the database as an external entity, but rather declared you application as an entity in the database: The databse tables were defined in a layer conceptually "outside" the program entity, encompassing it. Tables were defined using the same syntax as Pascal arrays, but being "outside" the program, they had a lifetime scope beyond the execution of the program; they looked just like other arrays, but were persistent, and had extended indexing facilities and other table operations. This compiler was based on the classical P4 Pascal compiler. So when you compiled a database routine, the P4 code was stored in the database, available to any program using this database as an outer shell. The routines were instruction set and OS independent; the database (with its embedded routines) could be accessed from any OS or machine architecture that offered a P4 interpreter. I believe that this project started around 1975-76, so it was quite early for this kind of architecture.

                L 1 Reply Last reply
                0
                • K kalberts

                  About 40 years ago, when I was still a student, we had this database system where you could define triggers in the database for doing all sorts of integrity checks etc. on the database, independently of the application. This was a realational database, but using an extended variant of Pascal to address the tables: What would be WHERE criteria in SQL was indexing expressions in the database tables, etc. You didn't adrress the database as an external entity, but rather declared you application as an entity in the database: The databse tables were defined in a layer conceptually "outside" the program entity, encompassing it. Tables were defined using the same syntax as Pascal arrays, but being "outside" the program, they had a lifetime scope beyond the execution of the program; they looked just like other arrays, but were persistent, and had extended indexing facilities and other table operations. This compiler was based on the classical P4 Pascal compiler. So when you compiled a database routine, the P4 code was stored in the database, available to any program using this database as an outer shell. The routines were instruction set and OS independent; the database (with its embedded routines) could be accessed from any OS or machine architecture that offered a P4 interpreter. I believe that this project started around 1975-76, so it was quite early for this kind of architecture.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  I have a vague recollection of someone talking about something similar to what you describe. Round about the late 1970s IIRC.

                  1 Reply Last reply
                  0
                  • A arnold_w

                    Let's assume we have methods that calculates something:

                        public int calculateSomething1(int param)
                        {
                            int calculatedValue;
                            ...Calculations...
                            return calculatedValue;
                        }
                    
                        public int calculateSomething2(int param)
                        {
                            int calculatedValue;
                            ...Calculations...
                            return calculatedValue;
                        }
                    

                    What would be the best way to store these in a database (we can assume SQL Server)? Would it be to put the methods in separate dll:s and then insert all the bytes inside the dll into the database and then load them as dynamic dll:s in RAM? Or something else? Inserting the raw source code as varchar and then compile it in runtime is too slow.

                    Richard DeemingR Offline
                    Richard DeemingR Offline
                    Richard Deeming
                    wrote on last edited by
                    #9

                    Maybe as SQLCLR functions? SQL Server Common Language Runtime Integration - ADO.NET | Microsoft Docs[^]


                    "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                    "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                    realJSOPR 1 Reply Last reply
                    0
                    • Richard DeemingR Richard Deeming

                      Maybe as SQLCLR functions? SQL Server Common Language Runtime Integration - ADO.NET | Microsoft Docs[^]


                      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #10

                      That's what I was going to suggest, but that kinda hides functionality, and if you're going to create those assemblies, why not just put them in your app? We use them where I work, but they were implemented years ago, and I'm not even sure where the source code is.

                      ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                      -----
                      You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                      -----
                      When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                      K 1 Reply Last reply
                      0
                      • realJSOPR realJSOP

                        That's what I was going to suggest, but that kinda hides functionality, and if you're going to create those assemblies, why not just put them in your app? We use them where I work, but they were implemented years ago, and I'm not even sure where the source code is.

                        ".45 ACP - because shooting twice is just silly" - JSOP, 2010
                        -----
                        You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
                        -----
                        When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013

                        K Offline
                        K Offline
                        kalberts
                        wrote on last edited by
                        #11

                        #realJSOP wrote:

                        if you're going to create those assemblies, why not just put them in your app?

                        Because there may be many apps. You may want some functions to carry out e.g. consistency checking (beyond what you can set up in the SQL schema) regardless of which application is used. Think of such code as belonging to the schema, not to the data.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups