For a while I have been thinking about how to deal with objects which are assigned identifiers by the database.
A typical object representing a table entity may look like:
public class Test
{
public int Id { get; private set; }
public string Something { get; set; }
}
Suppose we would like to use this object for inserting, retrieving and updating objects in the database. As for retrieving and updating we have no problems, since the Id field always has a value.
However, if we want to insert a new object of type Test into the database, the Id field will still need to have a value. We could just use "0", as it is unlikely to be used as a database key, but really this is not a good design.
Likewise, if we invert the situation and make the Id property nullable, we could use null for objects which have not yet been assigned an identifier by the database. However, it is now possible for an object retrieved from the database to not have an identifier (as allowed by the class design, but not the database design)
Any good ideas on how to make a good design for this problem?