Dynamic property in Static class

Asked

Viewed 278 times

1

I have a static class and in it I have a method too static. The method returns a value that is cached, however Key, it has the dynamic value:

key = User.Identity.GetUserName();
public static class MeuHelper {

   public static string Get {
      get {
         return cache.Get(key);
      }
   }
}

How could I declare this property KEY?

I tried the following statements

public static string Key = User.Identity.GetUserName();
public static string Key {
 get {
    return User.Identity.GetUserName();
 }
}

And even:

public static string Key {Get;set;}

In the builder:

static MeuHelper {
   Key = User.Identity.GetUserName();
}

When I go thresh, I do it this way:

Logo with a user in the browser, and in another anonymous tab, soon with another user, however always prevails the value of the first user.


Editing:

The helper is for cache usage. It must have a key for each logged in user, for this reason I use the username, which is unique per user.

So the idea is to have the key in String, to which the username of the logged-in user.

  • What kind of cache?

  • @The cache is redis, the same must have a unique key for each user session, so use the Username

2 answers

2


The most elegant way to do what you want would be to create an extension method for IPrincipal. User implements IPrincipal:

public static class UserExtensions 
{
    private static Object threadLock = new Object();

    public static String RedisIdentifier(this IPrincipal User) 
    {
        lock (threadLock)
        {
            return cache.Get(User.Identity.GetUserName());
        }
    }
}

I’ve already done thread-safe to avoid any competition problems in its application.

-1

As you mentioned in browser, I’ll assume you’re developing a web application.

In that case, the answer is: nay use mutable static state!

Using static status in a web application is a recipe for disaster. At any time, the server may be serving n requests simultaneously, where they all access shared variables, and it is difficult to properly synchronize access to those variables.

The only "acceptable" case to use static state is when it comes to read-only, immutable state, or when that state really needs to be shared by all requests (like the cache example).


That said, I think you should transform the property Get in a method GetCacheItem (assuming that the instance cache is thread-safe or that access to it is properly synchronized)

public static class MeuHelper
{
   public static string GetCacheItem(string key)
   {
       return cache.Get(key);
   }

   public static string GetCacheItemForUser(IPrincipal user)
   {
       return GetCacheItem(user.Identity.GetUsername());
   }
}

Each request must call the method and pass its own key:

var item = MeuHelper.GetCacheItemForUser(User);
  • the idea of creating helper class is not to repeat code, however despite its implementation resolve, it ends up repeating the call User.Identity =/

  • @Rod I agree with the idea of encapsulating repeated code, but this encapsulation does not have (and should not) to be static. I updated the example to demonstrate how to encapsulate repeated code without creating static state

  • Because the downvote?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.