totpero / DeviceDetector.NET

The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
Apache License 2.0
343 stars 73 forks source link

Improving documentation #83

Open mysteryx93 opened 1 week ago

mysteryx93 commented 1 week ago

I've been spending quite a lot of time trying to turn this class into a simple service I can inject on my page and call "IsMobile".

It's really not that simple with the current documentation. None of the code here is structured for Dependency Injection in ASP.NET.

It requires proper caching or can cause some performance issues. What's the proper way of setting up caching with dependency injection in ASP.NET? The comment here states that static cache isn't the best here; and just creating a new cache on every request is pointless. The cache must be created and configured only once for the duration of the app.

// OPTIONAL: Set caching method
// By default static cache is used, which works best within one php process (memory array caching)
// To cache across requests use caching in files or memcache
// add using DeviceDetectorNET.Cache;
dd.SetCache(new DictionaryCache());

The comment also points to LRUCachedDeviceDetector.GetDeviceDetector(userAgent); which behaves completely different from the line above. SetCache makes the DeviceDetector work with a cache; but this line (maybe?) returns a DeviceDetector from a cache, with no clientHints.

If all I need is to know whether the user is a mobile device, the caching should be used too?

What I need is to be able to put @inject DeviceDetector DeviceDetector on a page and @if (DeviceDetector.IsMobile(Request)) on the page with no further complications. I shouldn't have to spend hours trying to figure it out.