Applying the Singleton Design Pattern in .NET Remoting
page 3 of 5
by Joseph Chahine
Feedback
Average Rating: 
Views (Total / Last 10 Days): 22724/ 48

Solution

Many solutions to this problem can be found; the one below may not be the best, but it suits me well.  Some developers may instantiate the Combos class in each Remote Object.  I find this solution to be resource consuming.

What I would do is to create a single instance of Combos shared by all remote objects created on the Application Server.  The way to do so is to apply the Singleton Design Pattern to this class. Here is a sample showing how this can be done.

In the Business Tier you will be defining the Combos class as listed in Listing 1.

Listing 1

Public Class Combos
    Inherits MarshalByRefObject
 
    Private Shared _Instance As Combos

     Public Shared ReadOnly Property Instance() As Combos
        Get
             'Using lazy instantiation
            If _Instance Is Nothing Then _Instance = New Combos

             Return _Instance
        End Get
    End Property
 
    'Note that the constructor method is Protected (it can be Private)
    Protected Sub New()
        'Write some code here
    End Sub
 
End Class

As you can see, if it exists, an instance of Combos is accessed by all Remote Objects through the Instance property.  If it does not exist, a new (and unique) instance of Combos will be created and returned through the same Instance property.

In the Presentation Tier all you need to do is to call the Instance property and assign its return value to a variable of type Combos as shown in Listing 2.

Listing 2

Private Sub GetCountries()
  Dim Cbo As Combos = Combos.Instance
   ddlCountries.DataSource = Cbo.GetCountries()
   ddlCoutries.DataTextField = "CountryName"
   ddlCountries.DataValueField = "CountryId"
  ddlCountries.DataBind()
End Sub

View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-11-29 10:32:07 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search