About design, code and how to make them better.

воскресенье, 11 апреля 2010 г.

My vision of Storage API

Without boring preface I’m going to describe Storage API I would like to use, that imaginary storage has just two basic types end user has to deal with: Storage and Entity. One can consider Storage as a big Collection of Entities. For example, one can add or remove Entities from a Storage almost the same way as  from ArrayList. There are many different storages: SqlServerStorage, OracleStorage, InMemoryStorage, OfflineStorage. Different storage have different policy for transactions. For example, OracleStorage requires all operations for reading and writing data to be performed in transactions whereas offline storage does not. Let’s take a look at samples illustrating basic functionality:
Example 1 : Using SqlServerStorage
var storage = new SqlServerStorage {
ConnectionString=”Server=(local);DataBase=MyDB;”};

using (var t=storage.BeginTransaction()) {
var person = storage.Get<Person>(5);
storage.Remove(person);
t.Commit();
}


Example 2 : Queries


var storage = new SqlServerStorage {

ConnectionString=”Server=(local);DataBase=MyDB;”};
using (var t= storage.BeginTransaction()) {
var person = storage.Query<Person>().Where(p=>p.ID=5);
storage.Remove(person);
t.Commit();}


These samples work almost the same way as DataObjects usually do, just syntax is a bit different. Offline storage is more interesting. Offline storage deals with object the same way as Svn works with files. One can get objects which are in consistent state with each other but the whole storage state is not guaranteed to be in a consistent state. For example, that is how objects can be loaded


Example 3 :  Using OfflineStorage


var storage = new SqlServerStorage {
ConnectionString=”Server=(local);DataBase=MyDB;”};
var offlineStorage = new OfflineStorage(storage);
using (offlineStorage.BeginTransaction()) { // Real transaction was created
var person = offlineStorage.Get<Person>(4);
var book = offlineStorage.Get<Book>(5);
}
var person = offlineStorage.Get<Person>(6);


Person #4 and Book #5 are guaranteed to be in a consistent state but Person #4 and Person #6 are not. Offline storage never updates real data, one should call “Commit” manually.


Example 4: OfflineStorage changes


var storage = new SqlServerStorage {
ConnectionString=”Server=(local);DataBase=MyDB;”};
var offlineStorage = new OfflineStorage(storage);
var person = offlineStorage.Get<Person>(6);
person.Name = “Alex”;
offlineStorage.SaveChanges();


Of course, SaveChanges call leads to to creation of new Transaction at real storage.


Another interesting moment is transferring data over network. It is achieved using special storage called StorageProxy. There is an example of using StorageProxy:


var storage = new StorageProxy(“tcp:/myServer.com:5040”);
using (storage.BeginTransaction()) {
var p = storage.Get<Person>(5);
storage.Remove(p);}


So one can make n-tier application just plugging proxies one to another. I could continue examples, but for one post it seems to enough.

1 коммент.:

  1. It seems, at least for me, that this could be mostly a n-tier Storage API rather than ORM API. Personally, I don't see any ORM-related tasks in the examples.

    ОтветитьУдалить