0

I have a static class which is used to access a static concurrentdictionary:

public static class LinkProvider
{
    private static ConcurrentDictionary<String, APNLink.Link> deviceLinks;
    static LinkProvider()
    {
        int numProcs = Environment.ProcessorCount;
        int concurrencyLevel = numProcs * 2;
        deviceLinks = new ConcurrentDictionary<string, APNLink.Link>(concurrencyLevel, 64);
    }


    public APNLink.Link getDeviceLink(string deviceId, string userId)
    {
        var result = deviceLinks.First(x => x.Key == deviceId).Value;
        if (result == null)
        {      
           var link = new APNLink.Link(username, accountCode, new APNLink.DeviceType());
           deviceLinks.TryAdd(deviceId, link);
           return link;

        }
        else
        {
            return result;
        }
    }


    public bool RemoveLink(string deviceId)
    {
        //not implmented
        return false;
    }
}

how can I make use of this class in my controller in an asp.net

Ie I want to go:

LinkProvider provider;
APNLink.Link tmpLink = provider.getDeviceLink(id, User.Identity.Name);
//use my link

Back ground. The dictionary is used to save a link object between states / requests in a asp.net web api program. So when the service needs to use the link, it asks the linkprovider to find a link for it and if there isn't one it must create one. So I need the dictionary object to the same instance in all my http requests.

Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • If you are doing a static class, everything should be static. Have a private `bool` to flag if initialized, and take care of that at the beginning of `getDeviceLink`. – crashmstr Mar 11 '14 at 12:15
  • Do I really need a static class in my scenario then? Can I not just make the dictionary static? – Zapnologica Mar 11 '14 at 12:17
  • Do you *need* and instance of `LinkProvider` to work with the dictionary if the dictionary is static? Maybe. Depends on what else it is doing with *non static* data. But if not, a static class may be entirely appropriate. – crashmstr Mar 11 '14 at 12:19
  • In other words, if the dictionary is static and all of the methods only work with that dictionary and *don't* need any other data, then a static class is probably most appropriate. – crashmstr Mar 11 '14 at 12:21

1 Answers1

3

So I need the dictionary object to the same instance in all my http requests

Then use a static class, and make every method static too, so you could call it using the following syntax:

APNLink.Link tmpLink = LinkProvider.getDeviceLink(id, User.Identity.Name);

That being said, you should be aware that in-memory static variables in an ASP.Net application are not always safe to use, because your application isn't stateless and in case the application pool is recycled, your dictionary will be re-instantiated.

ken2k
  • 48,145
  • 10
  • 116
  • 176
  • Can I not initialize the class in my Startup class that asp.net provides where you normally add custom filters and routes? How does the initializing of the static class then work? – Zapnologica Mar 11 '14 at 12:37
  • @Zapnologica Well it's a bit hard to tell _exactly when_ the static constructor will be called, but in any case it will be called before any static method/member of the class is used in your code (for a static class, and before any instance method/member for a non-static class). – ken2k Mar 11 '14 at 12:41
  • So is my constructor fine in the example app? But will my constructor not be called every time I use the link provider – Zapnologica Mar 11 '14 at 12:43
  • @Zapnologica Yes, your constructor is fine, and it will be called only once. But getDeviceLink should be static. – ken2k Mar 11 '14 at 12:44