Dan Newcome, blog

I'm bringing cyber back

Archive for January 2011

MockCrm for testing MS CRM code

with one comment

I’ve been debating whether to release some code I’ve been playing with for a while for unit testing Microsoft CRM projects. I had hacked something together back in CRM 4 to test a project that was slow and tricky to test manually against a real CRM installation. But there wasn’t good test coverage on this code and there were a lot of cases that it didn’t handle.

I revisited the idea of creating a CRM mock service this week and created a version for CRM 2011 and it seems to be better suited for the new API.

The mock lets you set up some data at the top of the test like this:

m_service = new MockCrmService();
Entity de = new Entity();
de.LogicalName = "mydynamic";
de["prop1"] = "foo";
Guid deID = m_service.Create( de );

Then we can run a test on the fixture that we created in the code above like this:

	[FestTest]
		public void TestDynamic() {
			QueryBase query = CrmQuery
				.Select()
				.From( "mydynamic" )
				.Where( "mydynamic", "prop1", ConditionOperator.Equal, new object[] { "foo" } ).Query;

			EntityCollection bec = m_service.RetrieveMultiple( query );
			Fest.AssertTrue( bec.Entities.Count > 0, "found more than zero entities" );
		}


Note that these examples use my micro .NET unit testing library Fest and my CrmQuery DSL for building CRM queries.

The reason I’m debating releasing this code is that I don’t know if I can spare the time and effort to polish this up. On the other hand I’ve benefited from Phil Haack’s experiments in mocking .NET HttpContext and mocking email servers. I guess sometimes it is useful to just see the approach that others are trying when it comes to testing. You don’t always need the polish. What do you guys think?

Advertisements

Written by newcome

January 27, 2011 at 1:59 am

Posted in Uncategorized

Code reading: ASP.NET MVC

with 3 comments

I’ve been looking around for a good RESTful API framework for .NET to possibly implement an API design that I’ll be working on soon. One of the things I’m considering (other than WCF) is using ASP.NET MVC as a Web service. I’m not going to go through the whole MVC codebase here, but I was digging through it to find out how something was done so I thought I’d write up my notes here as a new installment of my Code Reading series.

As powerful as code metadata and reflection is in the .NET framework, there are often things that I find myself wanting to do that turn out to be awkward (and therefore almost not worth the effort) or just impossible without resorting to metaprogramming techniques like emitting IL or rewriting and compiling code files on the fly. One of the things that I always thought should be pretty straightforward was adding logging or error handling to a method using the .NET attribute mechanism.

For example consider the following method:

public List<Employee> GetEmployees() {
    // get empoyees here
}

What I’d like to do is add simple trace logging to the method by adding an attribute to it like this:

[CallTrace]
public List<Employee> GetEmployees() {
    // get empoyees here
}

where CallTrace causes a log entry to be written when the entry and exit points of the method are reached, which might look something like this:

Starting logging...
2011-01-10 TRACE - Entered GetEmployees()
2011-01-10 TRACE - Exited GetEmployees()

As far as I know there is no way to cause an attribute applied to a method to hook into the method without using reflection somewhere within the method or externally to search out any attributes that may be applied, which unfortunately falls under the aforementioned category of “awkward and more trouble than it’s worth”. The only solutions that I’ve come up with that avoid adding code to the methods themselves involves rewriting the class using reflection and emitting a new class with the new code inserted. In case you are wondering that is a big pain.

So, after having gone through this thought exercise I was surprised today when looking through some ASP.NET MVC documentation that mentioned that you should be able to do just what I was trying to do above using a mechanism called an ActionFilter. The idea is that you can create an attribute that implements ActionFilterAttribute and then you can apply it to a controller method and have actions trigger when the method is called. Here is an example that I found here (modified for brevity):

     public class LogActionFilter : ActionFilterAttribute
     {
          public override void OnActionExecuting( ActionExecutingContext filterContext )
          {
               Log("OnActionExecuting", filterContext.RouteData);       
          }

          private void Log(string methodName, RouteData routeData)
          {
               Debug.WriteLine( routeData.Values["action"], "Action Filter Log" );
          }

     }
}

