Backbone and ASP.NET MVC: Use Nullable types as Id

As a side note to my blog post about renaming the idAttribute in Backbone when using it with ASP.NET (MVC), this post continues with the Id attribute. The Id attribute in Backbone is very important. Backbone uses it to determine if an object is new, or already existed. Collections use it to determine if it already contains any given object. So I ran into a problem in a situation where my model required a lot of “supporting data” before it was saved. To add this data, I decided to create the models on the server and add it to a collection (since there were a whole list of them). No matter how many models I added to the list, only the first one was rendered. Why? Because my model on the server was using a non-nullable Integer as its Id property … Continued

3 Reasons Why Dedicated ViewModels in ASP.NET MVC is a *MUST*

I’d argue that using dedicated view-models in ASP.NET MVC (or any other MVC framework) is one of the things that has changed the way I work for the better. And for the better I mean more maintainable code, better designed code, more robust code — just better in all ways measurable. Here are three reasons why you should use truly dedicated view-models for any view in ASP.NET MVC. 1. Abstract code beyond your control If you work on a web team, that is part of a larger team you cannot expect to always have the entire back-end ready at the time you want to start coding a feature for the front-end. But why wait? Even if you don’t have the service layer, the domain models or the database ready, you can just easily create dummy instances of your view-model in a controller and … Continued

Backbone and ASP.NET MVC: Rename the ID attribute

In Backbone, a model’s ID attribute is vital in the way Backbone handles models and collections. In a collection, you can call get with an ID and you’ll get back the model. The thing is, naming conventions across languages does not always agree. In .Net properties are named in PascalCase. In JavaScript camelCase is the standard, so naturally this leads to a conflict when your model in Backbone names the ID attribute id, and the corresponding domain model on the server side is named Id. I was using an ASP.NET MVC controller to return a collection of Accounts, but when it hit the client, got changed and later saved, the ID was lost and the model was created as a new instance in the database. The problem was that when Backbone fetched the collection, and automatically turned the objects in … Continued

Entity Framework: Update single column

Yesterday I blogged about how to delete a detached entity using Entity Framework 5 by only using its Id. A common practice in many applications today is to not actually delete the entity from the database, but instead mark it as deleted and make sure your data access layer filters out those “deleted” items when selecting. So I wanted to implement a general “delete” method that would update the DateDeleted column of an entity without touching any other column. Again I use a detached entity, so the trick is to only mark the DateDeleted column as modified, and not the entire entity. public bool DeleteEntity(T entity) where T : ModelBase { entity.DateDeleted = DateTime.UtcNow; this._databaseContext.Set().Attach(entity); this._databaseContext.Configuration.ValidateOnSaveEnabled = false; this._databaseContext.Entry(entity).Property(m => m.DateDeleted).IsModified = true; int recordsAffected = this._databaseContext.SaveChanges(); this._databaseContext.Configuration.ValidateOnSaveEnabled = true; return recordsAffected == 1; } Notice how I disable validation, and … Continued

Entity Framework: Delete Entity by Id – Using Detached Instance

Entity Framework 5, with the Code First approach can do some pretty powerful things without much effort. Create your model classes, and it spins up a brand new and shiny database for you. But sometimes you do need to tweak a few things in order to achieve what you want. For example, I don’t want to load an entity before I can delete it. What’s the point? Say I have a list of Accounts on a webpage, with a shiny delete button that makes an AJAX call to an MVC controller, (or Backbone model, that hits the web backend). In that situation I’ll only have the Id of the entity, which should be enough to delete it. Attach, Delete To delete only by Id, you need to create a dummy instance of your model with the Id you wish to … Continued

Backbone.js Compatible Routes for, non-Web API, ASP.NET MVC projects

Backbone.js has become my JavaScript MV* framework of choice (you do use a JavaScript MV* framework, to structure JavaScript, right?). I find myself using ASP.NET MVC for less and less that has something to do with views, and other stuff that belongs on the client. The server, being ASP.NET MVC, is the new backend now a days. I’m sure that’s how Microsoft sees it as well, with the release of Web API. When using models in Backbone, and specifying the urlRoot setting, Backbone will automatically construct URLs to do CRUD operations against your server. It uses RESTful URLs, and regular ASP.NET MVC controllers are not compatible with those out of the box. Consider this Backbone model and collection: var Account = Backbone.Model.extend({ urlRoot: “/accounts” }); var AccountCollection = Backbone.Collection.extend({ model: Account, url: “/accounts” }); When you save a new Account, Backbone will make a POST … Continued

How To Hit Back When Your Boss Uses Salary Statistics To Negotiate

You sit in a meeting with your boss, negotiating your salary. Of course, you’re the awesome developer and feel like you should be paid 10x as much as the guy sitting next to you. You know that smart developers who gets things done are scarce, very scarce! And yet, your boss is arguing that your salary should be X because that’s the average salary for a developer in your area, based on some crappy statistics he pulled off of a website just before the meeting. How To Hit Back With all the confidence you can get, you must counter argue by saying that you are not an average developer. That you think this company needs more than average people to carry out their goals and deliver products that aren’t average either. And  that he must up his game dramatically, if … Continued

Use Recurly.js with ASP.NET (MVC), C# with this Nu Get library

Recurly is a full suite recurring payment service that lets you easily create subscription plans, billing cycles, handle customers, revenue etc. They made payment integration dead easy with their awesome JavaScript library called Recurly.js that you can use to inject a payment form onto any page with a simple piece of JavaScript, a long with a server generated signature. But .Net as a platform, and C# as a language is not very well supported by Recurly. They don’t event provide a client library for creating those signatures. I needed a client library and wrote this simple class that I’ve now uploaded to Nu Get. The package is called RecurlyJS, and the source code is on GitHub. Enjoy!

Unit Testing ASP.NET MVC Controller Action contains specific ActionFilter Attributes

ActionFilters in ASP.NET MVC are great. You can now easily share logic between controllers without having to inherit from a base controller, that does the common work. I have a content heavy application that supports a set of layouts. Each layout is rendered by setting the Layout view dynamically, which I do from an ActionFilter. My ActionFilter is fully tested, but when you delegate work to an ActionFilter, you should write a test that ensures the filter is defined on the action. Not surprisingly, this is very simple and relies on reflection (Download from Gist: https://gist.github.com/2605628): /// <summary> /// Verifies the controller action, contains an attribute of the specified attributeType. /// </summary> /// <param name=”controller”>The controller.</param> /// <param name=”action”>The action method.</param> /// <param name=”attributeType”>Type of the attribute to look for.</param> /// <returns>Returns true if the attribute was present on the action. Otherwise … Continued

Make features obvious — forcing usage is even better

When creating software, make your features obvious. A long time ago, Facebook introduced friendlists – a feature where you can group friends by any name you want. Along comes Google+, and have a feature called circles that, unlike Facebook’s lists, are very obvious and an integral part of the design and workflow for when adding friends. All over the press, this is the feature they focus on claiming it has an advantage over Facebook. It seems as if they don’t know about the lists feature of Facebook, which makes it very important to focus on the features you need and make them obvious. People use what they can see, and what they’re forced to use. It also makes you think about the countless times, when someone says “I hate the bla bla bla” – and most technical minded people just … Continued