HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Inventories, Containers and Filters

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
containersinventoriesandfilters

Problem

Coming off a comment on this question, I figure it cannot hurt to post my Inventory work here as well.

Basically, I have Item, which is a base item, and then Container, which is a specialized item that has sub-items in it. The Container object, however, can be filtered to only permit (or restrict) certain items by some "property" X.

The Container objects can have any number of filters on them, restricting different things. For example, all Container objects have an OversizeContainerFilter, which essentially prevents the container from filling up over the maximum Weight it can hold. The Inventory Container also has an ItemTypeContainerFilter to restrict it to only be permitted to contain other Container items.

Now, this is actually a pretty complex portion of the setup. Essentially, an Item is as follows:

/// 
/// A base class for an item.
/// 
public abstract class Item
{
    /// 
    /// Gets or sets a unique System.Guid to represent the  globally.
    /// 
    public Guid Guid { get; set; }

    /// 
    /// Gets or sets a string of the description of the .
    /// 
    public string Description { get; set; }

    /// 
    /// Gets or sets a string of the name of the .
    /// 
    public string Name { get; set; }

    /// 
    /// Gets or sets a float weight for the .
    /// 
    public virtual float Weight { get; set; }

    /// 
    /// Gets or sets a  representing the icon image of the .
    /// 
    public SingleIcon Icon { get; set; }

    /// 
    /// Gets or sets a List<Guid>> that represents the list of the s that make up this .
    /// 
    public List Materials { get; set; }

    /// 
    /// Determines how damaged the  is.
    /// 
    public ushort Wear { get; set; }

    /// 
    /// Determines what  the  is. Must be overriden in derived classes.
    /// 
    public virtual ItemType ItemType { get { return ItemType.None; } }
}


These are pretty simple, an Item has certain, base, properties.

Where it starts

Solution

For your Container class, the indexer probably should be read only. The Add and Insert methods have CanAddItem checks. But this line:

public Item this[int index] { get { return _Items[index]; } set { _Items[index] = value; } }

could allow someone to replace the value with an item that fails the CanAddItem check. I wouldn't suggest adding extra checks in the setter. It really should be a readonly property.

public Item this[int index] { get { return _Items[index]; } }

Code Snippets

public Item this[int index] { get { return _Items[index]; } }

Context

StackExchange Code Review Q#101645, answer score: 5

Revisions (0)

No revisions yet.