Yoann. B
Yoann. B

Reputation: 11143

Standard Methods vs Extensions Methods

Assuming the following domain entity :

public enum Role
{
    User = 0,
    Moderator = 1,
    Administrator = 2
}

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public Role Role { get; set; }
}

I need to know if the user can perform "Edit" action. So i've 2 solutions :

Create a CanEdit method inside the User entity

public class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public Role Role { get; set; }

    public bool CanEdit()
    {
        return Role == Role.Moderator || Role == Role.Administrator;
    }
}

Create a CanEdit Extension Method for User type :

public static class UserExtensions
{
    public static bool CanEdit(this User user)
    {
        return user.Role == Role.Moderator || user.Role == Role.Administrator;
    }
}

Both solution works, but the question is WHEN use standard methods vs using Extensions methods ?

Upvotes: 6

Views: 280

Answers (5)

M4N
M4N

Reputation: 96596

I mostly agree with Aaronaught's answer, but consider this:

Maybe your CanEdit() method or other similar methods (business rules) might change more or less often or depend on some external factors. Or over time, you will have more and more such rules (for different concerns). In that case you might want to keep them in a different place, separated from the domain model to ensure that the domain model doesn't have too many different responsibilities and doesn't need to change very often.

Then, one way can be to implement them as extension methods, because this allows you to keep these business rules separate from your domain model (e.g. User class), but the method is still easily discoverable by users of the User class.

Another way to implement such business rules would be the specification pattern, where you implement each rule as a separate (specification-) class, e.g. demonstrated in this blog post.

Upvotes: 2

Benjamin Podszun
Benjamin Podszun

Reputation: 9837

I agree with Aaronaught here: Implement your own logic the old-fashioned way. Static method might cause issues (missing using statement and the method seems to be "missing") lateron.

Something inherent to your model should be part of your classes.

Upvotes: 1

Peter
Peter

Reputation: 38555

if you do not have direct access to the source code for the class you should use Extensions methods if you do have access to the source code i see no reason to not use a standard methods...

Upvotes: 1

pdr
pdr

Reputation: 6440

There is very little point in using Extension methods for the sake of using them. If the method belongs to the class, use it there. Extension methods are for extending things, use them when you have no control over the class, or for giving functionality to an interface where the functionality should apply to all classes derived from that interface.

Upvotes: 1

Aaronaught
Aaronaught

Reputation: 122674

Extension methods are simply syntactic sugar for plain, ordinary static methods.

If you control the class structure, you should implement all the necessary functionality within the class. Where extension methods are really useful/necessary is if you don't own the class that you are trying to "extend."

For this example, I think you should put the logic inside the User class. It is a logical function of the user itself; consumers should be able to use the CanEdit() method without having to use or even know about the UserExtensions class.

Upvotes: 11

Related Questions