Search

Posts Tagged ‘programming’

I have recently been looking at OR/M solutions, most notably EF4 and NHibernate for a couple of reasons:

  • To familiarize myself with EF (I’ve never used it before)
  • To find the pros/cons to ultimately help decision makers to decide which is more suitable

As of now, I’ve only looked at EF4 (both standard and “code first” versions), and (Fluent) NHibernate. There are certainly other fine OR/Ms (LightSpeed, SubSonic, Castle ActiveRecord) that I may spike out in the future, but as for now I’m only working with the two heavyweights. I also did a Linq-to-SQL spike, but I probably won’t be blogging much about it.

I have been working on a sample MVC3 app called “SportsStore”, which is based on the SportsStore in Steven Sanderson’s book on MVC2. This app uses a Repository pattern for the data access layer, and has a Linq-to-SQL implementation as a sort of “default”. You can find this example app in my SportsStore project on GitHub in the master and/or develop branches. I’m also using StructureMap as an IoC container.

My idea was to implement the same repository interface (IProductsRepository) using each of the OR/M tools, with a branch for each, and use that as a basis for comparison. One of my goals was to keep the implementation details hidden as much as possible behind that IProductsRepository interface. If you want to skip ahead, you’ll see that there are three branches: ef4_spike, ef4_code_first, and fluent_nh.

A central goal of these posts is to solicit feedback. What am I doing wrong, what am I doing right, what could I be doing better? So please, be brutal, be honest, and be vocal.

I’ve been working with Razor some recently, and here are a couple of surprises that bit me.

razorwire.jpg

First, I had a block of JavaScript in a view, and I was getting a parsing error on the “less than” sign in a JavaScript “if” statement. The problem was that my JavaScript block was, itself, inside of a Razor block (a ‘using’ block actually), so Razor was having trouble parsing it. I just moved the JavaScript out of the block and Razor handled it fine. See: “Tag is missing a name” with Razor on Stack Overflow for more details.

Secondly, I was writing an HtmlHelper extension method. I put the namespace in the pages/namespaces Web.config block, but my Razor view didn’t seem to be able to find it. Apparently, Razor uses a different configuration block for this, which is kinda annoying. For details, check out Razor (MVC 3 RC) HtmlHelper Extensions Not Found on Stack Overflow. According to Marcin Dobosz (who left a comment on that SO page), this is not something they are going to fix/change. However, if you are using Razor, it looks like you can remove the standard pages/namespaces block from the Web.config.

On my Monodroid app, I have a list of portfolios. When a user clicks on that portfolio, they should be sent to a child activity to see information about the portfolio they clicked on. I already blogged about how to open a new activity, so now I’m going to blog about how I pass information to that activity (for instance, the name of the portfolio). Android calls this information “extras”.

I have a ListView with an ItemClick event already wired up. Here is the ItemClick event:

Again, the ClassName property is just something I cooked up to hold a string value (see above linked blog post). Notice the main difference is that I’m calling the “PutExtra” property on the intent. This is pretty much just a way to box information up and send it to the next activity. The “Extra_PortfolioID” property is, again, just a string, and an arbitrary string at that which acts as a “key”. I’m passing an ID (a long in this case) to the next activity as the “value”. (The coalesce operator is because my entity has nullable ints, but the -1 will never be used in this case).

Over in the PortfolioActivity, I simply use Intent.GetLongExtra(Extra_PortfolioID, -1); to get the ID number out back out of the intent object. I’m again using that arbitrary Extra_PortfolioID string as the key. The “-1″ is the default value, since technically the data is being serialized, but because of the way I’ve written the app, that value won’t ever be used. Then I can query my database with that ID or do whatever I need to do.

Here are the slides/links from my presentations at the CMAP Code Camp. This was a really good camp, and there were some great discussions, especially in the Euler session. I really appreciated CMAP inviting me out, and I would be happy to do it again sometime!

1. CakePHP

2. Eulering up your coding skills

I’m writing a Monodroid app that has a main “dashboard” screen and several other “child” screens. Here is how I wire up a button to start up one of the “children” screens:

Note that the static “ClassName” property is just a string, and my lame attempt at not repeating magic strings. The value is “monoStockPortfolio.AddPortfolioActivity”, which can be found in your AndroidManifest.xml.

The “intent” object can be used to pass information to the child screen, which I’ll show in a later post. By using “StartActivityForResult”, the user can click the “back” button found on all Android devices to leave the child screen (with the correct transition). Alternatively, here is how the user can get back without using the back button (i.e. they complete some task on the child screen):

Here I’m saving a new portfolio with a repository, but the key part is the last 3 lines. I’m creating another Intent object, but I’m using it to set a “result” (recall that I used StartActivityForResult to get here). And then I’m calling “Finish” to end the activity.

You can check out the full source code of this project as I go along at GitHub.

Bahaha, sucker! The sensationalist title sucked you in. I don’t hate Ruby at all! In fact, I love Ruby because of the things that it and it’s supporting community have helped to advance:

  • MVC on the web
  • Testing and test-driven development
  • SOLID principles
  • Delivering business value quickly
  • Pressuring other languages and frameworks to keep up

Not to mention some of the most important influences on my career have been Rubyists.

But here’s the thing: I don’t really use Ruby. Honestly, I just don’t really like the language. I’ve been through the Koans, I’ve written some (very helpful) automation scripts for my Linux media server, and some other stuff here and there. But I’ve never really felt comfortable with it. My mind works a certain way, and I don’t think Ruby aligns with it. If you are a Rubyist, that may seem shocking to you, because I read posts all the time that suggest the opposite: Ruby gives you more freedom, Ruby code looks like the way you think, Ruby has gems for everything, Ruby makes you popular and heals all wounds, etc. I’ve just not experienced that.

