The Darkside

Shedding light on things and stuff

 
  Home :: Contact :: Syndication  :: Login
  75 Posts :: 0 Stories :: 49 Comments :: 2 Trackbacks

Ads

Archives

Post Categories

Open Source Projects

Other Blogs

Development

Mostly R&D stuff

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...


A colleague asked me this morning if I already had a code snippet that compressed and decompressed byte arrays that he could make use of. He needed it to compress a variable before passing it as an argument to a WCF call, and decompress in the service itself. I’ve never made use of the System.IO.Compression namespace (I’d always used SharpZipLib), so I thought this would be a quick and useful experiment. After referencing the local MSDN and then the customary 30 second google, I decided I’d make my own, better, wheel :) I’m still convinced it’s less verbose than...


Speculative execution is a term applied to the execution of code that you may not need the results of. With the abundance of spare processing power on servers these days (and workstations for that matter), you can easily make an application be far more responsive to the user or make processing tasks on a server application quicker.


If you find you’re getting compile errors something along the lines of error C2664: 'CreateFileW' : cannot convert parameter 1 from 'char [261]' to 'LPCWSTR' or error C2664: 'CreateProcessW' : cannot convert parameter 2 from 'char *' to 'LPWSTR' I got plenty of these when compiling code which I C&P’d, which was working perfectly in one project, to a new project I had just created; All the errors were related to converting char/char arrays to LPWSTR. There are plenty of programmatic ways of converting this data, and keep in mind you may still want to check...


I was asked by a colleague about how I might go about shuffling a deck of cards in C#. I found a few takes on this general problem on Google, and make no mistake: this has been covered by numerous persons over the last 80 years according to some of the research I found. So, yes, this is mostly likely a Wheel Version 2.0 piece of code, but here it is and hopefully it’ll be useful to someone. Firstly, you need to decide on a Random Number Generator. If you’re just interested in solving this card-shuffling problem as part...


Seeing as I have to search for this code snippet on most occasions that I use it, I’ve decided to put it here as a post. I often find that I have to repeat the same code over and over when converting between two similar types (e.g. Person domain object and PersonDto object). If you see something like this crop up too often: var personDto =  new PersonDto {     Id = person.Id,     FirstName = person.FirstName,     LastName = person.LastName,     Age = person.Age }; You have two options: 1)...


This is (another) look at implementing a dynamic proxy using Reflection Emit. Why would you need a dynamic proxy? Well, there are times when functionality needs to be injected into existing code that can not be modified. My dynamic proxy generator allow you to supply two Action parameters the will be executed as pre- and post-call methods. The tests that I have provided with my sample application show an example of injecting timing code into existing methods. It could also be used to insert log


