Archive

Archive for March, 2009

A Profiler for the Entity Framework - Proposed

March 22nd, 2009 Simon Segal 1 comment

The .ToTraceString() method and the SQL Server Profiler are just too disconnected to provide me with a solution to analysing and logging the SQL Query output produced by my Entity Framework code. To change that, I am currently working on a profiler for the Entity Framework. Of course the inspiration to start on this project came from watching what Ayende was doing with NHProf (which is incredible), however I should point out that this project does not seek in any way nor set out to accomplish the same goals, it is my attempt at building myself a usable tool for a job and if it can anyone else then that’s great too. Here is the list requirements I am starting with:

  • UI for presenting SQL Statements and contextual Entity Framework Meta Data from the ObjectContext that created the query.
  • Entity Framework ObjectContext’s will need to opt in for profiling.
  • Saving of session profiling data to disk in a format that can be re-read and displayed  for later analysis.

So as you can see, I have kept it a pretty small list with a view to getting the basics done ef_profiler_early_layoutquickly. I have already decided that this project will not attempt to profile .NET executables working sets, however for reasons that will limit my current approach, I will consider using the Profiler API for a later version of this tool. The current approach has the profiler listening for Entity Framework Models that wish to send it messages. How? A bit ASPECT Oriented code weaving and WCF messaging. I have already begun to layout the UI to get a feel for it.  Currently I can’t see it supporting ESQL, however whilst that is limiting (something the Profiler API could change for me), I haven’t yet need to use it in battle and will have to revert to the old methods when I do.

I am happy to take feature requests but I cant promise that I will implement them all. I do want to keep the first and early versions very lite. Very very lite. I Should have a first look posted in a few more days for everyone to have a look at.

Share/Save/Bookmark

API Documentation

March 20th, 2009 Simon Segal No comments

In life we are free to define the same thing in many ways. I recently of read two separate definitions for Routed Events and presumably written by the same author. It his / her preface the author remarked that you could define Routed Events in two ways, a functional definition and an implementation definition. I cant imagine a world where the implementation definition is really that useful can you? Here they are:

Functional definition: A routed event is a type of event that can invoke handlers on multiple listeners in an element tree, rather than just on the object that raised the event.

Implementation definition: A routed event is a CLR event that is backed by an instance of the RoutedEvent class and is processed by the Windows Presentation Foundation (WPF) event system.

docsI read the functional definition and I say to myself, wow that’s cool, I can see myself using that…..very useful…..etc, etc. I read the implementation definition and begin to think it’s time for a coffee and a cookie. I have put together quite a few NDoc, SandCastle, JavaDoc API library and help documents over the years and when I strike one myself that exclusively uses “Implementation” definitions like the one discussed here, my head wants’ to explode. So please, do your colleagues a favour, if your going to document things, then only include implementation definitions if you have a functional one to go with it. In the case of API styled documents such as those produced by tools such as NDoc and SandCastle, often it’s the API signatures and examples that adequately describe the implementation definition at any rate.

Share/Save/Bookmark

Categories: Communication Tags:

Specification Pattern in IronRuby

March 17th, 2009 Simon Segal No comments

The Specification Pattern is one that I admire and have used often and most recently to good effect with both LINQ To SQL and the Entity Framework. This little gem of a pattern came in very useful with both ORM’s to enable dynamic queries built off Expressions or to be precise Expression<Func<T>>. If you want to know more about these implementations, then look here.

More recently in my travels down the polyglot road, I wanted to give the pattern a run with IronRuby. And boy I wasn’t disappointed. Getting a specification together in IronRuby was noticeably quicker and easier however please note that it lacks the happy ORM by-product as detailed above. Best to dive straight in to the code:

class Specification

    def initialize(expression)
        @expression = expression
    end

    def Matches(match_object)
        @expression.call(match_object)
    end

end

And that’s all folks. Compared to C# it’s certainly less code and I can’t help but enjoy it’s brevity. Let’s add some spice with a user defined class to test with specifications.

class Customer

    def Name
        @name
    end

    def Age
        @age
    end

    def initialize(name, age)
        @name = name
        @age = age
    end

end

And now let’s test this baby out:

#pass some code to test if a given number is less than 200
spec = Specification.new(lambda {|num| num < 200})
num_to_spec_on = 123
number_is_less_than_two_hundred = spec.Matches(num_to_spec_on)
puts "It is #{number_is_less_than_two_hundred} " +
     "that #{num_to_spec_on} is less than 200"

#use a user defined class to test with a specification
cust = Customer.new("Simon Segal", 35)
spec = Specification.new(lambda {|cust| cust.Name == "Simon Segal"})
customer_name_is_simon_segal = spec.Matches(cust)
puts "It is #{customer_name_is_simon_segal} that the customer is Simon Segal"

#combining lambdas into more complex specifications
l1 = lambda {|num| num > 1}
l2 = lambda {|num| num < 100}
l3 = l1 && l2
num = 54
spec = Specification.new(l3)
number_between_1_and_100 = spec.Matches(54)
puts "#{number_between_1_and_100} #{num} is between 1 and 100"

 

The Result’s

Looking at this code, you will see that we have dialled up an explicit arithmetic specification to test a given numbers value is less than 200 followed by an evaluation of a Customer object, testing if it’s name is equal to my very own moniker. Finally, an example of combining lambdas to produce more complex specifications. Here are the results (I am still using Ruby In Steel, which I prefer to the other options and it’s still free).

iron_ruby_specification_pattern

Download my learning IronRuby Visual Studio Project and code

Share/Save/Bookmark

Creative Commons Attribution-ShareAlike 2.5 Australia
Creative Commons Attribution-ShareAlike 2.5 Australia