Archive

Posts Tagged ‘tutorial’

Unit Testing, WCF and MEF – Part 2

October 18, 2012 2 comments

In the first part of this article I showed how I’ve been testing code using MEF and a generic base class that is in charge of wiring up the dependencies.

In this part I will focus on how I got WCF to wire up my web service using MEF in what I think is quite an elegant way.

My first requirement was to be able to write a web service that will use dependencies (imports) but that wouldn’t require me to write the composition code in the service class itself. Most of the examples I saw online didn’t provide me with a good way to do that unfortunately.

Until I came across this excellent post by Tim Roberts which shows how to tap into the WCF pipe-line and have your services composed automatically instead of having to do it per service or including wire-up code in your classes.

While the code Tim provided was a great start I thought it wasn’t just right for me. Specifically it was the fact that it required custom code in the Global.asax file which for me was not as elegant as it should be.

So I’ve done a bit more digging into the WCF pipe-line and discovered that I could achieve an elegant, completely configuration-based solution by implementing my own endpoint behavior.

In the end I needed to only create 3 classes to achieve this: EndpointComposedElement, ComposedEndpointBehavior and ComposedInstanceProvider.

Read more…

Setting up a Tridion 2011 GUI extension in 8 steps

March 3, 2011 11 comments

The new 2011 Tridion GUI framework is a major overhaul to the way extensions have been previously developed. The framework now is far more robust and well designed. Building new and exciting extensions has never been easier. However, configuring your extension may not be the easiest thing in the world to do…

The following tutorial steps show how to configure an extension for the Tridion 2011 GUI from start to finish.

The example configuration and code is based on my extension: The Item XML Display.

Read more…

More on getting Froyo (android 2.2) for your Galaxy S

November 15, 2010 3 comments

A few weeks ago I wrote about how to get Froyo (Android 2.2) for your Samsung Galaxy S even if you’re outside of Nordic countries and Kies doesn’t let you upgrade.

The article explained how to trick the Samsung application into thinking your phone is from the Nordic variety by changing some registry values. I believe that Samsung already blocked this approach on their servers a while ago but its not important because for most countries the official version of Froyo is out.

Read more…

Categories: Random Tags: , , , ,

Get Froyo (android 2.2) on your Galaxy S now

October 17, 2010 11 comments

Important! Read this article first: More on getting Froyo (android 2.2) for your Galaxy S

If you’re like me and you already heard that Samsung released their version of the Android 2.2, Froyo update but can’t wait until it reaches your region then you might want to read on…

Some areas like in Scandinavia have already been officially getting the update but here in the U.K. apparently it will take a few more days. Well, I’m not going to wait even that to get all of that Froyo goodness:

  • Pinch to Zoom homescreens overview.
  • Flash 10.1
  • Big speed increase
  • Auto updating Android market
  • Hebrew support
  • and more

To get the new update you first have to make sure you have the latest Kies software which you can download here. You can check which version you have by opening Kies and then clicking on the top left of the screen where it says  “Kies” and then “Program Information”.

samsung kies version

If you don’t have the 1.5.3.xxxx._82 then use the link above to download it.

Once you have done that, follow the instruction in this forum thread, instruction copied here for convenience:

Read more…

Categories: Random Tags: , , , , ,

My First Attempt at Tridion 2011 GUI Extensions

October 13, 2010 7 comments

About a month ago I happily shared a GUI extension I’ve built for Tridion 2009 called the Item XML Display extension.
This extension lets a user (typically a developer) view the entire XML structure of a Tridion item from within the GUI without the need to open a new IE window or having a remote session to the CM server.

If you’re one of those already using this extension and have been dreading the moment Tridion 2011 will come out and you will be left without being able to quickly view items’ XML, fear not! I got you covered:

xmldisplay-extension-2011-firefox
Read more…

Using jQuery for Tridion GUI Extensions

August 7, 2010 14 comments

A relatively unknown feature of the Tridion CMS product is the GUI extensibility framework.

This is actually a fantastic feature which allows us to create and supplement the user interface with just about any type of addition we can think of.

It is pretty unknown because of the lack of documentation but with a little bit of investigation work it is possible to find all sorts of treasures we can use to extend the current GUI.

Here’s one example that is already available on the SDLTridionWorld website’s Community eXtensions section: “Republish from publish queue”, this nifty extension gives users the ability to republish items directly from the queue instead of the normal way of locating the item within the folder or structuregroup hierarchy.

