• Post a collection of ViewModel's to a MVC Action with jQuery posted on 07 Dec 2011

    Maybe I searched for the wrong thing, but I couldn't find what I was looking for :( My Bing and Google fu failed me.

    Basically I wanted to post a collection of ViewModels to an MCV action. Turns out it's rather simple.

    Lets say I have a bunch of Products, and Products are managed in a WarehouseLocation. A product doesn't have a warehouse location, since it could exist in multiple locations.

    If I'm currently working in Location A, I want to post a collection of Products to an action, as well as the WarehouseLocationId.

    So given a simple ViewModel, and an Action:

    public class ProductViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }
    

    and

    Read more...

  • Optional Parameters with AttributeRouting posted on 27 Nov 2011

    I found a little trick with using Optional Parameters with AttributeRouting, by using standard optional parameters in the action.

    The documentation says you can add an attributes to specify the defaults, or add =value to the parameter name, and I guess that's a more correct way to generate routes, but you can achieve the same affect by making the parameter optional. Like so:

    [GET("videos/{?page}")]
    public ActionResult Videos(int page = 1)
    {
        return View("Result");
    }
    

    If I browse to the URL:

    It uses the default value of 1.

    Now when appending a number to the end of the URL:

    Read more...

  • Split Windows + Web Workbench = Win posted on 27 Nov 2011

    One of the cool features of the Web Workbench from Mindscape is the ability to generate the output files. Prior to using the Web Workbench I was using .LESS{} which uses an HTTP Handler to generate the output files for LESS.

    The problem with this is sometimes I would write some CSS and not realise I missed something only to find my site doesn't display anything, then I have to figure out what I did wrong.

    With the Web Workbench, and it's ability to generate the files every time I save, makes me less error prone.

    But the most efficient way of working with it I found (when writing a lot of CSS in 1 sitting) is to split my windows so the first window has where I'm writing, and the second window has the generated CSS file.

    If I make an error on the left side:

    I see the error on the right.

    If I make a change to the left, (added a background colour) i see the change on the right.

    Read more...

  • Unit of Work with WCF and Autofac posted on 06 Nov 2011

    I've just spent the last few days trying to find a way to have a Unit of Work with WCF, but it seems no one has a nice clean easy solution.

    The first, and the only decent solution I found was here:

    http://ianfnelson.com/archives/2010/04/09/wcf-nhibernate-unit-of-work-endpoint-behavior

    The problem I found with this solution is that the ICallContextInitializer as well as the EndpointBehavior is only created once. So it would seem all calls to a service would share the same Unit of Work instance.

    Maybe Castle Windsor does something fancy and injects a brand new EndpointBehavior every request to a service, but for me, it seemed the ServiceBehavior, EndpointBehavior, and ICallContextInitializer were all created once.

    This caused my service to resolve a different instance of IUnitOfWork to what was in the ICallContextInitializer.

    Interim Solution 1

    The first solution I came up with was to use Autofac to call Commit on release:

    builder.RegisterType(typeof (UnitOfWork))
            .As(typeof (IUnitOfWork))
            .InstancePerLifetimeScope()
            .OnRelease(x =>
                            {
                                ((IUnitOfWork) x).Commit();
                            });
    

    Read more...

  • Windows Phone, Windows Live and MSN posted on 02 Nov 2011

    Windows Phone 7.5 has been getting some awesome reviews, and rightly so. The thing is amazing. I really think Microsoft has begun leading the way for the mobile platform.

    But I'm highly pissed off with the Windows Phone team. Why? Because to use MSN, your msn contacts must be associated with the first live account you register to the phone.

    I don't even know what to say about this, it's so silly. I understand that it might get confusing to allow multiple Facebook accounts, multiple Windows Live accounts, etc.

    But could they not allow you to choose which account MSN contacts are associated to?

    When I first setup my phone I didn't use the Windows Live account I use for MSN (my Hotmail) since it's existed since 1997, it's got 100's of contacts I don't want in my phone-book, a lot of MSN contacts exist from work, or playing online games, or from forums etc. So I used the Windows Live account that I use for MSDN. I added all my contacts and everything was great, (after I imported all my contacts from outlook into Windows Live Contacts)

    Read more...

  • RavenDB - Changing the Lo on the HiLo Generator posted on 24 Oct 2011

    Well I'm currently learning RavenDB, it's awesome! But I noticed when I put data in, all the Id's generated every time I ran up my application to test were:

    1, 2, 3, 4, 5...

    1024, 1025, 1026, 1027, 1028...

    2048, 2049, 2050, 2051, 2052...

    This would be fine after the app is deployed since I wouldn't be restarting it over and over and over, but during development I personally find it annoying that the numbers jump so high.

    Fortunately I figured out a way. (which about an hour later I found on Google Groups, granted I had to use a different keyword to find it)

    Basically you just need to create a new instance of the MultiTypeHiLoKeyGenerator class, passing in the arguments and assigning it to the document store:

    var documentStore = (new DocumentStore { 
        Url = "http://localhost:12321/" 
    }).Initialize();
    
    var generator = new MultiTypeHiLoKeyGenerator(documentStore, 10);
    
    documentStore.Conventions.DocumentKeyGenerator = 
        entity => generator.GenerateDocumentKey(documentStore.Conventions, entity);
    
    using (var session = documentStore.OpenSession())
    {
        session.Store(new Project() { Title = "Hello World" });
        session.SaveChanges();
    }
    

    So running up my app once:

    Read more...

  • NHibernate Designer 2 posted on 20 Oct 2011

    The guys at MindScape have released their next version of NHibernate Designer, which now supports Fluent NHibernate and a whole heap of new features.

    They also dropped the price down to just $99!

    I recommend checking it out

    http://www.mindscapehq.com/blog/index.php/2011/10/19/nhibernate-designer-2-is-here/

    http://www.mindscapehq.com/products/nhdesigner

    Read more...

  • Website Folder Structure? CSS Files? Does anyone care? I do... posted on 17 Oct 2011

    When we build website's, more often than not we have: Separation of Concerns. Even if at most it's just basic 3-tier Architecture

    What annoys me though is a lot of time no one ever thinks about separation of concerns when it comes to the folder structure of their website, and where things go.

    Folders

    The one thing that annoys me above all else, is when 'layout images' (images for layout, design, styling etc) of the website, are shoved into the same folder that holds what I call "Content Images".

    For example:

    root/ 
    root/css/ 
    root/images/
    

    What I prefer to do is structure it so the css folder has it's own images folder, specifically for layout images.

    root/ 
    root/css/ 
    root/css/images/ 
    root/images
    

    Read more...

  • The benefits of letting the ORM generate the Identity (part 1) posted on 22 Sep 2011

    One thing I've learnt is that letting the database generate the identity for you is a bad thing. It always annoyed me that Oracle never gave a feature like AUTO_INCREMENT in MySQL or IDENTITY in SQL Server. I never understood, when inserting data how do I give it an Id?

    Well one of the benefits of ORMs such as NHibernate is we can generate the identity our-self, or rather, the ORM can generate it so we don't rely on the database. This also plays a major part in our code base when we insert a graph or batch of data and how the identity is added to our object.

    Ayende recommend avoiding identity.

    The thing with using SQL Server"s identity is that we need to select the identity back out after we do an insert. Not only that, when using NEWID()/NEWSEQUENTIALID() there is no way to select the value back other than using all the fields in a select in order to get the GUID relates to the record with all those values matching.

    For example:

    Given this rather simple table using IDENTITY.

    CREATE TABLE People
    (
        Id int NOT NULL IDENTITY (1, 1) PRIMARY KEY,
        FirstName nvarchar(100) NOT NULL,
        Surname nvarchar(100) NOT NULL
    )
    

    Read more...

  • Fluent NHibernate - Table Inheritance - Discriminators (Part 2) posted on 22 Aug 2011

    This is part two, to my post about Table Inheritance using Discriminators, in this post I just want to demonstrate the outcome when the sub-classes have their own properties, or possibly a property that maps to the same column.

    First thing however is Mark Perry pointed out in the comments that specifying a value for the baseClassDiscriminator will force it to store the value in the database as an INT rather than a a VARCHAR.

    DiscriminateSubClassesOnColumn("PostType", 0);
    

    This will create the table with an INT like so:

    create table WallPost (
      Id         UNIQUEIDENTIFIER   not null,
      PostType   INT   not null,
      DatePosted DATETIME   null,
      Title      NVARCHAR(255)   null,
      Content    NVARCHAR(255)   null,
        primary key ( Id ))
    

    Maybe INT is too big however, maybe we only want a SMALLINT? That will give us 32k sub-classes...

    DiscriminateSubClassesOnColumn("PostType", (short)0);
    

    Read more...