This attribute could be applied to a MVC Controller action like so:

[LogActionFilter]
public List<Employee> GetEmployees() {
    // get empoyees here
}

So my goal was to read through the MVC source to find out how the authors accomplished this feat.

Browsing through the code I found the following promising file:

ActionMethodSelector.cs

      private void PopulateLookupTables() {
            MethodInfo[] allMethods = ControllerType.GetMethods(BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public);
            MethodInfo[] actionMethods = Array.FindAll(allMethods, IsValidActionMethod);

            AliasedMethods = Array.FindAll(actionMethods, IsMethodDecoratedWithAliasingAttribute);
            NonAliasedMethods = actionMethods.Except(AliasedMethods).ToLookup(method =&gt; method.Name, StringComparer.OrdinalIgnoreCase);
        }

It turns out to be not a bad start for digging into the code. We see that ASP.NET MVC is using reflection to build a list of available action methods and store them in a lookup table. This is probably some kind of dispatch table, so let’s keep looking through code files.

Next up we have a dispatcher class.

ActionMethodDispatcher.cs


       private static ActionExecutor GetExecutor(MethodInfo methodInfo) {
            // Parameters to executor
            ParameterExpression controllerParameter = Expression.Parameter(typeof(ControllerBase), "controller");
            ParameterExpression parametersParameter = Expression.Parameter(typeof(object[]), "parameters");

            // Build parameter list
            List parameters = new List();
            ParameterInfo[] paramInfos = methodInfo.GetParameters();
            for (int i = 0; i &lt; paramInfos.Length; i++) {
                ParameterInfo paramInfo = paramInfos[i];
                BinaryExpression valueObj = Expression.ArrayIndex(parametersParameter, Expression.Constant(i));
                UnaryExpression valueCast = Expression.Convert(valueObj, paramInfo.ParameterType);

                // valueCast is &quot;(Ti) parameters[i]&quot;
                parameters.Add(valueCast);
            }

            // Call method
            UnaryExpression instanceCast = (!methodInfo.IsStatic) ? Expression.Convert(controllerParameter, methodInfo.ReflectedType) : null;
            MethodCallExpression methodCall = methodCall = Expression.Call(instanceCast, methodInfo, parameters);

            // methodCall is &quot;((TController) controller) method((T0) parameters[0], (T1) parameters[1], ...)&quot;
            // Create function
            if (methodCall.Type == typeof(void)) {
                Expression lambda = Expression.Lambda(methodCall, controllerParameter, parametersParameter);
                VoidActionExecutor voidExecutor = lambda.Compile();
                return WrapVoidAction(voidExecutor);
            }
            else {
                // must coerce methodCall to match ActionExecutor signature
                UnaryExpression castMethodCall = Expression.Convert(methodCall, typeof(object));
                Expression lambda = Expression.Lambda(castMethodCall, controllerParameter, parametersParameter);
                return lambda.Compile();
            }
        }

This code builds up a lambda expression for later execution. I don’t see any places where we would be injecting code from the attributes however. I could be missing something here, but let’s move on for now. The next thing I saw was an action invoker. Let’s take a look at an excerpt from this class (edited for brevity):

ControllerActionInvoker.cs



      public virtual bool InvokeAction(ControllerContext controllerContext, string actionName) {
            ...

            ControllerDescriptor controllerDescriptor = GetControllerDescriptor(controllerContext);
            ActionDescriptor actionDescriptor = FindAction(controllerContext, controllerDescriptor, actionName);
            if (actionDescriptor != null) {
                FilterInfo filterInfo = GetFilters(controllerContext, actionDescriptor);

                try {
                    AuthorizationContext authContext = InvokeAuthorizationFilters(controllerContext, filterInfo.AuthorizationFilters, actionDescriptor);
                    if (authContext.Result != null) {
                        // the auth filter signaled that we should let it short-circuit the request
                        InvokeActionResult(controllerContext, authContext.Result);
                    }
                    else {
                        if (controllerContext.Controller.ValidateRequest) {
                            ValidateRequest(controllerContext);
                        }
                        else {
                            ValidationExcludeAllAction();
                        }

                        IDictionary parameters = GetParameterValues(controllerContext, actionDescriptor);
                        ActionExecutedContext postActionContext = InvokeActionMethodWithFilters(controllerContext, filterInfo.ActionFilters, actionDescriptor, parameters);
                        InvokeActionResultWithFilters(controllerContext, filterInfo.ResultFilters, postActionContext.Result);
                    }
                }
                ...

                return true;
            }

            // notify controller that no method matched
            return false;
        }

