The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  83 Posts :: 0 Stories :: 57 Comments :: 2 Trackbacks

Ads

 

Donate via PayPal...

...if you feel the site helped.

Archives

Post Categories

Open Source Projects

Other Blogs

ActiveRecord

Castle ActiveRecord

Wow – it seems like last year just flew by. I opened LiveWriter this morning and realised that I last posted an article in June 2011, and that I have 14 incomplete articles that I’ve never got round to completing. To kick off the year, I’m going to start off with (what I think) is an underused class in Castle ActiveRecord (actually, it resides from NHibernate) – Disjunction. The Disjunction type can be used to construct large 'OR' statements for use in ActiveRecord data retrievals. If you have used this class before, you may have noticed that...


Configuration Version 3 now supports the short-hand configuration options supplied by NHibernate. You choose the DB type, supply a connection string name, and the rest of the defaults are chosen for you.   <configSections>     <section name="activerecord" type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord"/>   </configSections>   <connectionStrings>     <add name="ARTest" connectionString="Server=(local); Database=ComplexDBTest; Integrated Security=SSPI"/>   </connectionStrings>   <activerecord>     <config db="MsSqlServer2005" csn="ARTest"/>   </activerecord> SessionScope-less Lazy Loading This is a great change...


I was busy with a(nother) comparison of Castle ActiveRecord and the Microsoft offerings that have shipped with VS2010 just to make sure that I wasn’t missing out on anything. I ended up fiddling with EDMX files, and then custom generation, which then led me to downloading the ADO.Net POCO T4 generation templates and checking out what was in the files. I have to admit: when I saw what was in it, I got quite giddy with excitement, followed shortly by feeling like a tool. I can’t believe I’ve missed out on this excellent piece of functionality that has been around...


I stumbled upon an interesting problem this morning where Castle ActiveRecord was being initialised correctly in the development environment, but as soon as everything was rolled out to the lab environment, the application would throw errors about AR not being initialised. The error message was along the lines of: An ActiveRecord class (Darkside.Domain.Person) was used but the framework seems not properly initialized. Did you forget about ActiveRecordStarter.Initialize() ? A bit of background info: As is custom in our web projects, we use an HttpModule to do the AR initialisation. In this case, the web project is actually a...


There are a few solutions out on the web for this – here’s my implementation for you to use (and for me to safe-keep). I use this primarily for testing my ActiveRecord objects, as I haven’t really found any use for this mechanism in any production environment Firstly, the configuration. Notice that I specify my own connection provider (make sure you correctly swap out the relevant portions with your own namespaces). Also, there’s a slight twist in the connection string that you may want to take note of. The rest is straight forward.   <activerecord...


If you’ve decided to download the Castle ActiveRecord 2 Alpha version and see how it affects your existing projects, you’ll most likely get an error like this: “The ProxyFactoryFactory was not configured.Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.”. This release of ActiveRecord makes use of NHibernate version 2.1.0.1003, which requires an additional entry in the configuration section to work correctly. The updated configuration section for your config file looks like this (line 13 in the example is what you’ll need to add):     1   <activerecord    ...


Very often, one of the requirements that an application I'm working on has is that it needs an audit trail. In this post I'm going to go through a technique that will build the foundation which will allow you to add record-based audit trails to your application. We'll look at adding application user information and time stamps to the data using inheritance, as well as overriding some methods in the ActiveRecord base classes to automatically populate these values.


This is just a tip for anyone using GeneratorStudio or any other code generator for their ActiveRecord classes (or any other classes, for that matter). I generally have my templates set up to generate my files with the name "<TABLENAME>.Generated.cs" or something similar. In addition to this naming convention, I also make sure that my generated classes are marked as partial classes, and then I do all custom code, like additional "Find" methods methods, etc, in files named "<TABLENAME>.cs". This way, when you need to regenerate anything, you can be comfortable with overwriting all the classes that are...


I’ve started some new development on the Generator Studio tool, and one of our big features that we felt is a real necessity is the ability to point to different database engines. With that in mind, I started off trying to get Castle ActiveRecord connecting to MySQL as part of the learning curve. Here are some pointers on getting it to work smoothly, as well as links to some depenencies you may need.


A quick look at NHibernate.Linq and getting it to work in Castle ActiveRecord. Binaries are included for NHibernate, NHibernate.Linq and Castle ActiveRecord.


When running unit tests, I often using ActiveRecordStarter.CreateSchema() in my test startups to give me a clean database to work from. Whether or not this should be done by a SQL script or a restore from a backup is for another time. On one such occasion, I started getting a 'binary or string data would be truncated' error being thrown and on closer inspection, I noticed it was because of a certain column that was originally nvarchar (max) by design, but had been recreated by ActiveRecord as nvarchar (255). A quick Google found many folks having this problem and the solutions varied...


Generator Studio is a code generation tool that allows you to set up templates using the Velocity Template Language, and then generates files based on your source database structure. It will be continually updated with new features and templates, to keep checking back if you're interested.


To expand on the title a bit: What if I have an identity column in a table, but it isn’t my primary key? Or what if I have a computed column, and need to use the value as soon as I’ve created/saved/updated?


A post on fixing a "failed to lazily initialize a collection, no session or session was closed" related error when using Castle ActiveRecord/NHibernate in a web application, using the SessionScopeWebModule and a configuration tweak.


This is a follow-up post on an article from a few months back and hopefully expands on how to overcome the error "failed to lazily initialize a collection, no session or session was closed" which I get very often in WinForms development.


Following on from my previous post here on different methods of configuring ActiveRecord, I’ve decided to go into more detail on this; more specifically into automatically loading assemblies and configuring ActiveRecord without much fuss. Included is an example project demonstrating automatically configuring your application with a single call, as well as an HttpModule that you can add to your web application that initliases ActiveRecord without a call at all.


The Question: What if I still want to use ActiveRecord, but don’t want all my objects to inherit from ActiveRecordBase or ActiveRecordBase<> because of all the functionality I already have in my base object? The Answer: ActiveRecordMediator.


Making use of the [Version] attribute in Castle ActiveRecord is a really nice way of enabling your app to detect changes in data that may have happened in other apps. Usually, you’d just save your record and it’d be in the DB. Unfortunately, you may have inadvertently overwritten someone else’s updates. In more formal terms, it is a convenient way of solving data concurrency issues. I've set up an example demonstrating (really simply) how to put this into practice.


A short whle back a colleague asked a good question: How would you reproduce a "SELECT TOP 5 * FROM [Table]" in his ActiveRecord-based project. This is a short explanatory post on how to use it, and some other uses (and issues).


Although ActiveRecord handles the majority, if not all, of your data persistence requirements, there is often a need to get access to the underlying database connection for tasks like bulk SQL copies, one-off stored procedure calls, etc. The included code snippet demonstrates getting the connection and calling a stored procedure.


A potential requirement of an application might be to retrieve lookup data from a seperate server/database. Castle ActiveRecord provides 2 mechanisms that allow the developer to connect to multiple databases, and the third option I've listed here, making use of SQL2005 synonyms, could be an easier alternative. I've included a sample project download that demonstrates the first two options in this post.


A look at some of the finer details of Castle ActiveRecord like lazy loading of collections, making use of the SessionScope and some other attributes and properties that are useful


Three different methods of configuring Castle ActiveRecord for use; One using the app.config, the second only using code and lastly making use of a seperate XML configuration file.


Castle ActiveRecord is a .Net implementation of the ActiveRecord pattern. Castle ActiveRecord makes use of NHibernate as the underlying persistance framework and this article demonstrates the simplicity that ActiveRecord introduces.