views:

152

answers:

2

Hi,

I have a class called MembershipHelper, which I am using in my ASP.NET project. It looks like this:

Public Class MembershipHelper

    Public Shared Function IsMultiStoreUser() As Boolean
     return Roles.IsUserInRole(....)
    End Function

    Public Shared Function IsAdmin() As Boolean
     return Roles.IsUserInRole(....)
    End Function

    Public Shared Function IsReaderOnly() As Boolean
     return Roles.IsUserInRole(....)
    End Function

End Class

I read somewhere that its not a good idea to have a class with just shared functions - but I don't remember where.

Why is this bad and how can I improve it?

Thank you

+2  A: 

Shared functions are like static functions, which in turn are like global functions or objects.

What you are essentially doing in your example is adding some redirection and abstraction, which I think is fine for Helper/Extension classes.

Daniel A. White
...and having global functions is bad?
vikasde
Yes...................
Daniel A. White
Why is that bad?
vikasde
http://c2.com/cgi/wiki?GlobalVariablesAreBad
Daniel A. White
Thanks for the link.
vikasde
Not in my book.
Daniel A. White
The linked article talks about global *variables*, not globally available methods. That's imho a difference. I don't think there is anything wrong with static helper functions in general, they are frequently used in the .NET Framework, e.g. File.Exists.
0xA3
Generally they are bad.
Daniel A. White
But you say in your answer that they are fine? Or am I missing something?
0xA3
In helper or extension methods like what you have, in my book they are generally acceptable.
Daniel A. White
+3  A: 

From the naming that you used for your functions it seems that all functions describe properties of a user (e.g. whether the user is an admin).

Therefore it would seem more natural** to have these functions replaced by properties of your user object or by having your user implement an IRole interface.

** I'm not saying that your design is good or bad. Depending on the context such a helper class might very well be reasonable.

0xA3
Ah.... that makes sense. I guess that will be the more elegant solution.
vikasde