Here we have the smoking gun. We get filters that were applied via attributes:

       FilterInfo filterInfo = GetFilters(controllerContext, actionDescriptor);

then we use those filters for authentication, and later we apply all remaining filters with this line:

       ActionExecutedContext postActionContext = InvokeActionMethodWithFilters(controllerContext, filterInfo.ActionFilters, actionDescriptor, parameters);

Overall, pretty cool but not what I was hoping to find. I wanted to see some clever way of making the classes self-aware of their attributes rather than an external invocation mechanism. Hopefully you enjoyed the ride.

Written by newcome

January 11, 2011 at 1:29 am

Posted in Uncategorized

Using Haskell with Microsoft .NET

leave a comment »

I’ve been learning some Haskell recently since I ran into people at the Hacker Dojo who are putting on Haskell events. After running through the basic tutorials the first thing I like to do is to try to get it working with another platform that I already know well. I’ve done this with Ruby in the past, and it is a good way to learn your way around the new language.

The first step is to look around to see what options we have for getting Haskell to work with .NET. There isn’t (to my knowledge) a native .NET compiler for Haskell, but there are several projects that aim to provide interoperability in other ways. There is a list here of projects that we might be interested in, with the notable exception of Salsa, which I found independently.

The hs-dotnet project is what I had the most success with, so that is the approach that I’ll feature here. The project appears to be complete enough to experiment with and has worked for what I’ve been doing so far, but there is very little documentation other than some sample code that ships with the distribution package. If you haven’t learned a little Haskell already it could be tough going initially, even with deep knowledge of the .NET platform. I’m hoping to write a later post on how to approach the basics. In the meantime let’s look at how to get the environment set up.

I’m assuming that we are starting with a fresh Haskell Platform installation on Windows. Hs-dotnet has a package hosted on Hackage, so we can use Cabal to install it. Once the Haskell platform has been installed, the cabal binary should be in the path. Note that we’ll need administrator privileges to install hs-dotnet, as the installer will put the bridge library in the GAC.

(as Administrator)
> cabal install hs-dotnet

Once we have the package installed we can test things out in the GHC interpreter. The examples are included in the package archive, so grab it from here and untar it first. Load up the environment variable test code like this:

> cd hs-dotnet\examples
> ghci env.hs

Which should give us a Prelude prompt with some code in scope that uses the .NET runtime to read the value of a system environment variable:

Prelude Env>

If the code loaded with no errors we can test it by trying to read the name of the logged in user:

Prelude Env> getEnv "USERNAME"

In my case, I had an error when I tried to run any code that called out to the .NET runtime:

*** Exception: NET bridge error:
 Location: static-method
 Source: System.Environment.GetEnvironmentVariable
 Kind: COM error: 0x80070002 The system cannot find the file specified.
 Description:
 Method type: (S)S

I’ve been working with Windows for a pretty long time and COM errors like this often are the result of a library that failed to be registered using regsvr32. The installer should have taken care of all of this so I took a look in the Cabal package to see what the installer was doing, so maybe I could manually set things up. The setup script is called Setup.hs and is in the root of the Cabal package that we just downloaded from Hackage to find the sample code.

Taking a look in Setup.hs, we can see that the library that we are interested in is called HsDotnetBridge.dll and that it both gets installed into the GAC and is registered as a COM component using regasm.exe.

assembly = "bridge\\HsDotnetBridge.dll"
...

