Share via


Objects That Implement a Known Interface

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

The alternative approach is to use objects that implement a known interface. Developers can create a suitable interface, if one does not already exist, and then implement it in the target object. The interface allows the Policy Injection Application Block to detect the features of the object and discover the members to expose through the proxy it generates. The application block can then create a suitable helper/wrapper class that adds policy injection through a handler pipeline to the object. However, the client can only access the target object through the members defined within the specified interface.

If the target object already implements a known interface, the class source file will indicate this by specifying the interface name after the class name.

If an existing interface is not available, the developer can create one. For example, the following code shows an interface definition named IStoreTransactions that is suitable for use with the example OrderTransactions class shown in the topic Objects That Derive From MarshalByRefObject. It exposes a single method and a single property that implementing classes must expose.

interface IStoreTransactions
{
    int CountOrderItems(string customerName);
    string InvoiceGreeting  { get; set; }
}
'Usage
Interface IStoreTransactions
  Function CountOrderItems(ByVal customerName As String) As Integer
  Property InvoiceGreeting() As String
End Interface

The developer then references and implements this interface in their target class. For example, this class implements the IStoreTransactions interface shown in the preceding code example.

class OrderTransactions : IStoreTransactions
{
  private string greeting;

  public int CountOrderItems(string customerName)
  {
    int orderCount = 0;
    // Code here to get count of number of orders from database.
    // ...
    return orderCount;
  }

  public string InvoiceGreeting
  {
    get { return greeting; }
    set { greeting = value; }
  }

}
'Usage
Class OrderTransactions
  Implements IStoreTransactions

  Private greeting As String

  Public Function CountOrderItems(ByVal customerName As String) As Integer _
    Implements IOrderTransaction.CountOrderItems
      Dim orderCount As Integer = 0
      ' Code here to get count of number of orders from database.
      ' ...
      Return orderCount
  End Function

  Public Property InvoiceGreeting() As String _
    Implements IOrderTransaction.InvoiceGreeting
    Get
      Return greeting
    End Get
    Set(ByVal value As String)
      greeting = value
      End Set
  End Property

End Class

Notice that, in Visual Basic .NET, the developer must specify which interface member applies to each member of the concrete implementation. A compile error will result if the developer fails to implement correctly the interface members in the target class.