Archive for September, 2011

Conditional DataAnnotations in c#

This is something a bit weird, which maybe even, dare I say, a bug in the .net DataAnnotationsExtensions pack (Installable via NuGet). I decorated one of my class properties with an Email data annotation, because If the class had an email set, I wanted to ensure it was valid. I didn’t, however, decorate it with a Required attribute. However it seems the Email attribute will return false if no value is passed, ensuring that it is infact required? (Please leave a comment if there is some really obvious built in way around this!)

To get around this I created a simple “IfPresent” data annotation which you can chain another validation onto; This basically returns a true if the value is null, otherwise it will pass it on to the real ValidationAttribute to work it”s magic. It can be implemented like this;

[DataMember]
[IfPresent(typeof(EmailAttribute), ErrorMessage = "Must be a valid email address")]
public string EmailAddress { get; set; }

The code is;

namespace Chinook.Model.ValidationAttributes
{
    public class IfPresent : ValidationAttribute
    {
        private ValidationAttribute attr;

        public IfPresent(Type attr)
        {
            this.attr = (ValidationAttribute)Activator.CreateInstance(attr); 
        }

        public override bool IsValid(object value)
        {
            if (value == null)
                return true;

            return attr.IsValid(value);
        }
    }
}

8 Comments

Mobile HTML5 Offline app links

I’m looking at writing some apps I did on the iPhone a while ago (the first of which will be Karma) to be mobile html5 offline accessible apps, using html5 audio/ canvas etc- so I’ll use this post to collect some useful links for research;

Also decided to use this as an opportunity to mess with nodejs, using one of the “nosql” db platforms;

http://blog.mongodb.org/post/812003773/node-js-and-mongodb

http://nodejs.org/

1 Comment

jqZoomage jQuery component

I’ve built a simple jQuery plugin for use on a website I’m building and am in the process of submitting it to the jQuery plugins website. It’s a simple zoom panel, allowing your users to mouse over portions of your small image, to see a close up view. Ideal for use on product details page or similar.

Screenshot

Installation

Just add a reference to the script at the bottom of your page (where all your js should be anyway!)- as shown in the demo, this is a basic example;

$(window).ready(function() {
	$("#dinky-img").zoomage({
		zoomedImageContainer: "#big-img",
		zoomedImageUrl: "big-image.gif",
		stayActiveOnMouseOut: false,
		zoomedImageStartsCentered: true
	});
});

There are a couple of styles which are required for the view finder, and the mask which goes over the image (allowing you to customise the look)- add these rules to your style sheet somewhere;

#zoomage-view-finder { border: 1px solid #fff; }
.zoomage-view-finder-surround { background-color:#fff; opacity:0.7; }

As a general point, it’s best to run the script in a $(window).load() rather than the standard $(document).load(), as this ensures that all the images are loaded before the script begins, otherwise the script runs and grabs the width() and height() of the image to work out ratios, but these get returned as 0′s! More info on this can be found on the stack overflow issue about this

Configuration

zoomedImageContainer * Required *
Default Value : “#zoom-image-container”
This is the jQuery selector for the div which will act as the close up view for your image.

zoomedImageUrl * Required *
Default Value : “”
This is the URL of the zoomed image file. I generally use images 3 times larger than the original, however there isn’t a limit. The scale ratio is calculated on load.

stayActiveOnMouseOut
Default Value : false
If this is set to true, the view finder will continue to show in it’s last position even when the mouse pointer is moved off of the image. If false, the view finder will disappear on mouse out.

zoomedImageStartsCentered
Default Value : true
Indicates that, on load, the zoomed image view shows the center of the image.

centerOnMouseOut
Default Value : true
If true, on mouse out, the zoomed view will return to the center of the zoomed image.

Live Demo

You can check out a real life working version of it here!

Tested across all the major modern browsers (IE9/ FF4/ Chrome10)- give me a shout if you spot an issue.

Download

Current Version 1.2 – 2011-09-10
Unminified Development Version
Minified Production Version

Licence

This plugin is available for use under the MIT licence. For more info see http://codeblog.shawson.co.uk/storage/jquery.zoomage/licence.txt

jQuery Plugin Site Link

The official jQuery Plugin catalogue link for this plugin is http://plugins.jquery.com/project/jqZoomage

11 Comments

WCF Services with both JSON and SOAP endpoints

A quick example of how to set this up in the web.config.

Services, in this example, can be accessed

  • using JSON via localhost/Services/MyService.svc/HelloWorld or..
  • using SOAP on localhost/Services/MyService.svc/soap/HelloWorld
  <system.serviceModel>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />

    <services>
      <service behaviorConfiguration="WebServiceBehavior" name="Demo.Web.Services.MyService">
        <endpoint address="" binding="webHttpBinding" contract="Demo.Web.Services.IMyService" behaviorConfiguration="jsonBehavior" />
        <endpoint address="soap" binding="basicHttpBinding" contract="Demo.Web.Services.IMyService"/>
      </service>
    </services>
    
    <behaviors>
      <endpointBehaviors>
        <behavior name="jsonBehavior">
          <webHttp defaultOutgoingResponseFormat="Json" />
        </behavior>
      </endpointBehaviors>
      
      <serviceBehaviors>
        <behavior name="WebServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
    <bindings>
      <webHttpBinding>
        <binding crossDomainScriptAccessEnabled="false" name="httpBinding" />
      </webHttpBinding>
    </bindings>
    
  </system.serviceModel>

No Comments

Creating stretchy “9 patch” buttons in Android

Very cool button standard used in Android for creating flexible buttons – also has a little wysiwyg editor, tucked away in the SDK folder;

http://developer.android.com/guide/developing/tools/draw9patch.html

2 Comments

MVC Razor Syntax – Web forms “Master Page” content area equivalent

In razor instead Web Forms ContentArea’s in Master Pages you have @sections within your _Layout.cshtml file. In the _layout file you have a declaration such as;

@RenderSection("JavaScript", required: false)

Then in your content pages;

@section JavaScript
{
   <script type="text/javascript" src="@Url.Content("/Scripts/SomeScript.js")" />;
   <script type="text/javascript" src="@Url.Content("/Scripts/AnotherScript.js")" />;
}

Source : http://stackoverflow.com/questions/4311783/asp-net-mvc-3-razor-include-js-file-in-head-tag

No Comments

MySQL 5.1 with DotNet claims Procedure or function ‘‘ cannot be found in database ‘

We have some dot net code which uses MySQL- our live setup it thus;

MySQL 5.1.45
MySQL Dot net connector dll version 6.2.2.0

We recently rebuilt our dev server, and installed MySQL 5.1- it turns out the minor revision number was slightly higher, but nothing to set off any alarm bells, or so we thought;

MySQL 5.1.57

With this combination or MySQL connector 6.2.2.0 and MySQL 5.1.57, we couldn’t execute any stored procedures- we would just get;

Procedure or function ‘ ‘ cannot be found in database ‘

Googling turned up lots of suggestions like “make sure your connection string is lower case” etc etc- nothing fixed our issue.

We found the solution was to update our MySQL dot net connector to the latest, which at time of writing was 6.3.7.0.

You can get MySQL here, and the MySQL dot net connector here.

No Comments