For some time I've been considering how to approach objects that are designated identifiers through the database.

An average object representing a table entity may seem like:

public class Test
{
    public int Id { get; private set; }
    public string Something { get; set; }
}

Suppose we wish to make use of this object for placing, locating and upgrading objects within the database. For locating and upgrading we've no problems, because the Id area has something.

However, to place a brand new object of type Test in to the database, the Id area will still must have something. We're able to only use "", because it rarely is in used like a database key, however , this isn't a great design.

Likewise, when we invert the problem making the Id property nullable, we're able to use null for objects which haven't yet been designated an identifier through the database. However, it's now feasible for an item retrieved in the database not to come with an identifier (as permitted through the class design, although not the database design)

Worthwhile tips on how to create a good design with this problem?

Should you treat id in an effort to identify/grant originality to object in your application, this ought to be handled by database (unless of course obviously, you've different ways to assign identifiers to things).

When not (as with, it's object's property driven by small business) - whether 0 appliesOrgreat design value or otherwise is dependent on individuals small business purely. Is 0 valid value from say, consumer perspective?

You could wrap your object qualities into separate class, if you think that getting objects without ids set around inside your application is problematic. You will employ such class basically just for transporting parameters for not produced object (creation process is completed with database place). When the object will get placed, id designated and stuff - you are able to use your family entity. Will your user go "Oh, snap! What's that?" or "Ok.. I get sound advice.Inch once contacted by id = 0?

Edit

This (in other words my answer about wrapping parameters) brought to mind a well known fact that when surprised me. Whenever a child comes into the world, she does not appear in system until her parents officialy register her and she or he will get personal identification number designated. So technically, without id - child does not exist (a minimum of from system perspective), even tho everyone knows she was created and stuff. It is the same goes with database/your application - object without id (one which database cannot identify) does not exist - it is simply some parameters. Bit bizzare, however i hope my point is obvious :)

There's no problem with creating a category to ensure that an ID of signifies the entity has not been serialized. I've built systems previously that effectively used this method. Just make certain these semantics are very well defined inside your API, which this is respected in most from the code.

One trap to be careful for is applying the ID to define an equality relationship (such for producing hash codes for any dictionary). This must simply be accomplished for non-zero IDs. For testing equality with two IDs of zero, reference equality might be used.

However, since unstored organizations might have their ID change sooner or later later on, it is crucial that such objects will never be saved inside a Dictionary. Or, at the minimum, such products should be taken off any dictionaries before saving after which restored later on while using new ID.

With that certain reservation, this design should work fine.

public class Test : IEquatable<Test>
{ 
    /// <summary>
    /// The unique identifier for this Test entity, or zero if this
    /// Test entity has not yet been serialized to the database.
    /// </summary>
    public int Id { get; private set; } 

    public string Something { get; set; }

    public override bool Equals(object obj)
    {
        return Equals(obj as Test);
    }

    public bool Equals(Test other)
    {
        if (other == null)
            return false;
        // Distinct entities may exist with the Id value of zero.
        if (Id == 0)
            return object.ReferenceEquals(this, other);
        return Id == other.Id;
    }

    /// <summary>
    /// Gets a hash code for this Test entity. Warning: an instance with
    /// an Id of zero will change its identity when saved to the DB. Use with care.
    /// </summary>
    /// <returns>The hash code.</returns>
    public override int GetHashCode()
    {
        return Id;
    }
}