After doing some random surfing this morning (I think I started off with binging “operator precedence C#”, I landed on this page about one line variable swaps, which then got me thinking about how to do this in C#. My only solution I could come up with was using arithmetic to solve the problem:   int x = 11; int y = 42;   y = (y + x) - (x = (y + x) - x);   Debug.Assert(x == 42); Debug.Assert(y == 11);   Please don't use this code, unless you're on some type of job security mission :)  UPDATE: 12 Aug 2009 – Ok, so I wasted more...


I’ve been making use of Roger Alsing’s Async Fork for a few months as an alternative to the “for testing purposes only” Microsoft Parallel Extensions. With the release of C# 4.0 on the horizon, I decided to modify the internal workings of the Fork class to make use of the parallel extensions, which would result in very few modifications to my existing code. My reason for the modifications are to take advantage of the inner working of parallels extensions. Here is the modified version of the Fork class:     public class Fork    ...


If you've decided to self-host a WCF service - or as in my case you already have a service in production but need it to be consumed by a Silverlight application, but keep on getting a security exception, here's a method of getting your existing services to work. It involves getting the WCF service to serve a "file" called "clientaccesspolicy.xml" when one is requested, as is done by Silverlight.


In my previous post I looked at using Castle ActiveRecord and class hierarchies & inheritance to automate some tasks related to implementing audit trails in your application. The foundation was laid to ensure some required information, namely the current user and date and time of editing data, was seamlessly integrated into the domain logic. In this post I'd like to show the next step in this process - that of implementing triggers and the tables required to store this audit trail information.


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...


This post contains a skeleton/template Windows service project that can be run as a console application when a specific command line parameters is passed through to it, or as a Windows service when installed as such. Also included in the project is a ready-to-use Windows service installer class which can be added as a custom action in an installer project. The custom action will install the service for you.


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...


I had an interesting error crop yesterday on a production server that was only happening there, and not in development and QA. I was using a semaphore to throttle the number of threads that could enter a critical section on code and on this particular machine I was getting this run-time error begin thrown by the framework: "System.MissingMethodException: Method not found: 'Boolean System.Threading.WaitHandle.WaitOne(Int32)'" After a bit of googling, the best answer I could come up with is that this method was introduced in the .Net Framework 2.0 SP2. To muddy the waters a little more, you cannot download...


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.


Seeing as duck-typing is a buzz topic and quite the rage at the moment, I thought I'd try and introduce my own spin on this, namely duck-copying. As the title suggests, it's more like a copy constructor and an extension method for copying objects of different types, but have properties that are named the same and have the same type.


I feel that one of the short-comings in C# is the lack of default copy constructors for classes and, to add to my frustration, the help provided in MSDN about this topic just doesn't cut it. I decided to implement my own "generic" copy constructor, which is by no means feature-complete, but certainly delivers what I need now. Here is an example that'll hopefully help...


I came across a problem when a virtual directoy I had created inherited the setting from my web config file in the parent directory. I was always aware of this, and had used it to great effect, however, in this case, the parent site had all sorts of http modules/handlers and page directives included, and I really didn't want to (a) copy all of the bin files across just to make it work, or (b) add a whole bunch of <remove /> tags to the various sections just to take them out. After a bit of trial-and-error typing, I came across...


This articles demostrates using your own imlementation of IEqualityComparer<> to do case insensitive camparisons for set operations in LINQ.


This is a short explanatory article on lock keyword, the Monitor object, the Mutex object and finally the Semaphore object, with some examples of how to use them.


If you're using a database in any of your unit tests (or dropping/recreating) on a regular basis, you'll inevitably get an error alongs the lines of "Cannot drop database 'Test' because it's in use". Here how to (hopefully) get rid of that problem...


A colleague had an error cropping up when calling a WCF method that took a data contract as a parameter and returned a modified data contract. We couldn't quite pin-point or blame any code that we were looking at. The error was a standard WCF error message, along the lines of "The socket connection was aborted. This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue. Local socket timeout was '00:00:5


This is just a code dump of some useful string extension methods that I make use of daily. Use it, dont use it...:)


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.


I've recently needed to test an app on Windows XP that makes use of Windows Authentication for the application as well as for the Sql connection string, with different application functionality assigned to Windows users, and the various permissions that are set in Sql. Short of having to log in and out with every different user (which wasn't going to happen), I wasn't sure how this was going to happen, until I remembered the "runsas" DOS command. Here is how to use it...


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).


Microsoft Parallel Extension to .Net Framework 3.5 is "a managed programming model for data parallelism, task parallelism, and coordination on parallel hardware unified by a common work scheduler" (MSDN). I've taken a look at some examples, some timing versus the same tasks sequentially and included my test project in this article for download.


A HttpModule that removes white space, certain javascript comments, as well as optimising ASPX post-back javascript. This is useful when trying to save on the bandwidth your blog is using, or just plain and simply trying to decrease load time of your pages.


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.


The Microsoft Property Grid control is great for displaying properties of your types, but using it for displaying your own custom property lists is slightly more tricky. Included is an example and source code for implementing your own PropertyDescriptor class for use with the control.


Hosting WCF services in IIS is a breeze, however, an interesting error along the lines of 'This collection already contains an address with scheme http. There can be at most one address per scheme in this collection. Parameter name: item ' gets thrown on IIS servers that have multiple host headers/configurations. Included in this article is code for creating you own custom service host factory that allows you to get around this.


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.


Extension methods were introduced in Visual Studio 2008 as a way of extending functionality on existing classes (included sealed classes). This is particularly useful for adding methods to existing classes that you don’t necessarily want to inherit/extend for a single method, or you can’t inherit because they’re sealed.


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


Adding your own configuration values into a config file is really simplified when it comes to using the appsettings section, but do you really need all those key-value pairs clogging up your file and not having any noticable relation to one other? Making use of a custom configuration section allows you to group the settings you want related to each other and even add validation to the key-value pairs. This is a short how-to on getting started with custom sections.


This is a utility class (with a static ValidateXML method) that will allow you to quickly validate an XML file against its XSD.


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.


Hibernate is an open source object/relational persitance framework that is widely use in Enterprise Java applications. NHibernate is a .Net port of Hibernate for Java (more specifically the Hibernate Core), and can be used to persist plain .Net objects. This article provides an introducation with a sample application to get you going.


A simple method of sending SMTP mail using C# and making use of the System.Net.Mail namespace from the Microsoft.Net Framework version 2.0.


Full Development Archive