I took a long break from ORMs in my career: about 4 years. I was working on a reporting product, and an ORM is just the wrong tool for that. Before that, I worked with ORMs here and there: NHibernate and another ORM I don't even want to mention by name for fear of being associated with it in public. For some of my own projects like the next version of EZRep and this very blog site, I've switched to a so-called micro-ORM, specifically Dapper.
Now that I'm back to consulting, I'm back into the ORM game. This time, it's Entity Framework. Early in this project (that uses EF), I've been experiencing quite a bit of frustration. It seems like I'm swimming upstream while jumping through hoops to accomplish really simple things that wouldn't take nearly as much work in Dapper. Maybe this is just me getting back into the habit, but it's also got me thinking: why do I really need a "full" ORM anymore? Abstraction and indirection are important tools, but perhaps full ORMs aren't the least leaky abstraction anymore in many cases?
Yes, a full ORM is sometimes RTRJ. However, I think micro ORMs (Dapper, PetaPoco, Massive) and document databases (Raven, Couch, Mongo) have taken a big bite out of the pool of possible use cases, and that a lot of full ORM and/or RDBMS usage in projects is really used not because it's the best tool, but because of the sheer momentum of the status quo. Imagine a snapshot of a Venn diagram taken 5 or 10 years ago compared to one I made up to represent a snapshot of today.
There might be missing bubbles like "RDBMS + no ORM" or "Json string in text file", and the exact proportions are open to interpretation. But the point I'm trying to make is this: the number of use cases that are best served by RDBMS + full ORM is shrinking, and will continue to shrink.
Welcome to another "Weekly Concerns". This is a post-a-week series of interesting links, relevant to programming and programmers. You can check out previous Weekly Concerns posts in the archive.
- 50 Years of BASIC
- Why is it called "logging in"?
- I've been working with CKEditor recently, whereas most of my experience is with TinyMCE. I asked a question on Stack Overflow about manipulating the HTML in a CKEditor instance.
- Resizing images in your ASP.NET project? Check out ImageResizer. It's API and functionality had me crying tears of joy compared to the cludgy mess of System.Drawing and System.IO stuff I was using.
- Equality in JavaScript sure is cuh-razy!
If you have an interesting link that you'd like to see in Weekly Concerns, leave a comment or contact me.
It's the last part of the series, and I just thought I would wrap things up by talking about unit testing the aspect. I wrote a whole chapter in my book AOP in .NET on unit-testing of aspects. I also was honored to do a live webinar with Gael Fraiteur on unit testing with PostSharp, in which he has a different view on aspect unit testing, so I recommend you check that out too.
I'm not straying too much from the book in this example, so if you're familiar with that chapter, there's not much new for you here.
My plan is to not test the PostSharp aspect directly, but instead have the aspect delegate just about all the functionality to a plain old C# class that's easier to test. The old adage of solving a problem by adding another layer of indirection applies. Doing it this way makes my aspect easier to test, further decouples my application from a specific framework, but does require some more work, so it's not like it's a magic cost-free approach to testing either.
Here is the aspect, all tightly coupled to PostSharp:
And here is my refactored code, which you'll notice results in a higher number of classes, but it is ready to unit test. You may also notice that some of the logic is a bit different than the above aspect. This is because I found some issues and bugs while I was writing unit tests. So, hooray for unit tests! Also notice the "On" static field. I touch on this in the book, but this flag is merely there so that I can unit test the actual services while isolating them from the aspect. Since PostSharp uses a compile-time weaver, this is necessary. If you are using a runtime AOP weaver like Castle DynamicProxy, that's not necessary.
I'll even show you some of my unit tests, which I've written in mspec and JustMock Lite with the help of Sticking Place for testing the SqlException stuff.
Notice that I moved the Exceptionless-specific code behind an interface (IExceptionLogger) as well, to further decouple, improve testability, and also because Exceptionless is just the sort of thing that might actually be swapped out somewhere down the line. I've left out the details of that implementation because it's quite trivial.
So now you are caught up to today: this is the aspect I'm actually using and deploying to a staging site for my cofounder and our designer to use and test. And so far I must say that it's working quite well, but things could always change when it actually hits real customers, of course.
I like using mspec to write tests. Here are a couple of tips for getting the most out of mspec with ReSharper.
Tip 1: ReSharper can run mspec tests!
If you use NuGet to install mspec, then look in your /packages/Machine.Specifications.x.y.z/tools folder. You'll see a batch file like InstallResharperRunner.8.2.bat. This will install the mspec ReSharper test running plugin for you. Then those friendly little green test runner dots will show up in Visual Studio.
Tip 2: Use a ReSharper live template to write mspec tests faster
Mspec uses perfectly valid C#, but it's a bit unusual looking and sometimes hard to type all the correct symbols, especially when you are in a hurry. So I created a live template in ReSharper to automatically create the mspec skeleton for me. In Visual Studio, go to Resharper -> Templates Explorer -> C#. Click the New Template icon. I gave mine a shortcut of "mspec" and all I put in it is:
If you've not used ReSharper live templates before, then you're missing out. Just type the shortcut name and hit the tab key and voila.
(Off-topic side note: This is not a paid advertisement, but since I've created a couple of them now, I thought I'd mention that animated GIFs like above are super easy to create with Camtasia Studio from TechSmith, which is also a great tool in general for creating screencasts as well).
May 13th is .NET Day at manning.com. My book (AOP in .NET) is featured as part of this Deal of the Day.
The offer also applies to:
- C# in Depth, Third Edition by Jon Skeet
- Fast ASP.NET Websites by Dean Alan Hume
- Windows Store App Development by Pete Brown
- HTML5 for .NET Developers by Jim Jackson and Ian Gilman
- Metaprogramming in .NET by Kevin Hazzard and Jason Bock
- Dependency Injection in .NET by Mark Seemann
- ASP.NET 4.0 in Practice by Daniele Bochicchio, Stefano Mostarda, and Marco De Sanctis
- F# Deep Dives by Tomas Petricek and Phillip Trelford
- And C++ Concurrency in Action by Anthony Williams
The deal will stay active for about 48 hours. (They let it run a little longer than a day to account for time zones). So get yourself some books!
Use promo code dotd051314au.