Archive

Posts Tagged ‘.NET’

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…

Unit Testing, WCF and MEF

October 18, 2012 Leave a comment

Intro

This article will focus on how I’ve adopted unit testing in .NET using MEF and how to also apply this to WCF web services.

I haven’t been dedicated to unit testing for a long time but once I started I couldn’t stop. There are many great benefits to doing unit tests, I wont list them here but a good article to read is at: Agitar.com

What made me build something on my own? I wanted something light that I could also integrate into existing code without making too many changes. So I focused on a couple of things: Testing and Dependency Injection. I wanted an elegant solution that didn’t rely on external libraries or that will require a steep learning-curve.

In this article I will focus on these topics and how I solved it for myself. I’m not going to dive into other topics such as The SOLID principals, TDD, Dependency Injection, Mocking, etc. If these terms are not familiar to you, I suggest you read up on them first.

I’m actually going to divide this article into two parts, to keep them shorter and to the point:

Read more…

Calling all Tridion Developers

October 28, 2010 6 comments

With SDL Tridion (R)5.3 and the introduction of Modular/Compound Templates, the way of doing templates as radically changed, no longer we had to use an  old and obsolete technology such as VBScript. We could finally use .NET and Visual Studio to write template code as well as introduce “new” technologies such as XSLT with the help of Mediators (see XSLT Mediator).

One of the biggest advantages of this change is the ability to reuse existing code in a structured and clear way. Logic can be placed in dedicated classes, OO concepts can be leveraged for inheritance, encapsulation and more.

This led to the TemplateBase class which I and many others have been using for several years now. Around it I have also grouped additional classes to support reuse of common functionality that can be carried from one implementation project to another. This is known as the Templating Base Project.

If you’re unfamiliar with the Templatebase class and the Templating Base Project I suggest you hurry and get yourself over to SDLTridionWorld.com and download the code now – Template Base Project

Templating Base Project on SDL Tridion World Read more…

Integrating Tridion with Gravatar

August 11, 2010 3 comments

Lately I’ve worked on a Gravatar library for .NET which I released in July. I have also recently had the chance to work on Tridion GUI extensions for a customer.

This got me thinking; wouldn’t it be cool to integrate the two? Almost immediately I thought; yes, it will be cool!

So I’ve set to work on a nice GUI extension that will bring Gravatar to Tridion users and give the GUI a bit of a social touch make it feel more personalized by displaying a personalized photo for each user.

And so the integration is really 3 different extensions that can be each used individually or together.

Current User Photo

The first integration shows the Gravatar photo for the current user logged on to the Content Manager Explorer:

Read more…

Categories: Tridion Tags: , , , , , , , ,

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…

Announcing Gravatar.NET

July 15, 2010 4 comments

Introduction

For a while now I’ve been working on an implementation of the Gravatar public API.

Unfortunately it took longer than I had intended, what with those pesky work commitments and personal life issues…

I’m glad to tell you that Gravatar.NET is now available for download on Codeplex.

If you don’t know what Gravatar is, it’s: “A Globally Recognized Avatar”, allowing you to store a personal image (or images) and reuse it across different websites that support the use of it (example: WordPress.com).

If you’re building a website that includes storing user account information, and let’s face it, what website doesn’t do that these days, then typically one of the things your users expect is to be able to associate a photo to recognize their account on the site. Instead of having to store and manage these photos on your server (sometimes with a bandwidth/storage limit), Gravatar can do it for you.

Read more…

IE Caching Ajax Results – How to fix

April 27, 2010 8 comments

Updated (25/06/2010): Fixed typos in code example
Updated (16/11/2012): Fixed missing comma. 

I have been working on a website recently and one of the pages on the site has a  main functionality of showing dynamic results returned from a AJAX
call to a WCF service.

As I was developing and continuously testing with Firefox I was happy to see that my code was working well; based on the URL the results on the page will differ as expected.
However, When it came to test the page with Internet Explorer the results were dramatically different… No matter what the URL was the same content from my initial request kept appearing on the screen! Quickly enough I realized that it wasn’t something wrong with my code but with IE, after all in Firefox and Chrome it was working just fine.

So naturally, I googled for a solution and found this StackOverflow thread that confirmed to me that this was indeed an issue with IE. Unfortunately, the good answers there were only helpful to those who use jQuery to initiate the AJAX requests but I was using the proxy generated automatically by ASP.NET, so I had to find my own solution as I was unable to find anything useful online.

Below are ways you can use to fix the problem. First, I’ll show how to fix it when using the MS Service Proxy and then how to fix it when using jQuery.

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…

ASP.NET Custom Sitemap Provider

January 5, 2010 Leave a comment

Contents

Introduction

Quite a while ago I internally blogged about creating a custom Sitemap provider in ASP.NET.  Even though its been a while I believe its still relevant, especially the SQL caching example I added.  Hopefully you can still get something out of it.

With ASP.NET 2: Microsoft introduced a few services which are built on top of the provider model.
Services such as the membership or sitemap rely on providers to supply them with the actual information. All the providers are derived from the ProviderBase class.

The default Sitemap provider is the XmlSiteMapProvider which is used by ASP.NET to retrieve the website’s navigation structure stored in the Web.sitemap file.

The cool thing about this is that we can easily extend these providers if we need different functionality than what Microsoft provides. So As long as we keep inline with the providers’ interfaces the services will work with our own customizations just fine.

A benefit of creating a sitemap provider that reads from a database should be obvious: Its pretty easy to add custom navigation where needed to new or existing sites by reading ready information from our application data store. The information in many cases will already be structured in a hierarchy so its easier to retrieve it for a navigation structure. This allows us to create the main site’s navigation, breadcrumbs, or specific structures for different sections of our site in a pretty easy way to implement, no need to write additional files on to the webserver’s file-system and the caching mechanism shown in this article gives a boost for performance.

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
Follow

Get every new post delivered to your Inbox.

Join 302 other followers

%d bloggers like this: