List or BusinessObjectCollection?

后端 未结 18 1023
庸人自扰
庸人自扰 2020-12-23 11:11

Prior to C# generics, everyone would code collections for their business objects by creating a collection base that implemented IEnumerable

IE:

publi         


        
相关标签:
18条回答
  • 2020-12-23 11:32

    I've been going back and forth on 2 options:

    public class BusinessObjectCollection : List<BusinessObject> {}
    

    or methods that just do the following:

    public IEnumerable<BusinessObject> GetBusinessObjects();
    

    The benefits of the first approach is that you can change the underlying data store without having to mess with method signatures. Unfortunately if you inherit from a collection type that removes a method from the previous implementation, then you'll have to deal with those situations throughout your code.

    0 讨论(0)
  • 2020-12-23 11:32

    I do the exact same thing as you Jonathan... just inherit from List<T>. You get the best of both worlds. But I generally only do it when there is some value to add, like adding a LoadAll() method or whatever.

    0 讨论(0)
  • 2020-12-23 11:32

    At the most of the time I simply go with the List way, as it gives me all the functionality I need at the 90% of the time, and when something 'extra' is needed, I inherit from it, and code that extra bit.

    0 讨论(0)
  • 2020-12-23 11:38

    I prefer just to use List<BusinessObject>. Typedefing it just adds unnecessary boilerplate to the code. List<BusinessObject> is a specific type, it's not just any List object, so it's still strongly typed.

    More importantly, declaring something List<BusinessObject> makes it easier for everyone reading the code to tell what types they are dealing with, they don't have to search through to figure out what a BusinessObjectCollection is and then remember that it's just a list. By typedefing, you'll have to require a consistent (re)naming convention that everyone has to follow in order for it to make sense.

    0 讨论(0)
  • 2020-12-23 11:39

    It's recommended that in public API's not to use List<T>, but to use Collection<T>

    If you are inheriting from it though, you should be fine, afaik.

    0 讨论(0)
  • 2020-12-23 11:39

    You can use both. For laziness - I mean productivity - List is a very useful class, it's also "comprehensive" and frankly full of YANGNI members. Coupled with the sensible argument / recommendation put forward by the MSDN article already linked about exposing List as a public member, I prefer the "third" way:

    Personally I use the decorator pattern to expose only what I need from List i.e:

    public OrderItemCollection : IEnumerable<OrderItem> 
    {
        private readonly List<OrderItem> _orderItems = new List<OrderItem>();
    
        void Add(OrderItem item)
        {
             _orderItems.Add(item)
        }
    
        //implement only the list members, which are required from your domain. 
        //ie. sum items, calculate weight etc...
    
        private IEnumerator<string> Enumerator() {
            return _orderItems.GetEnumerator();
        }
    
        public IEnumerator<string> GetEnumerator() {
            return Enumerator();
        }    
    }
    

    Further still I'd probably abstract OrderItemCollection into IOrderItemCollection so I can swap my implementation of IOrderItemCollection over in the future in (I may prefer to use a different inner enumerable object such as Collection or more likley for perf use a Key Value Pair collection or Set.

    0 讨论(0)
提交回复
热议问题