The extension framework is quite robust (not without limitations of course) and allows us to add toolbar items, context menu options, tree nodes or simply run scripts that can do just about anything.

In this article I’m focusing on the latter, adding a scripted extension to do different tasks.

If you’ve done any kind of javascript development you know how difficult and complex it can get, that’s where jQuery comes in, jQuery being a javascript library created to ease the development of client side code.

jQuery’s CSS selectors and methods for DOM manipulation are exactly the tools to help us create Tridion GUI extensions quickly and with relative ease.

The way the extension framework is built allows us to create a jQuery extension that can be reused by other extensions.

Read more…

Creating a Gravatar client with jQuery and WCF

August 1, 2010 6 comments

Introduction

A couple of weeks ago I announced the release of the first .NET implementation of the Gravatar API.

Gravatar.NET is available for download on Codeplex.

What I’d like to show in this article is how to make use of the library using jQuery and WCF to create your own Gravatar client (Ponderi.com style).

This article will demonstrate how to show a user their Gravatar account images, allow the user to activate a different image or delete it.

I will build upon many of the concepts I described in an earlier article I wrote called: “Creating a Webservice Proxy with jQuery”. So if you like, check it out first as I won’t go into the basics in this article.

Read more…

LINQ to Tridion

January 8, 2010 4 comments

Contents

Introduction

Its Friday evening, a good time to share a little something I’ve been working on.

