Extend the User object - Would you?

Vote:
 

Hello all,

I'm interested in your opinions on this one, because Relate+ is implemented one way, and in the documentation (Developers handbook, by Netstar) recommends extending your objects (Blog, User, Image etc.) with new, inherited objects, for use in your project. Today Relate+ is using a lot of extension methods that are great for some uses, but I'm not convinced it is the best way to go about when you are using Attributes on a user.

So, what are your experiences and advice?

Examples:

Today the way of getting a users attribute for Region is implemented with an extension method

public static string GetRegion(this IUser user)
{
    string s = user.GetAttributeValue("Region");
    if (string.IsNullOrEmpty(s))
    {
      return string.Empty;
    }
    return s == "-1" ? string.Empty : s;
}

But would it not be more correct to utilize inheritance and good OO practice to har your own User object that implements those methods as Properties like this?

public class CustomUser : EPiServer.Common.Security.User
    {
        public string JobTitle
        {
            get
            {
                string s = this.GetAttributeValue("Jobtitle");
                if (string.IsNullOrEmpty(s) || s == "-1")
                    return string.Empty;
                return s;
            }
            set
            {
                this.SetAttributeValue("JobTitle", value);
            }
        }
    }

and then use

 

CustomUser user...;

string jobTitle = user.JobTitle

and be happier?

 

Go debate! :)

#40972
Jun 25, 2010 13:11
Vote:
 

Bump! This area need attention - how to properly extend the IUser/User object for your project. Or is eveyone really happy with calling GetAttributeValue or using a lot of extension methods?

#42001
Aug 05, 2010 18:00
Vote:
 

I think this very much depends on the usage scernario.

If you are only adding strong-typed access to a few values then extensions methods are all well and good.

Any more than a few, and I I think that you're be better off creating a CustomUser class that inherits from the User class. This object more accurately reveals your code's intentions. That is, that a CustomUser is a significant piece of your domain puzzle.

#42034
Aug 06, 2010 10:56
This thread is locked and should be used for reference only. Please use the Legacy add-ons forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.