Tuesday, November 9, 2010

WatchedIt - An MVC3, Razor, EF4 Code First Production

One of the features I love about applications like Boxee is it treats the content you're watching like email. Instead of "read" and "unread" it's "watched" and "unwatched". This is great if you don't have a significant other who tends to watch all of your shows before you. ;)

So I decided to make an application I call WatchedIt to track where I left off watching shows. What a perfect opportunity to try out some new technologies! For this project I used the following fairly new technologies:

So what did I think of the experience? MVC3 is a promising improvement over MVC2 and Web Forms. Outside of a radical paradigm change (like being more dynamic like Ruby) I'd pick MVC3 for my next project without hesitating. It integrated nicely with jQuery by offering ways to produce and consume JSON.

HTML5 gets a big thumbs up! I've already begun using as many features as I can that are backward compatible with older browsers. If you want to know what you can and can't use now, I recommend HTML5 and CSS3 by Brian Hogan.

I really liked Razor! But the lack of intellisense (auto completion, etc) in Visual Studio was a bit painful. Hopefully this will get added soon and it will be my preferred View Engine hands down. It was a little hard to figure out when you don't have to use the @ symbol to prefix code but the error messages were very helpful.
UPDATE 11/9/2010: And 3 hours after posting this, Intellisense has been added in MVC 3 RC!

EF4 Code First didn't go nearly as smooth as the other new features even though I love the concept. The parts that worked are great but it still has a few too many limitations for me to pick it again without hesitation over other options. Some of the limitations:
  • I couldn't find a simple, elegant way to do a cascading delete of all child objects when you delete a parent.
  • You can't have more then one DbContext share the same database (not a major issue)
  • Deploying to production didn't go very smoothly. I had to configure the connection string to use the "sa" account to get the DB created.
Keep in mind these .NET offerings are all fairly new and I think Microsoft is on the right track with all of them.

I've decided to give back to the community and put the source code on GitHub. I'm sure there are some things I could have done better or that didn't follow best practice so if you find something that would have been much easier to do another way let me know. I'd love to hear it.

Some 3rd party libraries that made this application possible:
Elmah - my favorite error logging library
tvdblib - they did an awesome job of wrapping the TVDB API!
TheTVDB - without this DB this application wouldn't be possible. Please consider contributing info and artwork.

Saturday, October 30, 2010

Custom iPhone 4 Mount For Car

Cutting to the chase. Here are the pics of my new custom iPhone mount!


Previously I had customized my car to mount my iPhone 3G using a Griffin iPhone 3G Windshield Mount.

A couple months later the iPhone4 came out and it does not work with the Griffin mount I was using. I took a gamble and order a Griffin WindowSeat AUX.

I got very lucky and was able to put the cradle from the new Griffin mount onto the existing arm from the 3G mount. It's an extremely tight fit but it works. It doesn't swivel as easily as I'd like but I'm very happy I didn't have to do anymore cutting.

The next customization I wanted to do was to add steering wheel controls for music. I found this Kensington LiquidAUX Bluetooth Car Kit and it was on sale so I got overly excited and bought 2!

After it arrived I was disappointed to find out that my Alpine CDA117 car stereo does not have a regular "line in" that would work with the Kensington Bluetooth kit. :(

In addition the AD2P audio takes over the wired line out so it was looking like I'd have to not use the Bluetooth kit or buy a new stereo. After some googling I found the solution to my problem. If you jailbreak your iPhone and install Bluetooth Profile Selector (BTPS) you can disable the AD2P out and the audio will play out the wired connection, BUT the wireless steering wheel controls still work!

The Kensington Bluetooth kit allows me to play/pause, and skip songs forward and backward and also has a button that will bring up Voice Control. Voice Control seems to work Ok for dialing numbers. So far I haven't found the trick to get it to play songs/artists while connected to the car stereo.

The final mod I did was to disable the "Accessory Connected" splash screen that replaces the iPod apps UI when connected to my head unit. There is another app on Cydia for jailbroken phones called No Accessory Splash. Install this and you can use the iPod app like normal in addition to having the head unit controls work.

The combination of a nice touch screen and steering wheel controls should make my commutes much more pleasant! Especially considering every other feature in my Mitsubishi Evo IX RS is manual (locks, windows, mirrors, seats, etc). For those curious the RS is a stripped down model primarily marketed to be raced. This saves weight and allows the car to be sold at a lower price.

Thursday, October 14, 2010

How Not to Implement Unsubscribe

UPDATE 10/14/2010 11:55 AM CST: SpeakerRate may not have done the unsubscribe correctly. But they know customer service. Thanks guys!
http://twitter.com/#!/speakerrate/status/27356776561

This is just plain unacceptable. I rated one speaker and used my email address to do so. I get a newsletter from SpeakerRate the next day which is fine because I planned to just click the Unsubscribe link at the bottom. Here's what I was presented with:



Do you really think I want to create an account?

Friday, October 8, 2010

Count the Number of Network Connections in Windows

If you need to count the number of network connections to a certain port start by entering this command into a command prompt:
netstat -a -n

Then find the IP:Port combo you want to count and modify the following command to get the count:
netstat -a -n | find /c "<ip:port>"

Example:
netstat -a -n | find /c "127.0.0.1:1935"




Thursday, October 7, 2010

Upgrade ASP.NET MVC 3 Preview to MVC Beta

I previously blogged about upgrading the TekPub ASP.NET 2 Starter Site from MVC2 to MVC3 Preview:
http://blog.cdeutsch.com/2010/08/upgrade-mvc2-to-mvc3.html

Today I upgrade that project to MVC3 Beta.

It was pretty easy. Biggest change was with Dependency Injection. IMvcServiceLocator no longer exits and has been replaced with IDependencyResolver.

My NinjectServiceLocator file has been replaced with NinjectResolver (thanks Jedidja from StackOverflow!) and looks like this:

namespace Mvc3Ninject.Utility
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Mvc;
    using Ninject;

    [System.Diagnostics.DebuggerStepThrough]
    public class NinjectResolver : IDependencyResolver
    {
        private static IKernel kernel;

        public NinjectResolver()
        {
            kernel = new StandardKernel();
            RegisterServices(kernel);
        }

        public NinjectResolver(IKernel myKernel)
        {
            kernel = myKernel;
            RegisterServices(kernel);
        }

        public static void RegisterServices(IKernel kernel)
        {
            //kernel.Bind<IThingRepository>().To<SqlThingRepository>();
        }

        public object GetService(Type serviceType)
        {
            return kernel.TryGet(serviceType);
        }

        public IEnumerable<object> GetServices(Type serviceType)
        {
            return kernel.GetAll(serviceType);
        }
    }

}

