Chris Eargle

Life Student of the Kodefu Arts

Exposing a Generic List

by chris 30. September 2008 13:35

You're writing a Customer class, and the Customer class contains a collection of Account objects. Because you want to add and remove accounts with ease, you implement this collection as a List<T>.

    public class Customer
    {
        
private List<Account> accounts = new List<Account>();

        
public List<Account> Accounts
        {
            
get { return accounts; }
        }
    }

Life is good. Your tests iterate through the accounts, add new accounts, and remove accounts. However, when you run FxCop, it complains that you shouldn't expose generic lists.

Do not expose List<T> in object models. Use Collection<T>, ReadOnlyCollection<T> or KeyedCollection<K,V> instead. List<T> is meant to be used from implementation, not in object model API. List<T> is optimized for performance at the cost of long term versioning. For example, if you return List<T> to the client code, you will not ever be able to receive notifications when client code modifies the collection.

FxCop is correct in its assessment as it becomes more difficult to later add underlying functionality. But I feel that it leaves out an important point. You shouldn't expose too much about your implementation, and this relays to the world that Customer uses a List<T>. Consumers of the Employee class don't care that you've implemented List<T>, they only care about the interface. Expose the public property as the interface the consumer should be using. In this example, our consumers want to utilize the interface IList<T>. Refactoring this is pretty easy: modify the property to be IList<Account>.

    public class Customer
    {
        
private List<Account> accounts = new List<Account>();

        
public IList<Account> Accounts
        {
            
get { return accounts; }
        }
    }

In other cases, the consumers may only want to iterate the collection without making modifications. In that situation, expose the list as IEnumerable<T>. The point is to take into account the interface that consumers want to utilize, then hide your implementation.

You shouldn't do it this way if the collection needs to be serialized. In that case, stick with one of the concrete classes such as Collection<T> like FxCop suggested.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

c#, design, interfaces

Kodefu

E-mail | Kick it! | DZone it! | del.icio.us
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Generics Don't Make Me SadI came across an interesting blog post today entitled, "C# generics make me sad..." by Matt Sheppard...Lazy Loading PropertiesHow to create a lazy loaded propertyParsing Properties and ItemsMSBuild files are loaded and parsed in order. This means that properties and items are in scope if t...
Saving the comment

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

January 6. 2009 02:43

Powered by BlogEngine.NET 1.3.1.0
Theme by Mads Kristensen

About the author

Chris Eargle Chris Eargle
Enterprise .NET developer in Columbia, SC.

E-mail me Send mail

Pages

  • Presentations

Twitter Updates

    Recent comments

    • Kicking Off 2009 In Style (2)
      Busby SEO Test wrote: Thanks for the post [More]
    • Kicking Off 2009 In Style (2)
      Justin James wrote: Congrats to both, particularly the C# MVP! I took … [More]
    • MDC Atlanta (6)
      SEO Dubai wrote: I forgot to congratulate you..:) [More]
    • MDC Atlanta (6)
      SEO Dubai wrote: wish you are happy in your new place.. [More]
    • WCF 3.5 Security Guidelines (9)
      Busby SEO Test wrote: nice work man [More]
    • Project Astoria September 2007 CTP Released (1)
      Busby SEO Test Pinay wrote: good luck to the project, i hope it gonna finish q… [More]
    • Load Balancing Header Issue (5)
      Busby SEO Test wrote: Me too [More]
    • WCF 3.5 Security Guidelines (9)
      Busby SEO Test wrote: nice work man [More]
    • WCF 3.5 Security Guidelines (9)
      Busby seo test wrote: Security indeed is very important in web developme… [More]
    • The Path Is Not a Legal Form (5)
      Busby SEO Test wrote: thanks for the information [More]

    Archive

    • 2009
      • January (1)
    • 2008
      • December (5)
      • November (4)
      • October (7)
      • September (8)
      • August (5)
      • July (1)
      • June (1)
      • April (4)
      • March (1)
      • February (4)
      • January (5)
    • 2007
      • December (5)
      • November (1)
      • October (6)
      • September (3)
      • August (1)
      • June (1)

    Tags

    • activex
    • addin
    • ado.net data services
    • ajax
    • architecture
    • asp.net
    • astoria
    • azure
    • beta
    • bug
    • c#
    • champs
    • code camp
    • com
    • community
    • communitycredit
    • consolas
    • continuous integration
    • conversion
    • ctp
    • database
    • deployment
    • design
    • design principles
    • download
    • ebook
    • entity
    • entlib
    • environment variables
    • expression blend
    • fail
    • font
    • framework
    • gadget
    • generics
    • grid
    • guidelines
    • icon
    • ineta
    • interfaces
    • jacksonville
    • lamdba
    • linq
    • linqtosql
    • list
    • live mesh
    • macro
    • mdc
    • mobile
    • msbuild
    • msdn
    • msi
    • mvc
    • mvp
    • powertoy
    • preview
    • properties
    • ray ozzie
    • refactoring
    • regasm
    • russ fustino
    • security
    • serialization
    • silverlight
    • snippet
    • source code
    • sql server
    • sql2008
    • sqlmetal
    • srss
    • starter kit
    • stream
    • string
    • tfs2008
    • trial
    • usability
    • ux
    • vb.net
    • vbscript
    • vista
    • visual studio
    • vs2008
    • wcf
    • web
    • winforms
    • wpf
    • xml

    Categories

    • RSS feed for Bleeding EdgeBleeding Edge (5)
    • RSS feed for Build NinjaBuild Ninja (2)
    • RSS feed for CEDGCEDG (2)
    • RSS feed for GeneralGeneral (1)
    • RSS feed for KodefuKodefu (21)
    • RSS feed for Path NotesPath Notes (10)
    • RSS feed for PresentationPresentation (5)
    • RSS feed for TechniquesTechniques (2)
    • RSS feed for TrainingTraining (5)
    • RSS feed for WeaponsWeapons (4)
    • RSS feed for ZenZen (5)

    Archive

    Blogroll

    • RSS feed for Structure Too BigStructure Too Big
      • Should you buy an exten...
      • WorldMaps Update
      • MSDN Roadshow -- coming...
    • RSS feed for Chris CraftChris Craft
      • Pimp My Phone – D...
      • Pimp My Phone – D...
      • Pimp My Phone – D...
    Download OPML file OPML

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2009

    Sign in