|Visual Basic (Declaration)|
Public Class ObjectContext Inherits MarshalByRefObject
|Visual Basic (Usage)||Copy Code|
public class ObjectContext : MarshalByRefObject
|Managed Extensions for C++|
public __gc class ObjectContext : public MarshalByRefObject
public ref class ObjectContext : public MarshalByRefObject
Thread Safety: Any public static members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
This class is the main point when loading, saving and deleting persistent objects to/from the storage. Whenever objects are loaded from the storage you need (don't use directly the storage) to request them by using an ObjectContext class. The class contains specialized methods (all generic) allowing the user to get single objects, a list of objects or an ObjectSet from the storage. Each object can also be marked for deletion by using the MarkForDeletion method of the ObjectContext. An object that has been marked for deletion is deleted the next time the changes are persisted to the storage.
When setting up an instance of the ObjectContext class you have to pass to the constructor an instance of a class that implements the IStorage interface. Those classes are so called "storages". They encapsulate a physical storage (like a database or anything else...) and each ObjectContext instance operates on one of those storages. The framework ships with a few storages for databases such as the OracleStorage, the MsSqlStorage and the OleDbStorage to mention any of them. When setting up the storage you have not to open it in any kind. This is done by the framework: the connections to the storage are handled by the classes internally (in the storages coming with the framework the ADO.NET connection pool is used).
Be aware that persistent objects loaded within one ObjectContext "life" in this ObjectContext. This means that the ObjectContext manages internally the state of the object (see ObjectInfo). This could cause problems when loading the persistent object with one ObjectContext and saving it on another that works on the same storage. To avoid this problem persistent objects that are loaded on one ObjectContext and saved on another have to implement the ISelfContaining interface. Persistent objects implementing this interface are ObjectContext independent and can be saved and loaded on any instance of an ObjectContext (if all of them are accessing the same storage). When implementing the interface the state is managed by the persisten object itself! The ISelfContainingObject interface is also implemented if a persistent object is remoted over the network or persisted (serialized) to any kind of storage for further use.
Before using an ObjectContext you should also connect an instance of a class that implements the IConcurrencyManager interface with the class by setting the ConcurrencyManager property of the ObjectContext. If the program works on a storage that is locally installed and if it is the only program working on that storage you could use no IConcurrencyManager. But it's not recommended! A concurrency manager checks if the persistent object to save has been changed by anybody else in the meantime (while being in memory). If somebody else changed the object in the storage you get a ConcurrencyException. With this excpetion you can inform the user that somebody else altered the object in the meantime. The object could then be reloaded or saved in any other place.
To save the changes of your objects you have to use PersistChanges of the ObjectContext. The method allows you to persist single objects or objects that implement the ICustomPersister interface. This interface is implemented by classes that persist more then one object. For an example look at the ObjectSet class.
If the class implementing the IStorage interface implements also the ITransactionStorage interface the ObjectContext supports also Transactions on that storage. Use StartTransaction, Rollback and Commit for transaction management. Please don't use directly the methods exposed by the storage.
The ObjectContext returns an ObjectSearcher when invoking GetObjectSearcher. You could use this class to specify your search queries for a given object type. For more information about creating your own ObjectSearcher check out the ObjectSearcher class.
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family
Assembly: Chili.Opf3 (in Chili.Opf3.dll)