And then in the Global.asax.cs file I changed this line....

//// Tell ASP.NET MVC 3 to use our Ninject DI Container 
MvcServiceLocator.SetCurrent(new NinjectServiceLocator(_container));

...to...

//// Tell ASP.NET MVC 3 to use our Ninject DI Container 
DependencyResolver.SetResolver(new NinjectResolver(_container));

And that's that! Have fun coding!

Here's a complete working sample project based on my Code-First Entity Framework upgrade of the TekPub Starter Site.

Sunday, September 5, 2010

Save Web Page as PDF on iPad then Annotate

I was viewing the schedule for Twin Cities Code Camp on my iPad this morning and wanted to circle the talks I was interested in. I quickly realized I wasn't going to be able to do this without some help. After some research here's what I came up with.
  1. Configure your iPad to sync Bookmarks with one of your browsers (I picked Safari)


  2. Add this Bookmarklet for converting web pages to PDF from PDF Download to Safari (originally found this tip on "The Heat Web")

  3. Sync your iPad with iTunes to get the Bookmarklet onto your iPad.
  4. Install one of the iPad PDF Annotation apps. I picked Noterize by Robert Stretch. I also looked at iAnnotate which I'll try next if I don't like Noterize.
  5. Browse to the web page you want to annotate, open your Bookmarks, and select Save Page As PDF
  6. Unfortuantely I had to hit stop and then "refresh" in Safari to get PDF Download to work (your results may vary)
  7. PDF Download will eventually open the page as a PDF. (it can take a while)
  8. Copy the URL of the PDF.
  9. Open Noterize, go to the root of your Notes and click Import PDF/PPT. Select the Web Browser option and paste in the copied URL.
  10. The PDF is now available to annotate.
I also highly recommend using Dropbox so you can easily save your annotations and view them on your computer or in other apps that support Dropbox. You can use Dropbox to easily get PDFs into Noterize from your computer as well.

Noterize does not automatically save your PDF's to Dropbox. You'll need to Share the PDF within Noterize after it has been annotated and then upload to Dropbox.

I'd love to hear from you! Leave a comment or reply to @cdeutsch on Twitter if you find something useful.



Saturday, August 21, 2010

Code-First Entity Framework ISession for Tekpub MVC Starter Site

I've been using a lot of the ideas/code in Tekpub's MVC Starter Site for a personal project of mine. For better or worse the features of .NET 4.0 and MVC keep rapidly changing. I wanted to use both MVC 3 Preview 1 and Code-First Development with Entity Framework 4 so I've taken the time to upgrade the MVC Starter Site to support both of these features. You can download my working project here. See this post for more info on the headache to upgrade to MVC 3.

IMO it took way too much time to do this but a lot of it is probably due to my inexperience with MVC and in particular Dependency Injection.

Below is the class I added in order to use Code-First in the MVC Starter Site. The class inherits ISession. At first I tried to get access to the underlying ObjectContext so I could just use the already written EFSession class. I couldn't get this to work and then luckily I took a step back and realized I didn't need it to. I still haven't figure out how to do a working IReadOnlySession.cs but for my needs it won't be an issue at this time.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Objects;
using System.Data.Objects.DataClasses;
using System.Data.Metadata.Edm;
using System.Data.Entity;

namespace Web.Infrastructure.Storage
{
    public class EFCFSession : ISession
    {
        DbContext _context;
        public EFCFSession(DbContext context)
        {
            _context = context;
        }

        
        public void CommitChanges() {
            _context.SaveChanges();
        }

        public void Delete<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T: class, new() {

            var query = All<T>().Where(expression);
            foreach (var item in query) {
                Delete(item);
            }
        }

        public void Delete<T>(T item) where T: class, new() {
            _context.Set<T>().Remove(item);
        }

        public void DeleteAll<T>() where T: class, new() {
            var query = All<T>();
            foreach (var item in query) {
                Delete(item);
            }
        }

        public void Dispose() {
            _context.Dispose();
        }

        public T Single<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) where T: class, new() {
            return All<T>().FirstOrDefault(expression);
        }

        public IQueryable<T> All<T>() where T: class, new() {
            return _context.Set<T>().AsQueryable<T>();
        }

        public void Add<T>(T item) where T: class, new() {
            _context.Set<T>().Add(item);
        }
        public void Add<T>(IEnumerable<T> items) where T: class, new() {
            foreach (var item in items) {
                Add(item);
            }
        }
        public void Update<T>(T item) where T: class, new() {
            //nothing needed here
        }
    }
}

Another interesting thing I found when doing this is that I couldn't use the same DB for the ReportingDB DbContext and the SiteDB DbContext without Code-First wiping the other out. Not a huge deal at the moment but could become an issue down the road.

So far I'm loving the idea of using POCO model classes and can't wait to try combine them with the MVC 3 Model Validation improvements!