.NET 3.0 brought with it a fantastic new language (VB, C#) extension called LINQ which stands for Language-Integrated Query.

Microsoft describes it as “a set of extensions to the .NET Framework that encompass language-integrated query, set, and transform operations. It extends C# and Visual Basic with native language syntax for queries and provides class libraries to take advantage of these capabilities.”

In short it gives the developer a more general syntax for querying sets or collections of data.

The beautiful thing about LINQ is that it can be used over any collection whether its XML, Database or even objects so long as they support the IEnumerable<T> interface.

For more information see the resources section of this article or simply google/bing it.

Read more…

A Few Useful Tridion Extension Methods

October 26, 2009 8 comments

Following my recent blog about extension methods. Here are a few useful extension methods I’ve created recently for working with Tridion’s TOM API.

.NET Interops – COM API (5.2 and onwards)

Here are a few useful methods to use with the older TOM API (interops), these can come in handy when writing custom tools, event system code, etc.

Publish

The publish method for both Page and Component objects require quite a few parameters which in many cases are always set the same within a single application scope. If you want to use defaults for these values you can use these extension methods to predetermine the values of the parameters leaving a shorter and more convenient way for sending the items to be published:

For Components:

1
2
3
4
5
6
7
8
/// <summary>
/// Offers a simple call to publish a components using defaults
/// for all of the publish parameters  
/// </summary>
/// <param name="targets">one or more pucliation targets or target
/// types to publish to
/// </param>
public static void Publish(this Component component, object targets, 
                                          EnumPublishPriority priority)
{
     component.Publish(targets, false, false, false, DateTime.Now, 
            DateTime.MinValue, DateTime.Now, true, priority, true, 100);
}

For Pages:

1
2
3
4
5
6
7
8
/// <summary>
/// Offers a simple call to publish a components using defaults
/// for all of the publish parameters
/// </summary>
/// <param name="targets">one or more pucliation targets or
/// target types to publish to
/// </param>
public static void Publish(this Page page, object targets,
                                   EnumPublishPriority priority)
{
page.Publish(targets, false, false, false, DateTime.Now,
        DateTime.MinValue, DateTime.Now, true, priority, true, 100);
}

UnPublish

Very similarly to the publish method, here’s an example for creating a shorter way to call un-publish on a page object:

1
2
3
4
public static void UnPublish(this Page page, object targets,
                                     EnumPublishPriority priority)
{
     page.UnPublish(targets, true, false, false, DateTime.Now, 
                                                 false, priority);
}

IsPublished

Tridion provides a method to check whether a page is published to a specific Target Type or Publication Target but what if you just want to check whether the page is published or not?
You need to check each target.
Here’s a way to add a method to the page type that will check whether a page is published by providing a list of targets to the method call:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/// <summary>
/// Assert whether a page is published or not
/// </summary>
/// <param name="page">The page in question</param>
/// <param name="targets">A list of target types or
/// publication targets to check
/// </param>
/// <returns>True if the page is published to at least
/// one of the provided targets</returns>
public static bool IsPublished(this Page page, IEnumerable<string> targets)
{
    foreach (var t in targets)
    {
        if (page.IsPublishedTo(t)) return true;
    }

    return false;
}

Release

Its very important to make sure you correctly dispose of the Tridion COM objects exposed by the interops.

Here’s an example of how to easily do that for components and pages without having to add: “using System.Runtime.InteropServices;” to every code file:

For Component:

1
2
3
4
5
6
7
/// <summary>
/// Releases the COM Component object
/// </summary>
public static void Release(this Component component)
{
    Marshal.ReleaseComObject(component);
}

For Page:

1
2
3
4
5
6
7
/// <summary>
/// Releases the COM Page object
/// </summary>
public static void Release(this Page page)
{
    Marshal.ReleaseComObject(page);
}

TOM.NET (5.3 and onwards)

Fields

One of the things I was quite astonished to find when i first started working with the TOM.NET API was that the Component object no longer exposed a fields collection as a property. Instead to get an instance of those fields you are required to write code which is not very intuitive and definitely shouldnt be required for such a basic property.
I’ve since filed a formal request to our R&D department to change this but there’s no guarantee that this is going to change anytime soon.

If you are using Tridion 2009 and .NET 3.5 or brave enough to write your .NET templates on 5.3 or 5.3 SP1 with .NET 3.5 you might be interested in using the following extension method:

1
2
3
4
5
6
7
/// <summary>
///  Returns an ItemFields collection for the Component
/// </summary>
public static ItemFields Fields(this Component component)
{
     return new ItemFields(component.Content, component.Schema);
}

With this method in place to get the fields collection of a component you only need the following: comp.Fields()…

Root StructureGroup

Another oddity in the relatively new API is that the publication object doesnt expose a RootStructureGroup property while it does expose a RootFolder property. Slightly annoying but easily fixed with… you guessed it! An extension method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/// <summary>
/// Returns the root structure group for this publication
/// </summary>
public static StructureGroup RootSG(this Repository publication)
{
    Filter filter = new Filter();
    filter.Conditions["ItemType"] = ItemType.StructureGroup;
    IList<RepositoryLocalObject> items = publication.GetItems(filter);

    if (items.Count == 0)
       return null;
    else
       return (StructureGroup)items[0];
}

Conclusion

These are just a few useful methods that can make your Tridion development quicker and more convenient.
Undoubtedly there’s many more such methods people can think of, if you do have some that you use please share them and if we have enough I will create an extension on World with all of them!

Code

You can download the methods ive shown in this article here: code.

for all of the

Creating a Webservice Proxy with jQuery

August 2, 2009 23 comments

Contents

Introduction

In a previous post I’ve shown how to use WCF, jQuery and jTemplates to retrieve information from the server and lay it out on the page using jTemplates’ templating engine.

The code example in that article used the Microsoft Ajax client library and the proxy it creates at run time to ease the javascript code needed to call the methods of the webservice I created.

In this article I would like to show you an easy way of creating a similar proxy to the Microsoft one in pure javascript and the help of jQuery’s AJAX capabilities. This makes a lot of sense if you do not have the option to use the MS library on the client or simply because you don’t want to.
I will be relying mostly on the code examples and concepts I described in the previous article  so I’d recommend reading it first before continuing to read this one.

You can let me know if you find this article useful or not by using the ratings and comments below.

Server Side

The good news: no need to change anything on the server side, the web service can continue to work as it always did. All we’re doing in this exercise is to replace the client side logic and so we will not touch the server code.

Client Side

Using the same website I’ve shown how to create in the previous article we will create the new proxy for our WCF webservice.
In the System folder create a javascript file and call it ServiceProxy.js, To the file add the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
ServiceProxy = function() //constructor for the proxy
{
  this._baseURL = "Services/TutorialService.svc/";
};

ServiceProxy.prototype =
{
  _defaultErrorHandler: function(xhr, status, error)
  {
    alert(xhr.statusText);
  },

  _doAjax: function(method, data, fnSuccess, fnError)
  {
    if (!data) data = {};

    if (!fnError) fnError = this._defaultErrorHandler;

    $.ajax({
      type: "GET",
      url: this._baseURL + method,
      data: data,
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      success: fnSuccess,
      error: fnError,
      dataFilter: function(data)
          {
            var response;

            if (typeof (JSON) !== "undefined" && typeof (JSON.parse) === "function")
              response = JSON.parse(data);
            else
              response = val("(" + data + ")");

            if (response.hasOwnProperty("d"))
              return response.d;
            else
              return response;
          }
    });
  }
};

Lets examine the code:

The first section is the constructor of the proxy object, in there we define the base URL of the service we will be calling.

The second method is _defaultErrorHandler, this is a default error handler, its strictly a developer friendly method, allowing the calling method to leave the pointer to an error handling method empty. Of course the implementation of this default error handler should be changed in a real life scenario as its not wise to show any raw error information to a website’s visitor.

The third and most important method is _doAjax, this is where the “magic” happens which takes care of the back and forward communication with the server.

The first two lines take care of the defaults, if there is no data passed to the webservice we assign an empty object and if there is no pointer supplied to an error handler we assign our default method.

Next is the where we use jQuery’s incredibly convenient Ajax method, lets examine this call, jQuery’s ajax method accepts an object that sets all the needed information:

‘type’: The HTTP verb: could be GET, POST, etc. In this example we use GET since we’re only retrieving from the server.
For a more robust scenario we would probably want to turn this into a parameter of the method.

‘url’: The URL of the webservice method, in this case the base URL defined in the constructor and the name of the method.

‘data’: The parameters passed to the service method, constructed as a JSON object.

‘contentType’: the content type of the data being sent to the server. In our case we’re communicating with the webservice using JSON and therefore we use: “application/json; charset=utf-8”

‘success’: A pointer to the method to be called when the asynchronous call has finished successfully.

‘error’: A pointer to the method to be called when the asynchronous call finished with an error.

‘dataFilter’: I’ve taken the code block in this example from Dave Ward’s Encosia blog. I will add the links to a couple of his articles at the end of this post.

The dataFilter is used whenever there is a successful response from the server and allows us to modify the response in any way we like before its passed to the success method.

In this example there are two uses to the filter: The first one is to turn the response into a JSON object from the string result coming from the server. It will first check whether the current browser supports JSON parsing natively (Firefox 3.5, IE8 for example). If it does it will use the native JSON object and if not will use the less optimized and less secure eval() function.

The second part is to check whether the response is enclosed within a ‘d’ property, this ‘d’ property is added automatically by Microsoft’s WCF service and since we want to abstract this fact from the caller we only return the relevant response if this is the case.

Now that we have everything we need for our base proxy functionality all we need to do is add the methods that match the exposed methods on the webservice. To the prototype of the ServiceProxy object add these two methods:

1
2
3
4
5
6
7
8
9
10
11
getArticles: function(success, error)
{
  this._doAjax("GetArticles", null, success, error);
},

getArticle: function(link, success, error)
{
  var data = {link: link};

  this._doAjax("GetArticle", data, success, error)
},

These two methods call our _doAjax, passing the necessary information such as the method name and the parameters if needed to the call.

In fact the methods mirror the methods available on the webservice thus completing the proxy.

Now the only thing to do is create a HTML file (or any other file, could even be JSP) that will replace the aspx page we created in the previous article.
You can find all the source code in the example site ive made available for download, in this post I will only show the changes needed.

- First, create a default.html in the root of the site.
- In the head element add these links:

<script src=”System/jquery.js” type=”text/javascript”></script>
<script src=”System/jquery-jtemplates.js” type=”text/javascript”></script>
<script src=”System/ServiceProxy.js” type=”text/javascript”></script>
<link href=”System/Styles.css” rel=”stylesheet” type=”text/css” />

- Copy over the entire javascript element.
- Add the following code as the first line of the script to declare the proxy:

<script type="text/javascript">
    var proxy = new ServiceProxy();

- In the document ready function replace the call to the GetArticles method with this one:

1
2
3
4
$(document).ready(function() //executes this code when page loading is done
{
       proxy.getArticles(articlesRetrieved);
});

- In the loadArticle function replace the call to getArticle with this one:

1
2
3
4
5
6
7
function loadArticle(link)
{
    $("#LoadingImg").removeClass("Hidden");
    $("#SingleArticle").html("");

    proxy.getArticle(link, articleRetrieved, serviceDefaultErrorHandler);
}

That’s all! Now you have a working page doing exactly the same as it did before only using our own javascript proxy without any dependencies on the Microsoft proxy or the MS Ajax library.
The end result is as expected identical to the one before:

serviceproxy result

Source Code

Can be downloaded here.

Resources

Follow

Get every new post delivered to your Inbox.

Join 302 other followers

%d bloggers like this: