Attached Properties in Silverlight

 

1. Bellow you can see how to assign value to an attached property from within C# and XAML:

C#
TabPanel.SetTabStop( uiElement, true );
    //or
uiElement.SetValue( TabPanel.TabStopProperty, true );
XAML
<TabPanel>
    <Button TabPanel.TabStop="True"/>
</TabPanel>

As you can see here the attached property is TabPanel.TabStop and the TapPanel is the property provider that has registered it internally. This registration is explained later in our example.

2. Here is how our TabPanel class finally should look like:

public class TabPanel : StackPanel
{
    publicTabPanel() {}
    public static readonly DependencyProperty TabStopProperty = DependencyProperty.RegisterAttached(
             "TabStop",                  //Name of the property
             typeof( bool ),             //Type of the property
             typeof( TabPanel ),   //Type of the provider of the registered attached property
             null );                           //Callback invoked in case the property value has changed

    public static void SetTabStop( DependencyObject obj, booltabStop )     {
             obj.SetValue( TabStopProperty, tabStop );      }

    public static bool GetTabStop( DependencyObject obj )     {
             return ( bool )obj.GetValue( TabStopProperty );  }
    //Just a sample method ilustrating the idea how to obtain the value
    //of the TabStop property for a specific element.
    private void ProcessTabKey() {
             foreach ( UIElement element in this.Children ) {
                                   bool tabStop = TabPanel.GetTabStop( element );
                                   //Perform some processing according to the tab stop… }
}}

Via Attached Properties in Silverlight

Web Application Toolkits

 

Web App Toolkits that show you how to accomplish common web development tasks with small sample apps.  The scenarios covered with the first round of Web App Toolkits include:

Via Web Application Toolkits | LostInTangent | Channel 9

Unity IoC in 30 seconds

Object Factory

Container-Configured Registration of Types

As an example of using the overloads of the RegisterType and Resolve methods, the following code registers a mapping for an interface named IMyService, and specifies that the container should return an instance of the CustomerService class (which implements the IMyService interface).

IUnityContainer myContainer = new UnityContainer();
myContainer.RegisterType<ILoggingService, LoggingService>();
ILoggingService myServiceInstance = myContainer.Resolve<ILoggingService>();

Container-Configured Registration of Existing Object Instances

As an example of using the overloads of the RegisterInstance and Resolve methods, the following code registers an existing instance of a class named LoggingService that implements the interface IMyService and then retrieves that instance.

IUnityContainer myContainer = new UnityContainer();
LoggingService myExistingObject = new LoggingService();
myContainer.RegisterInstance<ILoggingService>(myExistingObject);
ILoggingService myServiceInstance = myContainer.Resolve<ILoggingService>();

Constructor Injection

As an example of constructor injection, if a class that developers instantiate using the Resolve method of the Unity container has a constructor that defines one or more dependencies on other classes, the Unity container will automatically create the dependent object instance specified in parameters of the constructor. For example, the following code shows a class named CustomerService that has a dependency on a class named ILoggingService.

public class CustomerService
{
  public CustomerService(ILoggingService myServiceInstance)
  {
    // work with the dependent instance
    myServiceInstance.WriteToLog("SomeValue");
  }
}

Property (Setter) Injection

In addition to constructor injection, described earlier, the Unity Application Block supports property and method call injection. The following code demonstrates property injection. A class named ProductService exposes as a property a reference to an instance of another class named SupplierData (not defined in the following code). To force dependency injection of the dependent object, developers must apply the Dependency attribute to the property declaration, as shown in the following code.

public class ProductService
{
  private ISupplierData supplier;

  [Dependency]
  public ISupplierData SupplierDetails
  {
    get { return supplier; }
    set { supplier = value; }
  }
}

Highlights of the Unity Application Block


The Unity Application Block includes the following features:

  • It provides a mechanism for building (or assembling) instances of objects, which may contain other dependent object instances.
  • It exposes RegisterType methods that support configuring the container with type mappings and objects (including singleton instances) and Resolve methods that return instances of built objects that can contain any dependent objects.
  • It provides inversion of control (IoC) functionality by allowing injection of preconfigured objects into classes built by the application block. Developers can specify an interface or class type in the constructor (constructor injection), or apply attributes to properties and methods to initiate property injection and method call injection.
  • It supports a hierarchy for containers. A container may have child container(s), allowing object location queries to pass from the child out through the parent container(s).
  • It can read configuration information from standard configuration systems, such as XML files, and use it to configure the container.
  • It makes no demands on the object class definition. There is no requirement to apply attributes to classes (except when using property or method call injection), and there are no limitations on the class declaration.
  • It supports custom container extensions that developers can implement; for example, methods to allow additional object construction and container features such as caching.

Via Introduction to Unity