Amazon.com Widgets

Framework Design Guidelines: Factored Types

Continuing in our weekly blog post series that highlights a few of the new image[5]_thumb[2]_thumb[2]_thumbadditions to the Framework Design Guidelines 2nd edition.. This content is found in the Factored Types section of Chapter 9: Common Design Patterns. Phil offers some great additions to the base pattern.

PHIL HAACK

Since Factored Types have an explicit lifetime, it probably makes good sense to implement the IDisposable interface so that developers can make use of the using statement. The code sample here could then be refactored to:

using(SerialPort port = new SerialPort("COM1")) {
  
port.Open();
  
GZipStream compressed;
  
compressed = new GZipStream(port.BaseStream,
      CompressionMode.Compress);
   
compressed.Write(data, 0, data.Length);
}

Published 23 February 09 06:03 by BradA

Comments

# Anith » Framework Design Guidelines: Factored Types said on February 23, 2009 10:15 PM:

PingBack from http://www.anith.com/?p=12905

# What's New said on February 23, 2009 11:27 PM:

Continuing in our weekly blog post series that highlights a few of the new additions to the Framework

# Wesley said on February 24, 2009 5:15 AM:

And a factored type is ...?

Cheers,

Wes

# Ooh said on February 24, 2009 10:31 AM:

Is there a specific reason why you don't use the using statement with the GZipStream?

# Nicholas Paldino [.NET/C# MVP] said on February 24, 2009 10:50 AM:

It would be helpful if we knew what "the code sample here" was.  Having the "after" without the "before" is pretty meaningless.

# Daniel said on February 24, 2009 12:41 PM:

I'm not familiar with what a "Factored Type" is either.  A google search did land me on this interesting patent, though:

http://www.patentstorm.us/patents/7430732/description.html

Which makes me wonder if I'm legally allowed to use them?

# Kevin Daly said on February 24, 2009 11:58 PM:

First time I've heard the term.

@Daniel : Software patents are evil (and it's idiotic that they're granted), but patents on APIs are positively demented.

Just saying, s'all.

# Gavin Greig said on February 25, 2009 6:39 AM:

From reading the book, but trying to put it into my own words so that I'm not stealing thunder, Factored components are the potentially complex types underlying a simpler facade that is referred to as an Aggregate Component.

The simple facade may expose the underlying types for developers to implement more complex operations that the facade doesn't support.

(I think) Phil Haack's tip is that all the types involved should implement IDisposable so that it's easy for developers who use the API to manage the objects' lifetimes efficiently, through use of the "using" statement.

Read the book for a definitive explanation.

# Bert Huijben said on February 25, 2009 8:08 PM:

The example code ignores that the gzip stream buffers data to allow its compression.

For the example to work it should flush (or better yet: close) the GZipStream before closing the serial port.

(A nested using would probably give the cleanest example)

New Comments to this post are disabled

Search

This Blog

Syndication

Page view tracker