• Ensure Spark Views Are Marked As Content With Powershell

    I kept publishing a website project only to find each time a couple of spark files didn’t make it because they were not set as content in the cs project file. In order to limit the wasted time, I added the code below to the build script.

    Below I’m using the following to grab the project file as xml. Then foreach (|%) None element in Item group elements. Where (|?) the Include attribute is a spark file, add to the $notset collection. If any, then list and throw to stop the build. This was just added as a psake task. BTW, I’m really loving psake, you should really check it out if you need a build script on windows, for anything.

    $basedir = Resolve-Path .
    $proj = [xml] (get-content "$basedir\src\website\website.csproj")
    $notset = $proj.Project.ItemGroup |% {$.None} |? {$.Include -like "*.spark"}
    if ($notset.Count -gt 0) {
      $notset; throw "The above Spark files not set to content."
  • Mapping NHibernate many-to-many with a Class for the Joining Table

    I wanted to have a many to many association in nhibernate so that additional info can be added to the joining table. Like the date the relationship was setup. I couldn’t quickly find anything on google, but then remembered that this is really 4 associations in nhibernate; one-to-many : many-to-one for ClassA and one-to-many : many-to-one for ClassB.

    In this example we’re not going to map one-to-many from ClassB. ClassB will not know about this relationship, which will result in orphaned records if we delete any ClassB records.

    Mapping for Class A:

    <?xml version="1.0" encoding="utf-8" ?> 
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" default-lazy="true" assembly="Core" namespace="Core.Models">
        <class name="ClassA" table="classA">
            <id name="Id">
                <generator class="guid.comb"></generator>
            <set name="Matches" cascade="all-delete-orphan" lazy="true" inverse="true">
                <key column="ClassAId" />
                <one-to-many class="ClassAMatch" />

    Above ClassAMatch is the many-to-many class. The set is the one-to-many association to ClassAMatch.

    <?xml version="1.0" encoding="utf-8" ?> 
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" default-lazy="true" assembly="Core" namespace="Core.Models">
        <class name="ClassAMatch" table="ClassAMatches">
            <id name="Id">
                <generator class="guid.comb"></generator>
            <many-to-one name="ClassA" column="ClassAId" lazy="false" cascade="none" not-null="true" />
            <many-to-one name="ClassB" column="ClassBId" lazy="false" cascade="none" not-null="true" />
            <property name="MatchedOn" not-null="true" update="false" />        

    Above, this sets up the many-to-one associations to ClassA and to ClassB. This will be enough to persist the relation, but remember it will leave orphaned records if ClassB records are deleted since ClassB doesn’t know about his relationship/association. To fix that add a similar set mapping to that shown for ClassA.

  • Warming up to MVC2, Castle, and Spark with Red Yawk

    Red what? Red Yawk (silent W), as in Red27 Consulting’s Yet Another Web frameworK. I’m not sure I really like the name, but it’ll work for now.

    I spent a good part of today trying to decide on what web framework to use for some upcoming projects. I spiked with FubuMVC, which I would have liked to use, but the lack of documentation and my lack of knowledge about it was too much to overcome right now. I also spiked MonoRail, which I’ve used in the past and generally like. The main thing here was the lack of portable areas. So, I settled on Asp.Net MVC 2. I like the idea of portable areas, I’ve been working with MVC 1, so I’m familiar with a lot of helpers and architecture. I like that n2cms works well with it. And companies will most likely feel better having there site built on in simply because Microsoft is behind it. Regardless, I had to pick something.

    The goal is to have a solid base project to start with, without rebuilding everything every time a new project is started. Some requirements from me; Areas, Container (prefer windsor), Spark, and ultimately the ability to add on binary extension, portable areas. Also, it’s using Psake for build support, NUnit for testing, MongoDB for the document database.

    I recently saw a post about Warmup, which seems like it would work well, and so far it has. I think I’ll have other warmups, that include NHibernate support and n2cms support later

    Checkout the start of the project at http://github.com/candland/redyawk-warmups. Once, warmed up you can run the psake script to build and test, you’ll need to install psake or import the psake module in the tool directory.

    Is there a directory of Warmup templates?

    Is there a directory of Portable Areas?

  • Resolve Lists and Arrays with Castle Windsor

    I’ve worked with the Castle Project for a long time, primarily with Windsor. However, I often forget there are Array and List dependencies resolvers build-in, but they need to be wired up. This is mostly for my memory and hopefully it helps someone else.

    This sub resolvers are in this namespace;

    using Castle.MicroKernel.Resolvers.SpecializedResolvers;

    Before registering components, add the sub resolvers;

    container.Kernel.Resolver.AddSubResolver(new ArrayResolver(container.Kernel));
    container.Kernel.Resolver.AddSubResolver(new ListResolver(container.Kernel));

    Use like this, where there are multiple IService implementations that are needed by the DependsOnArrayOrList class.

    public class DependsOnArrayOrList
        public DependsOnArrayOrList(IService[] services)
        public DependsOnArrayOrList(IList<IService> services)
    public interface IService

    Not sure why these sub resolvers are not setup by default, but regardless they’re easy to setup and come in handy.

  • Psake Builds and TeamCity Integration

    Psake is a powerful build system, build on Powershell for a lot of functionality and familiarity. And TeamCity is a really nice continuous integration server, checkout the CodeBetter TeamCity install for a good demo. Hooking the two up was not a easy as using just Msbuild or Nant, but it’s not really too bad.

    First, your Psake script needs to check the exit codes of the applications it calls. This is important to do regardless because Powershell doesn’t error out based on the exit code. The trunk version of Psake has a convenience function to execute a command and check it’s exit code is greater than 0, called exec:

    You can also pass a message as the second argument.

    With this checking in place, next is a bat script to use from the TeamCity command line build option.

    @echo off
    powershell -NoProfile -ExecutionPolicy unrestricted -Command "& {Import-Module &lsquo;.\tools\psake\psake.psm1&rsquo;; invoke-psake -t %1; if ($Error -ne &lsquo;&rsquo;) {write-host "ERROR: $error" -fore RED; exit $error.Count} }"`

    This executes a Powershell prompt which imports the Psake module and then calls the provided Psake task. Finally, it checks the $Error variable and sets the exit code to the number of errors returned by the script. This will fail the TeamCity build and allows any error thrown by Powershell to break the build.

    Last is to have TeamCity run the bat file with the desired target.

    psake.bat Compile

    One thing to note is that some things return exit code other then 0 but that are not errors. In that case you can check the Powershell variable $LastExitCode and throw errors based on that.

    Thanks to the psake users group for all their help!

  • error CS1668: Warning as Error: Invalid search path ‘C:\Program Files\Microsoft SDKs\Windows\v6.0A\lib’ specified in ‘LIB environment variable’

    I’m not sure what caused my setup to produce this error, but after searching for a while and not finding any reason, I just added the lib directory and all was good. I was getting this when doing command line compiles and when using the spark view engine. I hope this might save me or someone else some time in the future.

    error CS1668: Warning as Error: Invalid search path &lsquo;C:\Program Files\Microsoft SDKs\Windows\v6.0A\lib&rsquo; specified in &lsquo;LIB environment variable&rsquo;
  • A New Year &amp; A New Venture: Red27 Consulting

    business-card-frontI’ve decided to take the plunge and start consulting full time. My company, Red27 Consulting, has been around for few years, but this year I will be pursuing consulting/independent development work full time. Red27 will be an agile/alt.net/craftsmanship based business, building websites and web applications. We will partner with customers to build what they need quickly and with high quality. The challenge is how to show the value in these agile/alt.net/craftsmanship processes and principles to people not in software development. I believe, over time, using these processes and principles will prove to be a better way to build software. I will write about these experiences here along with the technical content that I’ve been writing.

    Lastly, keep Red27 Consulting in mind when someone has a website or web application they want built!