Database class has a lot of overloading for retrieving single entities or a list of all the entities of a given type, the entities themselves or just Lazy objects
, in a reusable weak-typed or in a strong-typed way using generics.
But probably, the most flexible way of retrieving some entities is using the integration of Database Linq queries
with the retriever.
Finally, we will see how the Retriever actually works, just in case you are interested.
public static IdentifiableEntity Retrieve(Type type, int id)
public static T Retrieve<T>(int id) where T : IdentifiableEntity
public static IdentifiableEntity Retrieve(Lazy lazy)
public static T Retrieve<T>(this Lazy<T> lazy) where T : class, IIdentifiable
public static IdentifiableEntity RetrieveAndForget(Lazy lazy)
public static T RetrieveAndForget<T>(this Lazy<T> lazy) where T : class, IIdentifiable
public static List<IdentifiableEntity> RetrieveAll(Type type)
public static List<T> RetrieveAll<T>()
public static List<Lazy> RetrieveAllLazy(Type type)
public static List<Lazy<T>> RetrieveAllLazy<T>()
public static List<IdentifiableEntity> RetrieveList(Type type, List<int> ids)
public static List<T> RetrieveList<T>(List<int> ids)
public static List<Lazy> RetrieveListLazy(Type type, List<int> ids)
public static List<Lazy<T>> RetrieveListLazy<T>(List<int> ids)
Just after an entity is Retrieved, PostRetrieving method is called on it, so you can run whatever code you want. i.e. Assigning calculated values that are not stored in the database.
Note: Don't expect any of these methods to return null when the entity is not found. Instead they will throw an exception. If you want to test for the existance of an entity use Database.Exist method (weak and strongly typed). Know the reasons
Preserving Object Identity
, identity of rows is kept by the table they belong, and the almost-mandatory Identity column. If you have an identity column (and Signum Framework has one on every entity) there's no duplication of rows and finding a row is easy.
In object oriented languages, on the other hand, identity of objects is kept by the actual position on the heap they are placed. Two objects could be equal, but they are the same only if they are physically located in the same place in the heap. In C#, identity equality is tested using == operator or, if someone has overloaded it,
To integrate the two worlds, its necessary to have a data structure that allows RDBMS identity behaviour in some scope of your code. Signum Engines solves that using ObjectCache class