Monthly Archives: May 2013

My New Pluralsight Course: Automated Testing for Fraidy Cats Like Me

Pluralsight has just released my newest course: Automated Testing for Fraidy Cats Like Me

This is the 43 second introduction to my new course.

Watch the full course: Automated Testing for Fraidy Cats Like Me

It was also the 500th course to be added to their library, so a nice milestone to be associated with!

It’s a story I’ve been wanting to tell for a long time.

The course is only 90 minutes long. It is aimed at developers who have been thinking “I should learn about testing” but have the impression that it’s such a big investment that there’s no point unless you are able to go “all in” so to speak. I was of that mind for a long time. I’m finally benefitting from testing and I still have not gone all-in.

You don’t believe me? As a VB4/5/6 developer my “tests” were either done in the immediate window or just by running the app. I couldn’t test logic until I’d built a UI to execute it.

I have worked with numerous clients where we will build a small method and then the client says “okay let me add some buttons and text boxes to my form to see if that logic works”.

Yep, I’ve been there and done that.

I finally got to an AHA! moment where I saw that automating some of those tests was a huge win for me.

So my goal for the course is to help developers like me (whether you are new to development or have been programming for a long time, like I have) get to that AHA moment. At the same time, I provide a “lay of the land” so to speak. I show a little bit about using some of the most common techniques. I write and run some code (all in Visual Studio and .NET but this should be relevant to developers working with any technology). And I also provide an overview of some of the variety of techniques and tools available.

This is really designed to give developers enough of a look at testing to determine if it’s something they really *do* want to learn and then enough of the basics to allow them – you? – to dive in to the many other learning resources that exist for automated testing.

Watch the full course: Automated Testing for Fraidy Cats Like Me

Need a free Pluralsight trial so you can watch my courses? Send me an email through my contact form.

Entity Framework Power Tool Tips to View Model (A Feature I Depend On!)

When defining models with Entity Framework’s Code First and a DbContext class, I find the “View Model” feature of the Entity Framework Power Tool completely indispensable. Without it, I only *think* I know what I’ve told EF my model should be but I don’t really know how Code First will interpret the information I’ve provided (class structures, DbSets, configurations).

For example, I may have a set of classes but a DbContext that only specifies a single DbSet:

    public class CustomerServiceContext : DbContext
    {
        public DbSet<Customer> Customers { get; set; }
    }

But because of relationships from Customer to other types and their relationships to even other types, using it’s base rules (convention) Code First interprets your entire model as

image

Another common point of mapping confusion is with relationships. Being able to visualize how Code First interprets the model can show me where, for example, I’ve attempted to define a 1:1 relationship but in fact, I’ve defined a 1:0..1 which will really create problems down the road in my application.

I use the tool to show me where I haven’t configured things properly and fix my mappings (via convention or configurations) until the visual model aligns with my intention.

So I really can’t live without this tool and I show it to everyone I can.

Currently the tool is something you install as an extension to Visual Studio. It’s called Entity Framework Power Tools Beta 3 and it works even wtih the latest update to VS (Visual Studio 2012 Update 2).

While there are other features, with the tool installed, you can point to any class that inherits from DbContext, right click and see “Entity Framework” on the context menu. Select Entity Framework and you’ll see four options. My focus is on the first of these “View Entity Data Model (Read-only).

SNAGHTMLb0b6d0b

But half the time I show it, it doesn’t work and it’s always something I’ve done wrong and forgotten about.

Designer Problems != Modeling Problems

If there is a problem with your model, you’ll get a helpful error message in the output window. This is about what’s wrong with your mappings. One common one is you tried to create a one to one mapping but haven’t given code first enough detail (e.g. it needs to know which is the principal and which is the dependent).

The focus of this blog post however is problems I encounter with getting the tool to perform it’s task.

Most Common Reason that the Designer Fails: Can’t Find the Connection String

Even though the designer is not going to touch the database to generate the view, it still needs to have access to a connection string.

But when it can’t find it, it doesn’t always tell you that it can’t find the connection string.

Typically I get an error message that says: “sequence contains no matching element”.

Another is “Exception has been thrown by the target of an invocation”.

Designer Looks for  the Connection String in the Startup Project of a Solution

This is the A#1 reason that the designer won’t work. It’s a bit of a pain because you don’t really want to set things up for the designer, but for debug mode. It doesn’t matter if the startup project is executable. It just has to be some project with a config file and in the config file, valid connection information ( a connection string or connection info in the Entity Framework section) that EF will look for.

Designer Will Not Look Inside Solution or Project Folders

This is my A#2 reason the designer won’t work. I like to organize my solutions. I organize projects into solution folders and I organize code into project folders. If you config file is in a folder, the designer won’t find it.

