SaravananArumugam
SaravananArumugam

Reputation: 3720

Add functionality to the property of property

I have an Object called Graph.

I have list of Vertices in Graph class. So the Class would somewhat look like this. Its a .Net 2.0 code.

public class Graph
{
    public List<Vertex> Vertices 
    {
        get{ return _vertices}
    }
} private List<Vertex> _vertices;

Here is my question. I would like to validate the Vertex object (by calling Vertex.IsValid() method) before adding it to the Graph object.

What's the best way to do it? I like the way we add code inside the property's getter block. Is there any similar way to it?

I have workarounds, but would like to do it in the best way possible.

Upvotes: 1

Views: 139

Answers (3)

Ken Beckett
Ken Beckett

Reputation: 1283

My solution for similar situations is to add a helper method to add Vertex objects to Graph, like this:

public void AddVertex(Vertex vertex)
{
    if (vertex.IsValid())
        _vertices.Add(vertex);
}

If you don't have a large number of Graph objects, and Graphs usually have Vertexs, I would pre-allocate the List collection, so it's always empty and never null (I think in this particular case that's probably a good idea). Otherwise, your AddVertex method would have to check for _vertices being null and allocate the List. In situations like that, I usually would make a CreateVertices() method that allocates the List if it's null, so that it can also be used in other methods of the class if necessary, and even by the user of the class. The easy way out is to always pre-allocate the collection, but I personally avoid that if there are large numbers of instances of the class and the collection can often be unused.

Granted, this solution is not perfect, since the List isn't immutable - a user of the class can use the Vertices property to get the list, and do a direct Add() to it. My only solution for that is documentation - such as a remarks tag on the Vertices property that explains the proper method of adding to it. C# could benefit from a way to flag returned collections as read-only. If you're desperate to do it, you could wrap List and make a read-only collection that can copy-construct itself from a normal List, and then return the immutable list with the property.

Upvotes: 2

Tigran
Tigran

Reputation: 62248

Can try something like this, a pseudocode :

EDIT

Extension method

    public static class MyCoolVertex
    {
        public static void AddVertexAndValidate(this List<int> list, Vertex value)
        {
            if(value.IsValid())
                list.Add(value);
        }
    }

And after this use AddVertexAndValidate method.

Good: Basically you done

Bad: You can not stop someone to call just Add on the list and in this way violate your workflow.

Hope this helps..

Upvotes: 1

dlev
dlev

Reputation: 48596

The best solution is to not expose the list of vertices as a mutable list. Instead, if you need a Vertices property, make it return a read-only data-structure. Then, just include an Add() method, and validate all you want:

public ReadOnlyCollection<Vertex> Vertices
{
    get { return _vertices.AsReadOnly(); }
}

public void Add(Vertex vertex)
{
    if (vertex.IsValid())
        _vertices.Add(vertex);
}

Upvotes: 2

Related Questions