Contents tagged with powershell
As I mentioned in a previous post, Jim referred to on Twitter and officially named it, we’ve been tinkering with the idea of using Powershell and NHibernate together to move entity instances between persistence mechanisms. I’m going to cover how one can accomplish this functionality using Domain Console in this post. If you prefer just looking at PS1 files, a code download is available at the end of the post.
Inspired by my buddy Jim’s article series on the topic of creating Powershell drives, I had lunch with him to find out more about the complexities in doing so. Inspired by my Enterprise Library-like wrapper around NHiberate, he wanted to discuss with me an idea he had for a new drive provider that would universally access databases. The more we talked (and drank margaritas) the more the idea percolated as one with merit.
With Jim’s excellent article series as a starting point I created such a drive around “the knocks,” an NHibernate Quick Start library I created (that may be discussed later in more detail). The result appears to be something I think could be quite nifty – a command-line provider interface for any database platform supported by NHibernate.
Here’s a preview. The following cmdlet does a few things, which will be highlighted below. Imagine this as script you’d enter at a Powershell command line.
- Loads in the SQLite assembly, as it’ll be used as a database platform
- Sets up a drive. The parameters passed to the drive can be summarized, high level, as:
- domainAssembly – where the domain entities live
- dataAccessAssembly – where the implementations of the ISessionFactoryService and ISessionFactoryRequestService interfaces live
- requestServiceType – Implementor of ISessionFactoryRequestService
- sessionFactoryType – Implementor of ISessionFactoryService
- The database is then queried for a particular type of domain entity – a Geek object instance
When this script is executed and the database-creation process executes, the SQL is shown to demonstrate the mapping has succeeded.
Then, due to NHibernate’s convenient logging features, you’ll see more SQL output in the console window reflecting the inserts and selects being performed against the SQLite database. This gives a glimpse into how the data is added to the database.
Lastly, a shot of just when the Get-Item part of the script is called.
Once the database has been wrapped, the final call can be made, to Get-Item.
The output of this results with the objects our debug-time database-saving had completed so test data would exist. When the output streams into the console it’s obvious – we’re querying a database-agnostic NH repository library directly using LINQ at the Powershell command line.
The first shot is of the SQL getting executed directly into the SQLite database via NHibernate.
The second is how the objects look – and how they can be interacted with – at the Powershell command line. They’re instances, object-relationally mapped directly from a database, fresh for copying (or migrating and then copying) into an RDMBS living somewhere completely different.
Hopefully we’ll have some more information on this coming up and a lot more refinement. For now though, would you think universal RDBMS access via object-specification and retrieval via generic selection would be useful?