gacInstall args iflags pd lbi = do
  dir <- getCurrentDirectory
  putStrLn "Registering hs-dotnet Assembly.."
  ex <- rawSystem (dir </> "gacInstaller.exe") [dir </> assembly]
  case ex of 
    ExitSuccess{} -> putStrLn "OK!"
    ExitFailure{} -> putStrLn ("Unable to register assembly...; try doing it manually via 'gacutil /i'..")
  putStrLn "Registering Assembly COM Interop classes.."
  ex <- rawSystem (dir </> "regasm.exe") [dir </> assembly]
  case ex of 
    ExitSuccess{} -> putStrLn "OK!"
    ExitFailure{} -> putStrLn ("Unable to register assembly...; try doing it manually via 'regasm'..")

I looked in the .NET Global Assembly Cache and didn’t see this assembly installed there, so I did it manually like this:

"C:\Program Files\Microsoft.NET\SDK\v2.0 64bit\Bin\gacutil.exe" -i HsDotnetBridge.dll

Re-running the test as above now worked as follows:

Prelude Env> getEnv "USERNAME"
"dan"

Getting the environment set up is just the first step in calling your .NET code from Haskell. In order to call any arbitrary code, we’ll have to use a code generation tool to create Haskell wrappers for it. This is an unfortunate extra complexity but it is relatively easy since the supplied tools will automatically handle any dependencies for you, provided that they can be found in the .NET GAC. Stay tuned for more on this.

Written by newcome

January 10, 2011 at 1:15 am

Posted in Uncategorized

JS1k Xmas Edition post-mortem

leave a comment »

I submitted an entry for the JS1k Xmas competition but I never posted anything about it here. I was very busy at the end of the year but one night I was feeling exhausted with everything so I sat down and started hacking around with some ideas for a Javascript game. An hour later I had the first inkling that maybe I’d be able to pull together something for the competition.

What I had hacked together didn’t really have a Christmas theme though, so I had to get creative with the back story to make it work. Snowmen are just a few circles and easy to draw, but seemed kind of boring. Everyone loves zombies so I just turned the snowmen green and called it good. I thought that anything with zombies in it would win for sure but I was wrong. So wrong, in fact, that I didn’t even make it into the top 10. There are a lot of things I think could have made my entry better.

More detailed characters (zombie snowmen)

I thought that it wouldn’t be possible to make my zombies any more detailed and still hit the 1k limit. However, judging by some of the other entries, this seems to be incorrect. If I had put in some more details – maybe something a bit humorous – the game would have had a much greater appeal.

Better hit detection

The way I initially created the snowman animation was to start with the main larger circle that forms the snowman body. All other coordinates on the character are based from this origin point. The point would roughly correspond to the belly button of the snowman (if snowmen had belly buttons). This was lazy on my part, since it would have been more intuitive to present either the head or the entire character bounding box as the hit zone. This probably caused some confusion for players that tried to register a kill without reading the directions.

Satisfying kill animation

A large part of the appeal with a mindless shooter game, even one with otherwise non-lethal snowballs (which are like silver bullets to these undead snowmen) is a nice animation of the enemy’s demise. Something as simple as having the head fall off before removing the enemy from the playing field could have helped.

A way to die and lose the game

Since this was a quick demo, I didn’t think to add something in the game loop that checked how close a zombie snowman was to the player. This would have been really easy to add and could have made it a real game (since you could actually die). There is a score counter, but it is relatively meaningless since all you need to do is play the game longer to get a higher score since you can never die.

Multiple levels of play

There is only one difficulty level in the game as it stands. I could have increased the speed of the snowmen or maybe how often a new one spawned, which would have resulted in more snowmen as time went on. This, coupled with the snowmen actually killing you if they got to close could have made a better game.

Of course it’s possible that none of this would have helped me. Maybe zombie snowmen just aren’t going to cut it. It has been a while since I wrote the game now, so I don’t remember all of the things I was thinking at the time. Hopefully I’ll have the sense to look back at this for the next JS1k competition.

Written by newcome

January 7, 2011 at 7:56 pm

Posted in Uncategorized