June 7, 2010

Effortlessly map domain and DTO entities

Filed under: Development — Tags: , — Darrin Maidlow @ 10:19 pm

I recently created a set of objects to complement the domain entities we use for our data access layer for a new project we’re working on.  I immediately set out to write a mapping utility.  After a bit of thought pondering the complexity of the problem I decided to look around on the magical internets to see what options were available.

Came across .   This is a perfect match for my needs:

AutoMapper uses a fluent configuration API to define an object-object mapping strategy.   AutoMapper uses a convention-based matching algorithm to match up source to destination values. Currently, AutoMapper is geared towards model projection scenarios to flatten complex object models to DTOs and other simple objects, whose design is better suited for serialization, communication, messaging, or simply an anti-corruption layer between the domain and application layer.

It was quick to setup, and painless to use.  First define your object to object mappings:

   1: Mapper.CreateMap<RADE.BO.Domain.Application, RADE.BO.Entity.Application>();

 
Next, whip up a small conversion function:
   1: /// <summary>
   2: /// Convert a DTOApplication to abnd 
   3: /// </summary>
   4: /// <param name="app"></param>
   5: /// <returns></returns>
   6: public static RADE.BO.Domain.Application Convert(Application app)
   7: {
   8:   if (app == null) return null;
   9:   LoadDtoMappings();
  10:   return AutoMapper.Mapper.Map<Application, RADE.BO.Domain.Application>(app);
  11: }
  12:  
  13: /// <summary>
  14: /// Convert a list of domain Application objects to DTO Application objects
  15: /// </summary>
  16: /// <param name="apps">List of domain Application objects</param>
  17: /// <returns>List of DTO Application objects</returns>
  18: public static List<Application> Convert(List<RADE.BO.Domain.Application> apps)
  19: {
  20:     LoadDtoMappings();
  21:     return apps.Select(AutoMapper.Mapper.Map<RADE.BO.Domain.Application, Application>).ToList();
  22: }

It’s important to note that if your source object contains nested objects – a mapping for each nested object must also be defined.  This goes on recursively through nested objects.  My domain Application object contains some numerous sub objects – Map, Layer etc.  In this case rather than establish mappings for these child objects – I removed these objects from my DTO as the client consuming these objects would never need that data.  This also keeps the size of any data possibly being serialized down.   Automapper dealt with this automatically.

I’ve only scratched the surface of what Automapper can do.  Next step is to define unit test coverage using the , but it’s sleep time now… =)

April 15, 2010

Deployment project crashes on execution with .NET version error

Filed under: Development — Tags: — Darrin Maidlow @ 2:47 pm

This week I jumped on the bandwagon.  I had a proof of concept project that I wanted to bring up to the “real product” level.   This new product consists of a console application, a windows service, and some business logic and data access assemblies.  During the development of my proof I created a small deployment project to build a quick and dirty installer.  

I’ve upgraded the solution to 2010.  I’ve done all the refactoring and finished my work.  Everything tests out great in both unit tests and “developers computer” tests.  Just need to get the installer updated and get the pesky “other peoples computers” thing out of the way. 

I’ve updated the pre-requisites on my installer to include .NET 4.0 – rebuilt the installer and boom. 

Error 1001.  Exception occurred while initializing the installation.  System.BadImageFormatException: Could not load file or assembly ‘file:///C:\Program Files\Landor\ … \RADE.Connect.Service.exe’ or one of its dependencies.  This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded.

After much mucking around I found the hidden property that I needed to change.  I needed to change the .NET version of the .NET Launch Condition.  To find this gem right click on the deployment project in the solution explorer and choose View\Launch Conditions.  Next right click on the “.NET Framework” under “Launch Conditions” and choose “properties”.  In the properties dialog change the Version to be “.NET Framework 4”.

Update 4/20/2010

Don’t forget to edit the .NET version for both Release and Debug configurations.

April 12, 2010

Visual Studio 2010 and .NET 4.0 Full available on MSDN!

Filed under: Development — Tags: , — Darrin Maidlow @ 10:06 am

MSDN has been updated with the full releases of VS 2010 and .NET 4.0.  Getting some decent download speeds too..

http://msdn.microsoft.com

wooo!

Visual Studio 2010, .NET 4.0, AND ReSharper 5.0 launch today!

Filed under: Development — Tags: , , — Darrin Maidlow @ 8:32 am

