Is it better expose a collection or an API to the collection?

Take for example the following:

public interface IManager
{   
    void AddItem(CollectionItem item);
    void RemoveItem(CollectionItem item);
}

public interface IManagerCustomCollection
{
    ManagerCollection Systems;
}

Is it usually more preferable to create a collection, and allow the consumer to operate directly on the collection?

2

Is it usually more preferable to create a collection, and allow the consumer to operate directly on the collection?

It depends. Do you expect your user to create, manipulate, and otherwise use the collection? Or do you want to let them to do a few collection-like operations on something… that’s not really a collection?

But before you answer: consider that this is a code smell. Well, it’s two possible code smells:

  1. You’re making your own collection type. – The standard library collections (and interfaces) are good, extensive, and everyone knows them. Be really sure you need to before making a new one.
  2. You’re maybe violating single responsibility. – If you have some thing, and then that thing also has Add, Remove, etc… your thing might be doing two things. Look to separate those responsibilities.

All that said, I tend to prefer option 1. It provides stronger control over whatever weird invariants you need to protect if you’re not just using a List (or whatever other basic collection). Usually these sort of things need to associate the collection with some other instance, which gets hairy if you decouple the collection from its paired instance.

1

I would prefer to expose the API to the collection, that way when you are doing testing you don’t need to worry about constructing the proper collection and you could also do tests where the collection is null etc.

You would also be able to implement your own conditions on which type of items get added to the collection, i.e. no duplicates etc.

Expose access to the collection using an interface, such as IList<T>, ICollection<T> or ISet<T>. Developers are familiar with these interfaces while not tying it to a specific implementation. Throw a NotImplementedException for methods or properties you do not want to implement.

You lose some functionality. For example, as Jon Skeet points out, the interfaces are subsets of the features offered by concrete classes – IList<T> has fewer methods and properties than List<T>, for example. However, LINQ and similar mechanisms still work, unlike implementing your own interfaces or classes.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is it better expose a collection or an API to the collection?

Take for example the following:

public interface IManager
{   
    void AddItem(CollectionItem item);
    void RemoveItem(CollectionItem item);
}

public interface IManagerCustomCollection
{
    ManagerCollection Systems;
}

Is it usually more preferable to create a collection, and allow the consumer to operate directly on the collection?

2

Is it usually more preferable to create a collection, and allow the consumer to operate directly on the collection?

It depends. Do you expect your user to create, manipulate, and otherwise use the collection? Or do you want to let them to do a few collection-like operations on something… that’s not really a collection?

But before you answer: consider that this is a code smell. Well, it’s two possible code smells:

  1. You’re making your own collection type. – The standard library collections (and interfaces) are good, extensive, and everyone knows them. Be really sure you need to before making a new one.
  2. You’re maybe violating single responsibility. – If you have some thing, and then that thing also has Add, Remove, etc… your thing might be doing two things. Look to separate those responsibilities.

All that said, I tend to prefer option 1. It provides stronger control over whatever weird invariants you need to protect if you’re not just using a List (or whatever other basic collection). Usually these sort of things need to associate the collection with some other instance, which gets hairy if you decouple the collection from its paired instance.

1

I would prefer to expose the API to the collection, that way when you are doing testing you don’t need to worry about constructing the proper collection and you could also do tests where the collection is null etc.

You would also be able to implement your own conditions on which type of items get added to the collection, i.e. no duplicates etc.

Expose access to the collection using an interface, such as IList<T>, ICollection<T> or ISet<T>. Developers are familiar with these interfaces while not tying it to a specific implementation. Throw a NotImplementedException for methods or properties you do not want to implement.

You lose some functionality. For example, as Jon Skeet points out, the interfaces are subsets of the features offered by concrete classes – IList<T> has fewer methods and properties than List<T>, for example. However, LINQ and similar mechanisms still work, unlike implementing your own interfaces or classes.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Is it better expose a collection or an API to the collection?

Take for example the following:

public interface IManager
{   
    void AddItem(CollectionItem item);
    void RemoveItem(CollectionItem item);
}

public interface IManagerCustomCollection
{
    ManagerCollection Systems;
}

Is it usually more preferable to create a collection, and allow the consumer to operate directly on the collection?

2

Is it usually more preferable to create a collection, and allow the consumer to operate directly on the collection?

It depends. Do you expect your user to create, manipulate, and otherwise use the collection? Or do you want to let them to do a few collection-like operations on something… that’s not really a collection?

But before you answer: consider that this is a code smell. Well, it’s two possible code smells:

  1. You’re making your own collection type. – The standard library collections (and interfaces) are good, extensive, and everyone knows them. Be really sure you need to before making a new one.
  2. You’re maybe violating single responsibility. – If you have some thing, and then that thing also has Add, Remove, etc… your thing might be doing two things. Look to separate those responsibilities.

All that said, I tend to prefer option 1. It provides stronger control over whatever weird invariants you need to protect if you’re not just using a List (or whatever other basic collection). Usually these sort of things need to associate the collection with some other instance, which gets hairy if you decouple the collection from its paired instance.

1

I would prefer to expose the API to the collection, that way when you are doing testing you don’t need to worry about constructing the proper collection and you could also do tests where the collection is null etc.

You would also be able to implement your own conditions on which type of items get added to the collection, i.e. no duplicates etc.

Expose access to the collection using an interface, such as IList<T>, ICollection<T> or ISet<T>. Developers are familiar with these interfaces while not tying it to a specific implementation. Throw a NotImplementedException for methods or properties you do not want to implement.

You lose some functionality. For example, as Jon Skeet points out, the interfaces are subsets of the features offered by concrete classes – IList<T> has fewer methods and properties than List<T>, for example. However, LINQ and similar mechanisms still work, unlike implementing your own interfaces or classes.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật