May
13
Written by:
Peter Wiles
Thursday, May 13, 2010 5:36 PM
One thing we've regularly needed to do is to load and save objects to multiple data sources. One example where this might apply is when your security objects (users, roles etc) are stored in a separate database to your application database. Another example is where you have application data and user preference data - the application data being stored to a centralised database and the user preferences being stored to a local xml file, and you would like a unified interface where you simply save objects and have them go wherever they should.
In Habanero 2.3.2 this can be achieved with a DataAccessorMultiSource. Here is an example:
I've configured two database connections in my app.config (or web.config):
...
...
<configSections>
<section name="DatabaseConfig" type="System.Configuration.DictionarySectionHandler" />
<section name="LogonDatabaseConfig" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<DatabaseConfig>
<add key="vendor" value="SqlServer" />
<add key="server" value="localhost" />
<add key="port" value="" />
<add key="database" value="my_app_database" />
<add key="username" value="sa" />
<add key="password" value="sa" />
</DatabaseConfig>
<LoginDatabaseConfig>
<add key="vendor" value="SqlServer" />
<add key="server" value="localhost" />
<add key="port" value="" />
<add key="database" value="my_security_database" />
<add key="username" value="sa" />
<add key="password" value="sa" />
</LoginDatabaseConfig>
...
...
Now in my startup code, after the call to the HabaneroApp.Startup(), I can add the extra DataAccessor and set it up for type User as follows:
// create the login database connection and data accessor.
DatabaseConfig loginDatabaseConfig = new DatabaseConfig(
(IDictionary)ConfigurationManager.GetSection("LoginDatabaseConfig"));
IDatabaseConnection loginDatabaseConnection =
loginDatabaseConfig.GetDatabaseConnection();
IDataAccessor securityDataAccessor =
new DataAccessorDB(loginDatabaseConnection);
// create the multisource dataaccessor, configuring the current BORegistry.DataAccessor
// as the default dataaccessor (this will be using my_app_database)
DataAccessorMultiSource multiSourceDataAccessor = new
DataAccessorMultiSource(BORegistry.DataAccessor);
// tell the multisource dataaccessor to use the securityDataAccessor when
// loading and persisting objects of type User
multiSourceDataAccessor.AddDataAccessor(typeof(User), securityDataAccessor);
// set the application's dataacessor to the multi source one.
BORegistry.DataAccessor = multiSourceDataAccessor;
Now all objects will be loaded and persisted to my_app_database except for User objects, which will be loaded from and persisted to my_security_database.
1 comment(s) so far...
Re: Habanero 2.3.2: Easily persisting to and from multiple datasources
Thanks Pete This is a simple elegant solution and in the true test of solutions solves a number of other problems that I guess it was not origionally envisioned to solve. E.g. What if I need a really custom loading mechanism for a single Business Object e.g. My User is being loaded from a Stored Procedure and not via the Habanero ORM well with this solutions you can easily create a DataAccessor for the User that is customized for that user. With this you would then merely register the Totally Custom Data Source with your MultiDataSource and your done.
Awesome.
By brettright on
Friday, May 14, 2010 9:22 AM
|