July 24, 2010

Creating .NET objects for Mapguide Enterprise 2011

Filed under: Development, Mapguide, RADE — Tags: , — Darrin Maidlow @ 11:15 pm

Better late than never…  I’ve updated my Mapguide Enterprise .NET objects to work with 2011.  The steps in my   have been modernized and simplified significantly.   This post will show you how to create (or re-create) these objects for the 2011 release of Mapguide.  Sorry for skipping 2010 =)

First, download the actual non-aplha release of and extract it.  You will need to have .NET 3.5 installed to run LINQ to XSD.  The project itself is a Visual Studio 2010, .NET 4.0 project.  You should be able to change it to .NET 3.5 with no problems.  I’m not sure about using .NET 2.0.   I still assume these steps should work with Mapguide Open Source 2.2 given its pretty much the same thing as MGE 2011.  Please let me know if you try it and that is not the case.

Building the classes

Last time around we had to mess around with Visual Studio projects, building temporary code, finding and extracting that code from temporary files.  This time around, we’re given a nice little executable that can be run in a batch file.   I’ve posted a copy of my batch file below but it was simply made using a dir /b > CreateMapguideNetObjects.cmd in the Mapguide server schema folder (which by default is to c:\Program Files\Autodesk\MapGuideEnterprise2011\Server\Schema) .  I then edited that file with a text editor that support macros and removed the following schema files:

  • FdoProviderCapabilities-1.0.0.xsd
  • LoadProcedure-1.0.0.xsd
  • LoadProcedure-1.1.0.xsd
  • LayerDefinition-1.0.0.xsd
  • LayerDefinition-1.1.0.xsd
  • LayerDefinition-1.2.0.xsd
  • SiteInformation-1.0.0.xsd
  • SiteVersion-1.0.0.xsd
  • SymbolDefinition-1.0.0.xsd
  • WebLayout-1.0.0.xsd

These files are deprecated object definitions from previous Mapguide releases.  In the end each line in the batch file looks a little like this:

LinqToXsd ApplicationDefinition-1.0.0.xsd /filename:ApplicationDefinition.cs

Executing the batch file will create a number of C# files containing appropriately named classes.

Setting up your project

At this point you should be able to fire up Visual Studio and create a new C# project.   If you have an existing project from a previous version of Mapguide its good to start fresh.  Also, don’t try this in an existing solution that is dependant on the project that contains the Mapguide objects.  This will just result in a bunch of extra screwing around to avoid compiler errors from missing code during the process.    This time around its a lot easier.  Create a new DLL project.  Add a reference to the Xml.Schema.Linq.dll file that was included with LinqToXsd.exe.   Add a reference to the following Mapguide dlls:

  • OSGeo.Mapguide.Foundation
  • OSGeo.Mapguide.Geometry
  • OSGeo.Mapguide.MapguideCommon
  • OSGeo.Mapguide.PlatformBase
  • OSGeo.Mapguide.Web

You’ll also need to ensure that the appropriate unmanaged assemblies are available in the calling applications bin folder when you go to run this stuff. 

Next place all of the generated C# files in the new project.  Once you build you’re going to see a ton of errors.  We’ll clean those up.

To resolve a lot of these errors I did a global search and replace on “global::” and replaced it with nothing.  Also, I wrapped each class in a unique wrapper classes to prevent duplicate type errors.  Finally, the case issue with DataType was still an issue – and I resolved it by changing the case on DataType to be Datatype:

   1: public string DataType {
   2: public string Datatype {

For a complete view of the changes I made do a file compare between the code in the attached zip file and your newly generated code.

<insert 3 week gap here, wherein I had so that I could actually test this newly created code, oh and I also went on vacation for a few days too =)/>

And back.  The best part about this is that it seems ALL my old code just worked with upgraded basic layouts after changing it to look for the object definitions within the new wrapper classes.   Now I just need to add support for flexible layouts and we’ll be laughing.

The Code

Please note – I didn’t rebase the code.  It’s all in the RADE.MGE namespace.  If you would like to use it, feel free to re-base it – or just leave it as is.  Also, if you create any unit tests or enhancements and would like to share them – please feel free!  

As I update the project myself, or receive updates I will update this file.

Finally, the will still work with this new project.  Perhaps one day, I’ll post some fancy new code in C# =)

Hope this comes in handy, I welcome your comments.

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: ,

Older Posts »

Powered by WordPress