Cork
Cork

Reputation: 742

Object with user-configurable property "visibility"

I have a User object with a bunch of properties. I have a requirement that states when a user sets up their information, they need the ability to state which properties of their profile are visible to others.

The way I had envisioned this was adding an additional property - a list of string that would contain the property names that were publicly visible. I could then implement a method called ToPublicView() or something similar that would use reflection to set non-public properties to null or default.

Is this a reasonable approach, or is there a better way?

Upvotes: 5

Views: 673

Answers (4)

Ivo
Ivo

Reputation: 8352

I think it's the simplest of the options. If reflection start to kill your performance, you may want to have a dictionary of property-delegate for accessing the values.

And as the requirement is not to have dynamic properties but just to mark the existing ones, it doesn't make sense to have all the properties in a dynamic way (like a list of property objects). Also, having them as actual properties will make the code more readable when you have to use it for the rest of the application.

Upvotes: 1

Tigran
Tigran

Reputation: 62248

Can use, may be, some combination of custom DynamicObject implementation

EDIT

  //custom property class
  public class MyProperty
  {
      public bool IsVisible { get; set; }
      public string PropertyName { get; set; }
  }

  public class Dymo: DynamicObject
  {

      Dictionary<MyProperty, object> dictionary
          = new Dictionary<MyProperty, object>();

      public override bool TryGetMember(
          GetMemberBinder binder, out object result)
      {
          result = false;
          var prop = PropertyFromName(binder.Name);
          if (prop != null && prop.IsVisible)
              return dictionary.TryGetValue(prop, out result);

          return false;

      }

      public override bool TrySetMember(
          SetMemberBinder binder, object value)
      {

          var prop = PropertyFromName(binder.Name);
          if (prop != null && prop.IsVisible)
              dictionary[prop] = value;
          else
              dictionary[new MyProperty { IsVisible = true, PropertyName = binder.Name}] = value;
          return true;
      }

      private MyProperty PropertyFromName(string name)
      {
          return (from key in dictionary.Keys where key.PropertyName.Equals(name) select key).SingleOrDefault<MyProperty>();
      }


      public void SetPropertyVisibility(string propertyName, bool visibility)
      {
          var prop = PropertyFromName(propertyName);
          if (prop != null)
              prop.IsVisible = visibility;         
      }
  }

and use this, after like this.

dynamic dynObj = new Dymo();
dynObj.Cartoon= "Mickey" ;
dynObj.SetPropertyVisibility("Mickey", false); //MAKE A PROPERTY "INVISIBLE"

Upvotes: 0

Steven Doggart
Steven Doggart

Reputation: 43743

In such a situation, if possible, I would suggest simply having a list of your properties, such as:

public Class Property<T>
{
    public Property(string name, bool visible, T value)
    {
        Name = name;
        Visible = visible;
        Value = value;
    }

    string Name { get; set; }
    bool Visible { get; set; }
    T Value { get; set; }
}

Then you could create a list of the properties like this:

List<Property> properties = new List<Property>();
properties.Add(new Property<string>("FirstName", true, "Steve"));

If you need to be able to set the visibility today, you may need to set other meta-properties as well tomorrow. Color? Required/Optional? Size? Etc. Having your own Property type allows you to easily expand it in the future.

Upvotes: 1

GregRos
GregRos

Reputation: 9103

What? No. If that's the demand, then User properties should not be realized with actual properties, but instead using some sort of IEnumerable and Property objects, where each Property has its visibility, etc.

Upvotes: 0

Related Questions