It’s like hearing all your friends rave about how great a restaurant is, and then actually going there and finding out everything is covered in onions. Sure, it’s great food, if you like onions. But I just can’t eat them. Don’t read too much into that metaphor, because I’m not sure what the “onions” specifically represent.

So, this isn’t to say I hate Ruby (despite the title), or that I’ll never use it. I believe that I should challenge myself and learn new things all the time, including Ruby. I believe in RTRJ. It’s just that Ruby just isn’t at the top of my list, and I probably wouldn’t apply for a position or take on a project that is Ruby focused. I’m just not a Rubyist, plain and simple.

Here are my slides from the Richmond Code Camp. I presented on db4o.

Download the slides

This is the last of the InterOp posts. Just a brief word on the “why”, since I keep getting that question :)

I would not recommend InterOp as a first choice for writing a feature, or probably even a second choice. However, there are certain IT shops that have a fear of 3rd party components (they’ve been burned before, or have NIH syndrome, etc) or a mandate against using OSS. Writing our own spell checker or rich text editor can be very time consuming, so if InterOp is on the table, it’s a viable alternative to writing it yourself. However, InterOp can be a deployment headache, unless you know that every user will have Office, and will have the same version. Okay, on with the show.

I was tasked with putting a rich text editor into a WinForms app, to handle the editing of email body templates. I was actually pretty shocked to find that there aren’t really any decent open source or free RTEs for WinForms (at least that I could find). I briefly toyed with putting TinyMCE into a webbrowser control, but after reading a few blog posts, I decided to give InterOp a whirl and use the Trident (MSHTML) engine to be a rich text editor.

The idea is basically to use a WebBrowser control, set the ‘document’ in that control to ‘edit mode’ and voila, HTML rich text editor. Then you can program buttons to send ‘commands’ to the document, such as ‘bold’, ‘italic’, ‘create link’, etc. I won’t post a code snippet here, because it’s a relatively big hunk of code. Instead, I created an open source project called YARTE, or “Yet Another Rich Text Editor”. I’ve tried to make it easy enough to use right out of the box, but extensible enough to add in custom functionality when necessary.

yarte_screenshot.png

So that wraps it up for InterOp, for now. These InterOp posts were born out of necessity and experience. Not because I’m an InterOp nut or a WinForms fan (because I am neither), but in the hopes that I can help developers in similar situations to the one I was in.

If you’re like me and you don’t have an IT department for your home network with rigorous security standards, you need to be even more vigilant when it comes to security, because you have no one to blame but yourself.

I’ve been using Secunia PSI (Personal Software Inspector) for some time now, and I’ve found it to be an excellent product for identifying out-of-date and insecure programs. Not only does it identify programs that need updated, but also how to update them, how to patch them, and even specific forum threads if I have a problem getting the update to take. It handles insecure, end-of-life, and even browser security. In “advanced” mode, I can see at a glance what needs updating, how serious the threat is, links to patches/updates, and links to the forum thread:

secunia_screenshot_smaller.jpg

(Note that screenshot was taken before I had run Windows Update on a new box).

It’s always surprising to me what Secunia is able to find that I wouldn’t have found on my own. For instance, PhpStorm (an excellent PHP IDE, by the way) comes with a copy of Java (JRE) for convenience, but the JRE gets updated frequently and PhpStorm doesn’t update the JRE on its own. I had no idea the JRE was there until Secunia found it and told me it was out of date and a 4/5 threat rating. Simple solution: delete the outdated JRE and PhpStorm was able to find the updated version on my system with no problem.

Problem 22 was pretty straightforward; the problem description lays out the algorithm. One elegant piece of Linq was: word.Sum(c => c – (‘A’ – 1)), which sums up each letter score of the word nice and compactly.

Problem 23 – given the upper and lower limits of all possible abundant sums, this problem was, again, straightforward given the algorithm description. The toughest part was getting it performant, which is why I used a lookup collection for the IsAbundant method. At this point, my MathHelper class is coming in very handy, so I may just release it as its own project one day (though I’m sure there are many other equally good Math libraries for .NET).

Problem 24 – I did solve this problem, but my solution was not fast enough. Once I got the answer, I looked to see how other Eulers were doing it. So, for this class, I have both my solution and the more correct, “Fast” solution in the code. I think later on, I eventually created a permutation class, so I may need to revisit this problem with that class.

Problem 25 – Another easy one with BigInteger. I used a KeyValuePair because I was curious to see the actual number in the sequence, not just the index.

Problem 26 – This one took a lot of research and thinking. I spent a lot time reading about Cyclic numbers and Primitive roots. The solution feels like kinda a cheat to me, but I really don’t see any way around it since “No simple general formula to compute primitive roots modulo n is known”. Fortunately, Wikipedia lists just enough of them for me to solve the problem.

Problem 27 – I came up with a handy Quadratic class for this problem. I think in this case, the problem sounds pretty complex, but I broke it down into individual parts the best I could, and everything fit pretty well together. This is also a good strategy if you are having performance issues, as it will be easier for a profiler to point out where exactly the bottleneck is (hint: it’s almost never where you think).

As always, you can view my code at CodePlex – feel free to submit criticisms/comments/patches there, or use the framework for your own Project Euler solution.