My Typical Setup for Success

So I have a separate project in my solution that is NOT inside a folder just to help me use this critical designer tool. Thanks to Vaughn Vernon for helping me find a better name for the project than I had been using that makes it very clear that this has nothing to do with my application at all.

image

So when I need to view a model, as long as I remember to set that project as the startup project in my solution, the designer is able to do it’s job. Here is an example of using this for a context that doesn’t even inherit DbContext directly. It inherits from my own class “BaseContext” which inherits from DbContext. The designer is able show me the visual model even with this extra abstraction.

SNAGHTMLb07a092

 

Another Weird Error that Is Not About the Connection String: “A constructible type …”

Thanks to Ethan in comments for pointing out this one. I actually had it the day *before* I wrote this post and had forgotten (short term memory issues…old age…excuses excuses 🙂 ).

‘A constructible type deriving from DbContext could not be found in the selected file’.

This was misleading because like Ethan, the fact that I was deriving from BaseContext led me to believe that *this* was the cause for not being able to find the DbContext type. But changing CustomerServiceContext to inherit DbContext directly did not fix my problem.

I found this StackOverflow thread about this error message with a response from Brice Lambson of the EF team that suggested an Visual Studio extension was the issue. I had just added two extensions for spell checking my code and comments. One was HTML Spell Checker (from Microsoft), the other was Multi Language Spell Checker (also from someone at Microsoft). So I thought I would disable one at a time to see if one was the culprit. I happened to pick HTML Spell Checker to disable first and voila, the View Entity Data Model tool worked again (after a VS restart). Fortunately the second extension is the one I had really wanted anyway.  I believe that this problem will be resolved by the time we get the tools integration in EF6.

My Current Pluralsight Courses on Entity Framework

Pluralsight - Hardcore Developer Training

Course Released Hrs:Min
Entity Framework 4.0 By Example Aug 2010 2:19
Entity Framework and Data Models Nov 2010 1:32
Designer Supported EDM Customization Jan 2011 2:05
Querying the Entity Framework Feb 2011 1:23
Entity Framework 4.1 – Code First Jun 2011 1:59
Entity Framework 4.1 – DbContext Sep 2011 1:21
Data Layer Validation with EF 4.1+ Feb 2012 1:54
Entity Framework Code First Migrations  Mar 2012 1:10
Getting Started with Entity Framework 5 Mar 2013 4:23
Entity Framework in the Enterprise  Sep 2012 3:16

 

Need a free 30-day trial for Pluralsight to check out my courses? Send me a note on my contact form

CodeStock Keynote and Maybe Some Sessions, too

image I’m thrilled to have been invited to deliver the keynote talk at CodeStock 2013 in Knoxville this July. I’ll be talking about how Domain Driven Design (DDD) has inspired me a-new after almost 25 years of software development. And I hope to inspire others to take a closer look at DDD as well.

In addition to the keynote, I submitted two conference sessions and sessions are selected by a voting process where registered attendees vote for the talks they want to hear. Such a cool idea.

I feel a little guilty submitting sessions since I already will be presenting the keynote and there are so many awesome abstracts including some on EF and DDD.

Here are the two talks I submitted if you’re planning to attend CodeStock and interested in hearing me go on about them. But if that’s the case, you’ll need to go vote.

 

Entity Framework in Core-Business Applications

Demo-ware is great for getting your head around a new topic but we’ve been seeing Entity Framework demo ware for 5 years now. You’re probably not writing an application to manage your music collection or composing yet another Twitter client. You need to build complex applications related to the Core-Domain in the enterprise and therefore you need to know how to integrate Entity Framework in serious and decoupled architectures. In this session we’ll look at breaking up your domain-models following the Domain Driven Design Bounded-Context pattern, splitting and defining entities and aggregates properly in the different domain models and implementing repositories and units of work that are relevant to EF DbContext and DbSets, in the data persistence infrastructure layer. You’ll also see how to build automated tests around code that involves Entity Framework, whether they be integration tests or strictly unit tests where Entity Framework seems to get in the way.

 

Automated Testing for ‘Fraidy Cats Like Me

Sometimes learning about Automated Testing from the testing gurus might be a little daunting. It’s hard for them to relate to the fears that developers who’ve never used automated tests before – especially those of us who have been coding for years and years. This will not be a "drink the unit testing or TDD Kool-Aid" session. Instead, I will share revelations that have helped me ease myself and clients into automated testing. Not only has it benefitted my development process, but looking at code from the perspective of "can I test this?" has resulted in a huge improvement in how I design my application architecture. Come dip your toe in the water and see if automated testing might be for you.