I may be weird, but I actually went to bed excited last night.  VS 2010, .NET 4.0, AND are supposed to be released today.   For the first time in probably months I got out of bed at 7am -  It’s now 7:30 Redmond time – but MSDN has not yet been updated!  Apparently we have to wait until 8:30 Redmond time.   Adobe is all over their release of CS5 today, and they didn’t make their users wait until 8:30 Redmond time! =)

In the meantime, The Register has a good write up on some of the coming in an hour or so.  That will learn me for getting up early.  Oh well, time for another !

April 9, 2010

NHibernate 2.1 throws System.InvalidCastException on Oracle 10g R1 client

Filed under: Development — Tags: , — Darrin Maidlow @ 3:52 pm

When deploying an early build of an up and coming product on a customers machine I came across the following error:

System.InvalidCastException: Unable to cast object of type ‘Oracle.DataAccess.Client.OracleConnection’ to type ‘System.Data.Common.DbConnection’

This was one of those awesome “doesn’t happen on any of my machine” errors.  After some mucking around we determined that the client machine was using the Oracle 10g  R1 client.  The machines and VMs here we used for testing were all running either 11g, or 10g R2.  Doh!

The simple resolution to this was to modify the NHhibernate config and add the following property:

   1: <property name="hbm2ddl.keywords">none</property>

Depending on your underlying databases and mappings this could cause problems with your code project.  Hopefully you’re not using reserved words, or bad Oracle syntax =).

Under the hood, the problem seems to be that the Oracle10gDialect does not provide an implementation of IDataBaseSchema for 10gR1.   Fabio Maulo has provided some sample code and the steps on for your database and submit it to the NHibernate project for inclusion.

April 2, 2010

ReSharper 5.0 is coming!

Filed under: Development — Tags: — Darrin Maidlow @ 5:29 pm

Ok, I’ve in the past – but I had to bring it up again..  Version 5 has some great new functionality and its   I’m especially excited about the huge web development.  I’ve been using the 5 beta and RC for probably a month or two and its great – I’ve been doing a metric butt-ton of refactoring and its great to have my changes go all the way into the .aspx files. 

The NUnit test engine is great.   I didn’t realize this was a ReSharper tool at first.  Now, I can’t live without it.

The localization plugin is going to save me days of effort on an upcoming action item. 

The class/code generating tools are indispensible – especially if you create a lot of classes (e.g NHibernate POCO’s).

The refactorings have been helping me slowly increase the legibility of my code – and also helping me build better coding habits.  Check out the comparison between

If you’ve never tried ReSharper –   only I could make the time to properly learn all the different tools available…

March 26, 2010

Upgrading to NHibernate 2.1 for new Validators!

Filed under: Development — Tags: — Darrin Maidlow @ 5:04 pm

I wanted to implemented the new which meant upgrading to NH 2.1.   Found a great post documenting the new “breaking change” between 2.0 and 2.1 pertaining to the new proxy factory functionality in NH.  “The ProxyFactoryFactory was not configured.” jumped up and bit me in the ass.  

NH used to assume Castle – but now it needs to be configured.  In addition to the Castly proxy, there is a new LinFu proxy available too.   , Davy Brion has it all for you here =). 

I opted to go with the Castly Dynamic Proxy – I’m not in a position to do any performance tests between– Castle has been around a long time and has a pretty big user base.  Good on this subject.  However, LinFu would have won if I was basing my choice on name alone…

After a bit of fiddling, I’m back up and running with NH 2.1 and shiny new validation attributes.   

mmmm validation…

April 8, 2009

ReSharper 4.5 Released!

Filed under: Development — Tags: — Darrin Maidlow @ 7:09 pm

JetBrains has just released !   Some of the key features of this upgrade are:

  1. 1. performance and memory usage improvements
  2. 2. Solution-wide code inspections (tons of cool tools to find unused params, privates, etc)
  3. 3. VB9 support (too bad I’ve been busy moving to C# =])
  4. 4. Native MSTest Support (I much prefer though)

 

Check it out.  ReSharper is one of the tools I use every day and couldn’t live without…One more day and I’ll be done in Visual Lisp and can actually try out the new release a little more =)

Technorati Tags:

April 6, 2009

Using Relative URLs in CSS with ASP.NET

Filed under: Development — Tags: , — Darrin Maidlow @ 6:42 pm

When we started redoing the I decided that this new site would be all CSS based.  No more nasty tables.   Once the initial xhtml/css template was made I moved it into and ASP.NET master page.  As the site was fleshed out I ran into a problem where some of the images were not loading as the master page was used deeper within the sites structure.   A little inspection of the underlying CSS and I noticed this:

   1: #header {width:100%; height:50px;    background: #7D7D7D url(images/header.gif) left top no-repeat;}

 

