Wednesday, October 8, 2008

Unity 1.2 and Interception

Unity 1.2 is under development, but we do know that it will support Interception.

David Hayden has a nice screencast showing how it works.

David shows how you can add an interceptor to time the execution of a method in an object that Unity has created for you. Cool stuff. Check it out!

Monday, October 6, 2008

Fun with C# 3.0 (#4) - Nullable types and the ?? operator

C# has reference types and value types. Reference types are commonly called objects, and variables that are declared to be of a reference type can refer to an object or be null. Value types (int or bool, for example) can be stored in variables that contain the actual value, and therefore can never be null.

Sometimes it is useful to have a value type that can have a null value, so this feature has been added to C# 3.0.

This feature is implemented using generics and the type System.Nullable<T>, the shorthand for which is T?. The property HasValue can be used to see if the variable is null. Here's an example:

int? x = 10;
if (x.HasValue)
{
System.Console.WriteLine(x.Value);
}
else
{
System.Console.WriteLine("Undefined");
}

A nullable type can not be assigned to a value type. The following will not compile:


int? x = 10;
int y;
y = x;

Rather use the Value property:


int? x = 10;
int y;
y = x.Value;

Accessing Value will throw an InvalidOperationException if the nullable type is null. Use the ?? to assign a default value.


int? x = null;
int y;
y = x ?? 0;

This is equivalent to:


y = x.HasValue ? x.Value : 0;

Usage of the ?? operator is not limited to Nullable types. For example:


string name = enteredName ?? "Name not entered.";

Friday, October 3, 2008

The Common Service Locator

I have argued against making 'hard' references to an underlying Dependency Injection (or IoC) container; rather, allow the container to do the work for you, namely, inject the dependencies. I realize that it can be difficult in practice, and apparently so have others.

A group of DI container authors have come together to define a common interface for using the underlying containers without creating a hard reference to a particular implementation. This is useful when you are writing code that needs to use a container to resolve a dependency (i.e. get an instance) but you don't know or care what container is being used.

So now we have the Common Service Locator library. Thanks to Ayende for blogging it. UPDATE: Also Glenn Block.

Each container needs an adapter to implement the interface and do the translation from the common method calls to the native container calls. This allows all container references to be declared as Microsoft.Practices.ServiceLocation.IServiceLocator.

Your code has two ways of getting the container:

  • The IServiceLocater reference can be supplied by the calling code and you just use it, or
  • You code can use a static property, ServiceLocator.Current.

Here is the interface that you will use (API Reference):

using System;
using System.Collections.Generic;

namespace Microsoft.Practices.ServiceLocation
{
// Summary:
// The generic Service Locator interface. This interface is used to retrieve
// services (instances identified by type and optional name) from a container.
public interface IServiceLocator : IServiceProvider
{
// Summary:
// Get all instances of the given TService currently registered in the container.
//
// Type parameters:
// TService:
// Type of object requested.
//
// Returns:
// A sequence of instances of the requested TService.
//
// Exceptions:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is are errors resolving the service instance.
IEnumerable<TService> GetAllInstances<TService>();
//
// Summary:
// Get all instances of the given serviceType currently registered in the container.
//
// Parameters:
// serviceType:
// Type of object requested.
//
// Returns:
// A sequence of instances of the requested serviceType.
//
// Exceptions:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is are errors resolving the service instance.
IEnumerable<object> GetAllInstances(Type serviceType);
//
// Summary:
// Get an instance of the given TService.
//
// Type parameters:
// TService:
// Type of object requested.
//
// Returns:
// The requested service instance.
//
// Exceptions:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is are errors resolving the service instance.
TService GetInstance<TService>();
//
// Summary:
// Get an instance of the given named TService.
//
// Parameters:
// key:
// Name the object was registered with.
//
// Type parameters:
// TService:
// Type of object requested.
//
// Returns:
// The requested service instance.
//
// Exceptions:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is are errors resolving the service instance.
TService GetInstance<TService>(string key);
//
// Summary:
// Get an instance of the given serviceType.
//
// Parameters:
// serviceType:
// Type of object requested.
//
// Returns:
// The requested service instance.
//
// Exceptions:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is an error resolving the service instance.
object GetInstance(Type serviceType);
//
// Summary:
// Get an instance of the given named serviceType.
//
// Parameters:
// serviceType:
// Type of object requested.
//
// key:
// Name the object was registered with.
//
// Returns:
// The requested service instance.
//
// Exceptions:
// Microsoft.Practices.ServiceLocation.ActivationException:
// if there is an error resolving the service instance.
object GetInstance(Type serviceType, string key);
}
}



