Visual Studio Fonts and Colors: Version 2

March 28, 2011

Some time ago I wrote about my chosen colour scheme for Visual Studio.  I’ve updated it several times since, so I’ve uploaded the latest version to StudioStyles. It looks like this:

Unfortunately, StudioStyles doesn’t support all the styles for VB.Net, so you’ll have to tweak it a bit if you want VB.Net goodness.

Advertisements

SQL Server: Drop all Foreign Keys that Reference a Table

March 17, 2011

This post has moved to Yagni.net.

 


Design Pattern: Object Pool

February 7, 2011

Type

Creational Patterns

Name

Object Pool

AKA

Resource Pool

Motivation

Performance can be an important consideration for applications. In some scenarios, object creation is a costly step.

Intent

Avoids cost of initializing objects by maintaining a pool of pre-initialized objects that can be re-used.

Description of Benefits

Can offer a performance boost where:

  • object instantiation is expensive
  • instances of the class are frequently created
  • number of instances at any one time is small

Can make initialization time predictable where it would otherwise be unpredictable (e.g. when squiring resources over a network)

Known Uses

Instantiation of objects that represent:

  • database connections
  • socket connections
  • threads
  • large graphic objects

Real-World Illustrations

  • Shoe shelf at a bowling club
  • Car pooling
  • Library

Participants

Reusable: An object used by Client until they it is no longer required.

Client: Uses an instance of Reusable for a limited amount of time

ReusablePool: Manages the collection of Reusable objects by:

  • creating new instances of Reusable
  • supplying instances of Reusable to Clients

Typical methods induce:

  • getInstance: A static method that returns an instance of ReusablePool
  • aquireReusable:A method that returns an instance of Reusable
  • releaseReusable(Reusable):A method that returns a Reusable to the pool

Basic Implementation (Collaboration)

Object Creation

The Client is responsible for requesting the Reusable from the ReusablePool.

On receiving a request for an object, the ReusablePool will attempt to supply an suitable Reusable.

Object Use

The Client should be unaware that the Reusable can be shared with other Clients:  from the Client‘s point of view, the Reusable can be treated in exactly the same way as an object that has been created in any other way,the only difference being that it must return it to the ReusablePool once it has finished using it.

Object Release

The Client is responsible for returning Reusables to the ReusablePool once it is finished with.

Implementation Details

Resource Loading Strategy

Several strategies are available, for example:

  • Eager: A specified number of Reusables are created by the ReusablePool when the ReusablePool is instantiated.
  • Lazy: Reusables are not created by the ReusablePool until they are requested by the a Client. Once Reusables are released, they are immediately available for other Clients.
  • Hybrid: A specified number of Reusables are created eagerly, but additional Reusables are created lazily.
  • Lazy-Expanding: Creates resources lazily, but doesn’t re-use them until the ReusablePool reaches a certain size.
  • Eager-Expanding: Creates resources eagerly. Creates additional resources when available objects in the ReusablePool drops below a certain threshold.

Maximum Pool Size

Some implementations may set a maximum number of Reusables in the ReusablePool

Empty Pool

Various strategies can be adopted to handle the situation where a Client requests an instance from the pool, but none is currently available:

Prevent the situation by ensuring that the ReusablePool will always contain enough Reusables.

  • Fail to provide Reusable, and inform the Client that none is available
  • Create a new Reusable, thus increasing the size of the pool
  • Block until another thread to releases an object back into the pool
  • Forcible reclaim a Reusable from a low-priority Client

Synchronization

In a multi-threaded environment, careful consideration must be given to synchronisation of methods on the ResourcePool object.

Failed Release

If the Client fails to return a Reusable to the pool, then it will be unavailable to other Clients. To avoid this, the ResourcePool could implement an expiry time for Reusable:
if the Reusable has not actually been used for a certain length of time, it can be made available to other Clients.

Resource Eviction

In some situations, it may be undesirable to hold unused Reusables in the ReusablePool for long periods of time. In this case, it may be desirable to evict Reusables from the ReusablePool if they have not been requested by a Client for a specified length of time.

Recycling Methods

Some types of Reusables may need to be reset to a know state before they can be allocated to another Client. This is the responsibility of the ReusablePool.

Variants

  • Sub-pools
  • Mixed Pools

Relationship with Other Patterns

ReusablePool is often implemented as a Singleton.

References


Solution: Service Unavailable after ASP.Net Publish

October 26, 2010

If you receive a “Service Unavailable” screen after publishing an ASP.Net web site, check the application pool assigned to the site. The pool you use needs to be assigned the right version of the .Net framework. For example, a .Net 2.0 site won’t work if the pool is using .Net 1.0.


Exception Passing DBNull.Value to a Varbinary Using Parameters.AddWithValue

June 23, 2010

In .Net it seems reasonable to try to pass a NULL to a SQL Server stored procedure using the following syntax (C#):

myCommand.Parameters.AddWithValue("@MyParameter", DBNull.Value);

Unfortunately, if the sproc is expecting a parameter of type Varbinary(max), the following exception will be thrown:

Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

This appears to be a bug in the .Net SqlDataAdapter.

The work-around is to be explicit about the type of data you are passing:

myCommand.Parameters.Add("@MyParameter", SqlDbType.VarBinary, -1);
myCommand.Parameters["@MyParameter"].Value = DBNull.Value;

My thanks go to dnagelhout and Matt Neerincx for their invaluable posts on this subject.


BBC Micro High Scores!

May 11, 2010

Pengo 46330

Labyrinth 63870

Monsters 10620

Snapper 41820

Air Lift 70460

Zalaga 59490

Pole Position 56600

Nemesis 16290

Space Eagles 11880

Starship Command 889

Chucky Egg 235330

Snake (fast) 1016

Planetoid 115400

Mr EE 103100


Three Distractions

May 11, 2010

Some quick notes:

The Desire for Possessions The Desire for Physical Pleasure The Desire for Power and Prestiege
(Pride)
Genesis 3 Pleasant to the eyes Good for food Desire to be wise
1 Jn 2:15-17 Lust of the eyes Lust of the flesh Pride of life
Matthew 13 Riches Pleasures Cares (using own power)
Matthew 4:13 Kingdoms of the World Stones to Bread Throw yourself down (you’re so important, God won’t let youu hurt yourself)
Ezekiel 16:49 fulness of bread abundance of idleness was in her and in her daughters … neither did she strengthen the hand of the poor and needy pride
Remedy Giving Fasting Prayer

Further Reading: Warfare on Three Fronts