Dynamicweb 8 collection changes



The majority of collections in Dynamicweb are being upgraded in Dynamicweb 8 to support LINQ, IEnumerable and other cool stuff.


Background

Dynamicweb on the .NET platform was developed on .NET 1.1. At that time .NET did not support Generics or LINQ, and collections was back then based on System.Collections.CollectionBase. With the release of .NET 2 in the end of 2005, generics became available and later on (2006) LINQ with .NET 3.

Because most collections in Dynamicweb 7, including 67 eCommerce collections, are based on System.Collections.CollectionBase, they do not support the use of LINQ because of the lacking implementation of IEnumerable.

LINQ which comes in handy in many situations can therefore not be used in parts of Dynamicweb. We can’t have that! So with Dynamicweb 8 most collections are updated to support LINQ and all the other goodness that comes with that – slightly better performance, better sorting capabilities etc.

The change

Most collections in Dynamicweb are being upgraded from System.Collections.CollectionBase to System.Collections.ObjectModel.Collection(Of t). That means that the collections because of the richer base implementation with IEnumerable, better inner lists etc. gives you as developer new possibilities of working with these collections.

What does that mean?

If you have custom code that iterates through collections being upgraded, some of your code might need a minor upgrade because of the changed behavior of the collections due to the upgrade. Approximately 90 collections have changed – the most important ones being 67 eCommerce collections like ProductCollection, GroupCollection etc.

In most scenarios your code will not need any changes – iterating a collection is the same.

The good thing is that you can now use the power of the better collections in future implemenations.

The following code pieces illustrate the old and the new way of working with the collection accessors. The first code is an example from the Dynamicweb 7.2 API and then from Dynamicweb 8.0. The code is grouped by programming language.

Dynamicweb 7.2 C#

[Subscribe(eCommerce.ProductList.BeforePaging)]
    public class ProductListSubscriber : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            // Cast args object to the correct strongly-typed args type
            var myArgs = (eCommerce.ProductList.BeforePagingArgs)args;

            // Default Item Accessor: Get the first element
            var product1 = myArgs.Products[0];

            // No Default Item Accessor: Get the first element
            var discount = product1.Discounts.get_Item(0);

            // No Item Accessor at all: Get the first element
            Dynamicweb.eCommerce.Products.Detail detail;
            foreach (Detail loopDetail in product1.Details)
            {
                detail = loopDetail;
                break;
            }

            // Default ItemById Accessor:
            //  Get the relation with the given ID
            // Works more like a dictionary
            var relation = product1.PropertyRelations["TheID"];

            // Item Accessor: Get the first relation in the collection
            var relation3 = product1.PropertyRelations.get_Item(0);
        }
    }

Dynamicweb 8 C#

[Subscribe(eCommerce.ProductList.BeforePaging)]
    public class ProductListSubscriber : NotificationSubscriber
    {
        public override void OnNotify(string notification, NotificationArgs args)
        {
            // Cast args object to the correct strongly-typed args type
            var myArgs = (eCommerce.ProductList.BeforePagingArgs)args;

            // Default Item Accessor
            var product1 = myArgs.Products[0];

            // Default Item Accessor
            var discount1 = product1.Discounts[0];

            // Default Item Accessor
            var detail = product1.Details[0];

            // Default Item Accessor
            var relation = product1.PropertyRelations[0];

            // Changed to function
            var relation2 = product1.PropertyRelations.ItemById("TheID");
        }
    }

Dynamicweb 7.2 VB.NET

<Subscribe(BeforePaging)> _
Public Class ProductListSubscriber
    Inherits NotificationSubscriber

    Public Overrides Sub OnNotify(ByVal notification As String, ByVal args As NotificationArgs)
        ' Cast args object to the correct strongly-typed args type
        Dim myArgs As BeforePagingArgs = DirectCast(args, BeforePagingArgs)

        ' Default Item Accessor:
        '   Get the first product in the ProductCollection
        Dim product1 As Product = myArgs.Products(0)
        Dim product2 As Product = myArgs.Products.Item(0)

        ' No Default Item Accessor:
        '   Get the first discount in the Discounts collection
        ' Collections with no Default Item Accessor
        Dim discount As Discount = product1.Discounts.Item(0)

        ' No Item Accessor at all:
        '   Get the first detail in the DetailCollection
        Dim detail As Detail
        For Each loopDetail As Detail In product1.Details
            detail = loopDetail
            Exit For
        Next

        ' Default ItemById Accessor:
        '   Get the relation with the given ID
        ' Works more like a dictionary
        Dim relation1 As PropertyProductRelation = product1.PropertyRelations("TheID")
        Dim relation2 As PropertyProductRelation = product1.PropertyRelations.ItemById("TheID")

        ' Item Accessor:
        '   Get the first relation in the collection
        Dim relation3 As PropertyProductRelation = product1.PropertyRelations.Item(0)
    End Sub
End Class

Dynamicweb 8 VB.NET

<Subscribe(BeforePaging)> _
Public Class ProductListSubscriber
    Inherits NotificationSubscriber

    Public Overrides Sub OnNotify(ByVal notification As String, ByVal args As NotificationArgs)
        ' Cast args object to the correct strongly-typed args type
        Dim myArgs As BeforePagingArgs = DirectCast(args, BeforePagingArgs)

        ' Default Item Accessor
        Dim product1 As Product = myArgs.Products(0)
        Dim product2 As Product = myArgs.Products.Item(0)

        ' Default Item Accessor
        Dim discount1 As Discount = product1.Discounts()(0)
        Dim discount2 As Discount = product1.Discounts.Item(0)

        ' Default Item Accessor
        Dim detail1 As Detail = product1.Details()(0)
        Dim detail2 As Detail = product1.Details().Item(0)

        ' Default Item Accessor
        Dim relation1 As PropertyProductRelation = product1.PropertyRelations(0)
        Dim relation3 As PropertyProductRelation = product1.PropertyRelations.Item(0)

        ' Changed to function
        Dim relation2 As PropertyProductRelation = product1.PropertyRelations.ItemById("TheID")
    End Sub
End Class


What else?

Not much – an important upgrade to a lot of objects in Dynamicweb – lots of new benefits and easy to upgrade from 7.2 to 8.0.

Hope you will enjoy this change! Let me know if you have any other input.




2 Comments

  1. Kevin Steffer 26 October 2011 20:09
    Long waited - awesome, know we have to delete all our list.Cast<OrderLine>().ToList() stuff :)

  2. Imar Spaanjaars 28 October 2011 13:25
    Great stuff indeed. One remark: if ItemById is now a method, shouldn't it be better called GetItemById? And shouldn't it be ID with two capitals? I prefer Id, but since there are already lots of situations where ID is used, I think it's better to be consistent.....

    Imar

Lea