Massive and multiple arguments

I thought that Massive would simple let me do something like this:

table.Delete(where: "Pin_Added = @0 and Pin_Schools <> @1", 
             args: new { pinAdded, pinSelected });

That would be slick! But I get something more like this:

lettering-kaboom

It turns out that that fancy anonymous object doesn’t get parsed. I tried to grab some of the fancy tricks from the MVC source code, like this:

RouteValueDictionary result = new RouteValueDictionary();

if (htmlAttributes != null) {
    foreach (PropertyDescriptor property in 
    			TypeDescriptor.GetProperties(htmlAttributes)) {
        result.Add(property.Name.Replace('_', '-'), 
        		property.GetValue(htmlAttributes));
    }
}

return result;

But that did not work. I found this post on DataChomp that solved my woes:

object[] args = { pinAdded, pinSelected };
table.Delete(where: "Pin_Added = @0 and Pin_Schools <> @1", args: args);

EF and “dynamic” data

We use EF for our e-commerce website, and we have the need to get some data out in the form of reports. Ideally I’d like to be able to basically write up a report query and have the data come out on the other side. However, with EF it isn’t necessarily that simple.

I really don’t want to have to go through all the ceremony of creating models, mapping classes, services, etc. I’d like to keep it pretty simple with a service that can give me back the data I want, then I can use it in a Google Chart or a jQuery datatable or something on the client side. I want the server-side to be basic, and then I can focus on the client-side to give a rich report experience.

I did a quick search and came up with a couple of interesting options. The first is a way to dynamically query views. It sounds very interesting, but it looks like it isn’t quite what I am looking for. The post leaves things a little open-ended, but it looks like the views he is querying already have all the strongly-typed stuff needed and the code just performs dynamic queries against those static objects. I could be wrong though as there is not concrete example of how it is used, just the theory behind all the dynamic wackiness going on.

The next option looks a little more like what I am looking for – it returns a dynamic object using SqlQuery. The code in the post is a little verbose for my taste, but a commenter converted the code into an extension, and I like that idea a lot better. I put his code into a gist.

After looking into the options, I think that maybe the notion that I have to stick with EF is just holding me back. I am very familiar with Massive and have used it and enjoy using it. It is a single class file, and maybe it is just the thing I need to get this report data out of my database easily – which is the idea behind Massive!

Massive

I finally had a good situation to use Massive, so I took it.

I was working on some functionality of a web service, and I needed to refactor some data access code. We were using SubSonic, and that is great and simple (typically), but this data access code is in a library, so changing meant opening up the project, making changes, building, pulling the new DLL over, etc. Not a huge amount of work, but too much work for a web service that is basically working with a single table.

There was minor learning curve, but Massive is pretty darned simple. It was mostly my mistakes in not paying attention, or old ASP classic-style blunders like using the wrong name for the column. Overall, it was a lot of fun.

My favorite bit was when I needed to get a SUM and a GROUP BY. If I were using EF or SubSonic, I may have struggled a bit with the proper LINQ syntax to do this (I need to increase my LINQ-FU skills, they are not strong), but with Massive I simply… wrote SQL… GASP!

I made a custom object with the 2 properties I needed, and then I looped over the returned values, pushed ’em into a list and returned it as an array. Here is the code:

[WebMethod, SoapHeader("Authorization")]
public DownloadDetail[] GetOrderDownloadCounts(object orderId, 
                                               object profileId) {
    var list = new List<DownloadDetail>();

    dynamic table = new DownloadAuthorization();
    object[] queryargs = { orderId, profileId };
    var auths = table.Query(@"SELECT Id, SUM(DownloadCount) AS Total
FROM Downloads
WHERE TagString=@0 AND ProfileID=@1
GROUP BY Id", queryargs);
    foreach (dynamic auth in auths) {
        list.Add(new DownloadDetail { Id= auth.Id, 
                                      DownloadCount = auth.Total });
    }

    return list.ToArray();
}

I am thinking I could have done some sort of LINQ-y extension to eliminate the foreach loop, but maybe not? I don’t really know much (read: anything) about dynamic, so maybe it would not work? Not really sure. I guess I am showing my Intellisense crutch dependency here.
🙁

Backbone.js

We have decided to use backbone.js for a project at work, so I have been soaking up everything I can find about it. Tekpub has a great series (currently in development) that I am really digging, and it has got me pretty excited.

I recently checked out the Peepcode preview and it looks pretty promising. A co-worker also pointed me to a great book hosted on Github. I’d also like to check out the NetTuts backbone/.NET videos. Can you tell I like video learning?
🙂

I started thinking that WebAPI would probably work really well with backbone rather than a full-blwon MVC site. I started searching and I have been finding some great links, so I thought I’d compile a list of them here:

I am (obviously) a little late to jump on the backbone bandwagon, so there is a ton of great content out there. I would really like to find a project that can allow C# code to be converted into backbone models, but that may be a bit difficult. It would be great to use the strongly-typed models and then add some spice for backbone and have some Nuget-able library that can convert the C# model/spice to backbone for you to save some of the javascript work. Don’t get me wrong – I am not afraid of javascript. Quite the contrary – I have grown to really love javascript. What I don’t like is doing things twice. Writing the backbone views and then writing a lot of the same type of code on the server-side (for the data persistence) just doesn’t sound appealing.

One thing that could maybe make it all a little easier is to use Massive. I recently swapped out some data access code that was using SubSonic to use Massive (eliminating some other DLLs along the way too) and it is pretty cool. Of course, it uses the dreaded dynamic keyword, so no fancy-pants Intellisense, but that is okay. Methinks that a properly coded backbone app using WebAPI would result in very little server-side code anyway, so some dynamic objects pushing and pulling to and from a db would be no big deal.