05/26 2012

If you are going to do something, do it right

I love this quote and I think it really fits in with what I’m doing with this blog, and in my everyday life. Taking pride in your work is the only way to ensure that it’s as good as it can be. Every day I like to feel that things are better than when I started. It’s oftern the little things that count the most and here I have the opportunity to explain why.

Comments
11/22 2011

Physics for games

I’m revisiting a lot of the physics and I predict trigonometry that was once a daily part of my life before graduating university.

I’m surprised by how much I’d forgotten and writing it down always helps you remember it. Especially when it’s on a blog and you can search for it later ;).

The system I’ll be focusing on is Rigid Body Dynamics so lets start with the basics.

Any input into the system should be in the form of an impulse, which is basically a force against a point on a body. If all the impulses act against the center off mass we can deal with it just like a simple force.

The formula for a force:

force = mass x acceleration

Which can be rewritten as:

acceleration = force / mass

Which is great because our input is a force and mass can be whatever fits for our simple system which gives us acceleration.

The next thing we need is the velocity formula:

velocity = acceleration x Δtime

Now for those of you that don’t know Δ or in in English Delta means change or in this case change in time.

You should keep track of your objects velocity and add to it whenever the body experiences acceleration.

So now we have velocity and here comes position which we also keep track off and add to.

Position formula:

position = velocity x Δtime

Now we can do all the way from the initial push to there the body ends up after a known change in time. The same also applies to angular forces and motion but that’s something I’ll get to later. To be honest I’m still trying to wrap my head around inertia tensors which you need to

Helpful links Dynamical simulation

Comments
10/18 2011

WP7 Mango Unit Testing Framework “Cannot find a Resource with the Name/Key typeNameConverter”

When trying to setup unit testing I was slightly dissapoited you couldn’t test a silverlight library using a normal test project. However you can use the testing framework by using the Silverlight 3 libraries here.

I wish I’d known that, instead I found a link to an older tutorial but got the nice error Cannot find a Resource with the Name/Key typeNameConverter and the only real match had a broken link. The solution was simple though, find the source and update to the mago compliant version.

Comments
09/27 2011

C# and casting

I see alot of people using the as keywork where they really just want to do a cast. Most of the time a cast will be what you want because it’s faster and will fail early when somthing goes wrong.

When should you cast? You should cast when you already know what type the object is. A cast also will **not ** fail your value is null.

var value = (Apple)appleDictionary[key];

When should you use as? When you don’t know the type of object.

bool makeA = true;
BaseClass baseClass = null;
baseClass = makeA ? new SuperClassA() : new SuperClassB();

SuperClassB val = baseClass as SuperClassB;
if (val != null) { //...

You will almost always be compairing the value to null after using as.

It’s important to know as does not replace cast in any way, always pick the one you need for your situation.

Comments
08/27 2011

IIS Background Thread Abort Exception

One of my applications creates a background thread on startup which executes a series of tasks at a regular interval. However I get a ThreadAbortException after my application pool is recycled.

The problem is simply IIS is aborting my thread and my application is not handling it well. I found the solution in this Stack Overflow question. You have to tell your thread to stop via the Application_End method in Global.asax.cs which gets called when the application is recycled.

Comments
08/26 2011

C# and AOP (Aspect-oriented programming)

I just had a very interesting experience with AOP in C#. I have a function with a return type List which is being intercepted and that’s all well and good. However the interceptor function is a validator style function and can prevent the real function by being called and returning the boolean false.

So the code looks a little bit like this:

List<Update> updates = Manager.ValidateAndCreate();

// protected void Save(List<Update> updates) { ....
Save(updates);

The Method Interceptor looks like the following

public class ExceptionAdvice : AopAlliance.Intercept.IMethodInterceptor {

    public object Invoke(AopAlliance.Intercept.IMethodInvocation invocation) {

        if (isValid(invocation)) {
            return invocation.Proceed();
        } else {
            return false;
        }
    }

    private bool isValid( ...
 }

Now after validation fails the value of updates is actually a boolean not a List, I thought there would be some kind of runtime error here but there was not, so:

updates.GetType().Name == "Boolean"

But:

updates is bool == false

So save will still accept its mutated list of updates and will complain later on when you try to use it.

So how is this possible in a type safe language like C#? btw it’s spring-aop.

Link to the StackOverflow question

Comments
08/19 2011

Object-Relational Mapper Exceptions

When working with Object-Relational Mappers like nhibernate and Entity Framework 4 it can seem like a good idea to setup a Session or Context at the start of a request and finalise it at the end of a request.

This is exactly what I was doing until I figured out how wrong it was. I’ll be using Entity framework for my examples so I’m not repeating myself. So the problem is basically that a Context is not your database access object but a Unit of Work that will be committed to the database. This is an extremely important point and this is because of exception handling.

Now for a nice example, say you want to save a record to the users table but the username has a uniqueness constraint. If we’re using one context per request and try to add a new user with an unavailable username we get a nice UpdateException. Now once this happens you can’t use the context anymore. As a work around you can test for error states in a transaction before they happen, but that’s allot of work and doesn’t sound too nice.

The solution therefore is to perform actions as a Unit of Work. That way if one doesn’t work out you can handle it properly and recover.

Take a look at this article to point you in the right direction Using Repository and Unit of Work patterns with Entity Framework 4.0.

Comments
08/02 2011

Sharepoint People Picker Displays Selection as Html

When adding a people picker to a custom Html page I encountered an interesting error. When submitting the page the content would return hidden html from the control along with the message “You are only allowed to enter one item”.

The html looks like this.
< span id=’span xxxxx’ tabindex=’-1’ contentEditable=’false’ class=’ms-entity-resolved’ title=’xxxxx’ />

The issue seems to be I’m using IE 9 Standards mode, as SharePoint usualy runs in quirks mode.

Similar issues: http://blog.richfinn.net/blog/CommentView,guid,4fce2e56-8e53-48cb-b6d9-6249af8e2141.aspx

Comments
07/05 2011
I thought it was about time to present a first look at the website I&#8217;ve been working on for the last couple of months. Code named Deskspace it serves as an online notepad that is automaticly persisted. That means content, location and size so your deskspace.. is always just how left it. It&#8217;s built using ASP.Net MVC3. I&#8217;d ask for feedback but I havn&#8217;t set up comments yet, so hopefully you can try it out soon.

I thought it was about time to present a first look at the website I’ve been working on for the last couple of months. Code named Deskspace it serves as an online notepad that is automaticly persisted. That means content, location and size so your deskspace.. is always just how left it. It’s built using ASP.Net MVC3. I’d ask for feedback but I havn’t set up comments yet, so hopefully you can try it out soon.


Comments
07/05 2011

Sharepoint Permissions

The SharePoint permission model uses user impersonation, where a typical asp.net application will run under the permissions of the application pool SharePoint runs under the users credentials.

The scenario we’ll be looking at is updating a sharepoint list programmatically.

SharePoint has four default user groups, Read, Contributor, Designer and Full Control. Users would usually be in any of these groups or anything in between. This means that unless the users are also given explicit access to the list directly or via a group they may not have access to update a list even if allow unsafe updates is on.

To resolve this issue there are two solutions.

*The first is no revert back to the app pool, using this method means you have to identify the current user manually and by default all updates will appear as created or modified by the system account.
*The second option is to assign users access to the list explicitly or create a group with access and add the group to the users (either automatically or manually via SharePoint).

In my case I needed a solution where users could not access the list normally but using my custom solution allowed them to make updates. Therefore option one was the way to go for me.

Comments
1 of 2