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. The Lounge
  3. Naming conventions: Object ID

Naming conventions: Object ID

Scheduled Pinned Locked Moved The Lounge
c++comarchitecturequestion
45 Posts 30 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.
  • M Marc Clifton

    peterchen wrote:

    Object.PKey

    Hey Joe, what type is that peekey again? ;P Marc

    Thyme In The Country

    People are just notoriously impossible. --DavidCrow
    There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
    People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith

    D Offline
    D Offline
    Dan Neely
    wrote on last edited by
    #35

    machined metal in yellow fluid.

    -- Rules of thumb should not be taken for the whole hand.

    1 Reply Last reply
    0
    • C Chris Maunder

      In our datebase we have a table called Object and it has a primary key ObjectID. In our code we have an object called Object and it has an ID field called Id. Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId, while part of me says this is a tautology. What do you guys use?

      cheers, Chris Maunder

      CodeProject.com : C++ MVP

      E Offline
      E Offline
      eddlsda
      wrote on last edited by
      #36

      I usually have an abstract ModelBase class that contains not only an ObjectId but a VersionId. The ObjectId is the persistent identity and the VersionId provides support in optimistic locking and support for maintaining history when needed. When these are mapped by a concrete domain model class, The ObjectId does map to a column called ObjectId which is the persistent identity of the class. If the class wants to provide a more natural alias like FooId for Foo class then defining a property that just returns the ObjectId works nicely. If an object also has a business key then that is represented and stored too. For example, it is sometimes the case that some business domains want a natural (human-readable) key to identify instances of a particular class like Product. The key for me is not to use that key as the system key as it couples different concerns and if the company decides to redo how they identify their products you’re not hosed at the database level. In terms of foreign keys, I find using the Name of the table followed by ObjectId most useful. For example, given Foo and Bar, a relationship from Bar to Foo would have FooObjectId in Bar table that is the foreign key to the ObjectId in Foo. This not only indicates that the relationship is a system key link but what type of object is related (what you need to know).

      Don Eddleman Principal Enterprise Architect Healthways

      1 Reply Last reply
      0
      • C Chris Maunder

        In our datebase we have a table called Object and it has a primary key ObjectID. In our code we have an object called Object and it has an ID field called Id. Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId, while part of me says this is a tautology. What do you guys use?

        cheers, Chris Maunder

        CodeProject.com : C++ MVP

        P Offline
        P Offline
        Paul Watson
        wrote on last edited by
        #37

        Our DBA uses ObjectID in the database and, since I am using an ORM, I refer to it as ObjectID. So yeah, I get code like Object.ObjectID which gets on my nerves. I prefer "id" in the table and then Object.id in the code.

        regards, Paul Watson Ireland & South Africa

        Shog9 wrote:

        I don't see it happening, at least not until it becomes pointless.

        1 Reply Last reply
        0
        • M Marc Clifton

          Chris Maunder wrote:

          In our datebase we have a table called Object and it has a primary key ObjectID.

          Unless there's a really good reason, I wouldn't have a table with such a generic name. But besides that, the PK would be "ID".

          Chris Maunder wrote:

          In our code we have an object called Object and it has an ID field called Id.

          That's what I would do, but again, I wouldn't call the class Object. I guess you're not planning on having a class of that name in C#, are you?

          Chris Maunder wrote:

          Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId, while part of me says this is a tautology.

          Agreed. ID is for the OK, and fooID is for FK's. Marc

          Thyme In The Country

          People are just notoriously impossible. --DavidCrow
          There's NO excuse for not commenting your code. -- John Simmons / outlaw programmer
          People who say that they will refactor their code later to make it "good" don't understand refactoring, nor the art and craft of programming. -- Josh Smith

          H Offline
          H Offline
          HellfireHD
          wrote on last edited by
          #38

          Marc Clifton wrote:

          Agreed. ID is for the OK, and fooID is for FK's.

          I agree with this. I have found it beneficial for migrating data between databases (for partitioning or archiving) since with a common name for the primary key I can then write generic scripts to perform the migration and only need to worry about dependencies. I'll often go one step farther and also ensure that any descriptive fields are consistently named within the application (in other words either object.Name or object.Title, not both) allowing every row to be presented generically, eg. for a search results page. (Yes, I'm aware that I can just override ToString() in every class, and I do, but having a consistent column name lets the code generator do that for me.)

          1 Reply Last reply
          0
          • C Chris Maunder

            In our datebase we have a table called Object and it has a primary key ObjectID. In our code we have an object called Object and it has an ID field called Id. Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId, while part of me says this is a tautology. What do you guys use?

            cheers, Chris Maunder

            CodeProject.com : C++ MVP

            F Offline
            F Offline
            Frank Wong
            wrote on last edited by
            #39

            Chris: Keep in mind that objects need an ID so as to stay unique in their application domain; otherwise, the effort is a waste. Operations on objects are basically creation, destruction, and modification. It is much easier to handle when the object is renamed outside the domain while the objectId stays the same. In my business an electronic component is called by a part number, then it is called by a reference designator in a PCB board; but it is given an objectID in a CAD file. Microsoft assigns Universal ID to objects so as to be unique for the entire world. So you have to ask how ambitious you want to promote your universe! Best Regards, Frank XrxDeveloper

            1 Reply Last reply
            0
            • C Chris Maunder

              In our datebase we have a table called Object and it has a primary key ObjectID. In our code we have an object called Object and it has an ID field called Id. Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId, while part of me says this is a tautology. What do you guys use?

              cheers, Chris Maunder

              CodeProject.com : C++ MVP

              R Offline
              R Offline
              Rocky Moore
              wrote on last edited by
              #40

              Personally, I find it easier to use the object name with the ID. Yes, it can look better from a single object standpoint to have ".Id", but when you throw in FKs you have to use two naming conventions instead of just one because all FKs will be nameId and the PK will just be Id. To me that just seems mixed. For me, it is looks more consistent to use: Member { MemberId ProfileId AddressId Name Age IsFat HowManyWives } than: Member { Id ProfileId AddressId Name Age IsFat HowManyWives } In this example, when you use the member ID in another table as a FK, to the member table is is Id but the other table it will be MemberId, just does not seem consistent. And for what, typing a few extra letters? BTW, have to admit, I had to look up "tautology" :-O Common, keep it simple :)

              Rocky <>< Latest Code Blog Post: Free grid for WPF! Time limited - act now! Latest Tech Blog Post: Scratch: fun for all ages for free!

              1 Reply Last reply
              0
              • C Chris Maunder

                In our datebase we have a table called Object and it has a primary key ObjectID. In our code we have an object called Object and it has an ID field called Id. Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId, while part of me says this is a tautology. What do you guys use?

                cheers, Chris Maunder

                CodeProject.com : C++ MVP

                C Offline
                C Offline
                cmk
                wrote on last edited by
                #41

                I used to use: DB: Object.Id Code: Object.Id Now i use (last 4-5 yrs): DB: Object.ObjectId Code: Object.ObjectId I can't remember the reason for the switch, but there was one. Aside from whatever the reason was, i find Ta.TaId == Tb.TaId gives me a warm fuzzy. The lack of symmetry in Ta.Id == Tb.TaId bothers me. As far as DB vs Code, i don't see a compelling reason to change the name going from one to the other. It also makes script gen-ing one from the other a bit of a pain.

                ...cmk Save the whales - collect the whole set

                1 Reply Last reply
                0
                • C Chris Maunder

                  In our datebase we have a table called Object and it has a primary key ObjectID. In our code we have an object called Object and it has an ID field called Id. Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId, while part of me says this is a tautology. What do you guys use?

                  cheers, Chris Maunder

                  CodeProject.com : C++ MVP

                  J Offline
                  J Offline
                  Jasmine2501
                  wrote on last edited by
                  #42

                  I generally do this application-wide. If something represents the exact same thing, then it has the exact same name. I have contact_id in my database and it's used as a primary key and foreign key, but as long as it represents the ID of a contact, that's what I call it, and I never use the name contact_id for anything else. If I add a special context to it, I keep the base, but add to the variable name (sender_contact_id, recipient_contact_id, etc.). I keep this convention in stored procs and application client code as well. Makes it kinda funny when you see: where contact_id = @contact_id ... but any SQL programmer worth a damn should understand that.

                  "Quality Software since 1983!"
                  http://www.smoothjazzy.com/ - see the "Programming" section for freeware tools and articles.

                  1 Reply Last reply
                  0
                  • C Chris Maunder

                    In our datebase we have a table called Object and it has a primary key ObjectID. In our code we have an object called Object and it has an ID field called Id. Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId, while part of me says this is a tautology. What do you guys use?

                    cheers, Chris Maunder

                    CodeProject.com : C++ MVP

                    A Offline
                    A Offline
                    andy_fcm
                    wrote on last edited by
                    #43

                    As an example, this is what I do: class Customer Customer.Id Customer.Name Customer.Address Customer.Stuff Customer.DifferentStuff class Order Order.Id Order.CustomerId Order.Quantity Order.Stuff Order.DifferentStuff So, as you can see, Id is the primary key of Customer and CustomerId is the FK of Order

                    1 Reply Last reply
                    0
                    • C Chris Maunder

                      In our datebase we have a table called Object and it has a primary key ObjectID. In our code we have an object called Object and it has an ID field called Id. Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId, while part of me says this is a tautology. What do you guys use?

                      cheers, Chris Maunder

                      CodeProject.com : C++ MVP

                      J Offline
                      J Offline
                      JohnGriffiths101
                      wrote on last edited by
                      #44

                      Chris Maunder wrote:

                      Part of me wants to stay consistent and have the object in code have an ID field Object.ObjectId

                      ---- I try to be totally consistent:- ---- [table_name].[field_name] [class_name].[field_name] Product.Id Product.Name Order.Id Order.Product_Id * foreign key becomes [table_name].[foreign_table_name]_[field_name] [class_name].[foreign_class_name]_[field_name] * Notice how . becomes _ ---- * field names Id type is unique number By type is person ie Order.CreatedBy Date type is datetime ie Order.CreatedDate ----

                      1 Reply Last reply
                      0
                      • L Leah_Garrett

                        Yeah the best way to solve naming convention issues is to ask the opinons of more developers. ;-) Having had to detangle some odd FK naming I like the consistant PK and FK naming approach. Consistant naming in layers is also very helpful. But consistancy is the key, so if someone is using ID for PK then I will use that. -- Leah

                        T Offline
                        T Offline
                        Tim Schwallie
                        wrote on last edited by
                        #45

                        I usually ask the psycho guy...what's his name...Freud.;P

                        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