When the master page is used from a page deeper than the site root, the paths in the CSS become invalid.  To work around this I made my CSS dynamic!  I added a new page to my project in the CSS folder.  I called this page allcss.aspx.css.   Open up this new aspx file and remove all the html.  Leave only the page header definition:

   1: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="all.css.aspx.cs" Inherits="landorIS.com.css.allcss" ContentType="text/css" %>

image

Note that you may need to update your classnames and stuff depending on how you name the file.  I wanted my file name to reflect the fact that it was a CSS file – but it’s still needed to end with .aspx to ensure the file would be properly parsed by the ASP.NET engine without having to mess with the extension mapping on the web server.  This resulted in Visual Studio making some illegal class names.

Also be sure that you add the ContentType attribute to the @Page definition, and set it to “text/css”, or you will get an error in some browsers stating that the Mime Type “text/html” is not “text/css”.  Nice of Firefox to do a little validation on the content.

At this point you can add your CSS to the file and use <%%> tags or codebehind to manipulate the values.  Now the CSS that was causing problems with the images looks like this:

   1: background:url('<%=ResolveUrl("~/img/header-bg.png")%>') no-repeat 0 0;

 

Now when the CSS is requested by the browser and rendered out, the URL to the image file will be properly displayed as /RootFoolder/img/header-bg.png.  This is a very simple modification, but one could go a little more crazy and do something like  detect browser make/model and send out browser specific css in pre-defined literals, or possibly add authentication checks. 

 

 

Technorati Tags: ,

March 2, 2009

One Click Builds Are A Beautiful Thing – Automated Build Studio

Filed under: Development — Tags: , — Darrin Maidlow @ 7:00 am

I’ve known for a long time that an automated build system is valuable, hey it’s number two on .  Recently the complexity of some of our products made an automated build system a requirement.  It was getting to the point where creating a build of was a four plus hour ordeal.  It had dozens of little steps, and missing even one would result in a defect.   This defect would be a vicious cycle.  We would have to debug to find the problem and fix it.  This would cause ANOTHER four hour rebuilt, and the “cycle of bug” would continue.

This complexity resulted in infrequent builds and as a result the code was not tested as well as it should have been.  Not to mention, I’d get really frustrated and even pissed off on build days.   It took me a while to find a system that really made me happy.  On a whim one night I downloaded (ABS) and gave it a shot.  After installing ABS on a new virtual machine that would become our dedicated build system I fired it up.  I was pretty impressed with the wide range of actions and  tools it supported.  In our environment we had several must have items:

  • 1. SourceGear Vault
  • 2. Visual Studio 2008
  • 3. Oracle/SQL Server
  • 4. Installshield 2009

 

ABS worked with all of them.  In addition to the key requirements I had – it supported a plethora of other operations.

First thing I did was sketch out a rough outline of what was involved in creating a build.  Get latest source code, compile, delete temporary files, etc etc etc.  I started selecting various options from the ABS and fleshing out my macro.  Within a couple of hours, I had a sweet macro in place.  My macro started to look like this:

  • Getting the latest code from Vault
  • Compile all assemblies
  • Delete the temporary files
  • Build the needed release folder structure
  • Move the new files into the needed structure
  • Execute on all assemblies
  • Increment the build version, and set the version on all assemblies
  • Create a label of the build in Vault
  • Build a zip of the release
  • Build the Installshield installer
  • Connect to the build FTP and upload the new build

 

The best part – all of this was occurring in a 100% reliable fashion, oh and it took minutes for a full build to occur.  Over time, this project was tweaked, and copied.  It evolved as I learned more about ABS – or thought up new requirements.  Take for example, it has evolved into three editions, sharing a similar yet different set of source files.  ABS lets me rebuild all three editions with the click of a button – in minutes.   Long long ago, in the before times something like this would have taken days, and years off my life.

I’ve only scratched the surface of what I’ve been able to do with ABS – some other cool features worth mentioning is that builds can be scheduled to run, there is a web application for viewing build history as well as kicking off builds.  It has a scripting language, and the support I’ve received is fantastic.  The price of the product makes the decision to buy ABS a non issue.

Man, now I’m starting to sound like a sales guy. =)  Seriously though, check out if you develop boxed, or even custom software.

« Newer PostsOlder Posts »

Powered by WordPress

Switch to our mobile site