Here are the containers that have adapters at the moment:





I recommend IServiceLocator be used in all components that might need a reference to a container. Note that they are no common calls for configuring the container because this is a container-specific task that should be handled in the calling code.

Thursday, October 2, 2008

Microsoft "Dublin" addresses composite SOA applications

Microsoft just released a white paper outlining how .NET Framework 4.0 and new Windows Server capabilities (code-named Dublin) will help developers design, build and maintain Composite Applications. The white paper is available here.

What are composite SOA applications? Applications that are composed by assembling and integrating reusable services. This makes the deployment and management of applications (and servers) more complex and creates new requirements to address scalability, performance and reliability.

Microsoft has announced .NET Framework 4.0 to add functionality to WCF and WF, and also "Dublin", an add-on to Windows Server 2008, that will make SOA applications and servers easier to deploy, configure, manage and scale. The first preview will be available at the PDC at the end of October.

See the chart below from the white paper:

 

Windows Communication Foundation 4.0

Windows Workflow
Foundation 4.0

Windows Server
"Dublin" technologies

RESTful enhancements

·   Simplifying the building of REST Singleton & Collection Services, ATOM Feed and Publishing Protocol Services, and HTTP Plain XML Services using WCF

·   WCF REST Starter Kit to be released on Codeplex to get early feedback

Messaging enhancements

·   Transports - UDP, MQ, Local in-process

·   Protocols - SOAP over UDP, WS-Discovery, WS-BusinessActivity, WS-I BP 1.2

·   Duplex durable messaging

Correlation enhancements

·   Content and context driven, One-way support

Declarative Workflow Services

·   Seamless integration between WF and WCF and unified XAML model

·   Build entire application in XAML, from presentation to data to services to workflow

 

Significant improvements in performance and scalability

·   Ten-fold improvement in performance

New workflow flow-control models and pre-built activities

·   Flowcharts, rules

·   Expanded built-in activities – PowerShell, database, messaging, etc.

Enhancements in workflow modeling

·   Persistence control, transaction flow, compensation support, data binding and scoping

·   Rules composable and seamlessly integrated with workflow engine

Updated visual designer

·   Easier to use by end-users

·   Easier to rehost by ISVs

·   Ability to debug XAML

Provide standard host for WF and WCF applications   

Pre-built developer services

·   Message-based correlation

·   Message forwarding service

·   Content-based message routing

·   Compensation service for long-running transactions

Greater scalability and easier manageability

·   Enable scale-out of stateful workflow applications

·   Persisting and rehydrating state for high scalability

·   Enhanced management and monitoring functions

·   Tracking store for workflow events

Supports “Oslo” modeling platform

 

 

Key takeaways:

  • Microsoft will make it easier to design applications as WF workflows that are easily integrated into WCF services.
  • WCF and WF both use XAML. Microsoft will move to a "unified" XAML to make the integration easier.
  • Microsoft is moving more toward REST for web services due to its simplicity. The REST Starter Kit will be available on Codeplex in the near future.
  • The server infrastructure (Dublin) to host and manage these new types of applications will become available after .NET 4 and Visual Studio 2010 ship.

Links: