C#C
C#10mo ago
Core

✅ Best approach for returning typed objects

Hello,
I am porting a popular UA parser library called DeviceDetector to .NET.
The library determines the client from the user agent, which can be a browser, media player, feed reader, etc.

With the current implementation, there is an interface that all client models implement, and the returned object after parsing is of type
IClientInfo
. This means that users would need to perform type checking and casting to determine the exact client type. As I see it, there are two possibilities, each providing the user with a different way of handling the concrete client.

Which of these approaches would be the best to follow?

1st approach
c#
public interface IClientInfo
{
    string Name { get; init; }
    string? Version { get; init; }
}

public sealed class BrowserInfo : IClientInfo
{
    public string Name { get; init; }
    public string? Version { get; init; }
    public BrowserCode Code { get; init; }
    public string? Family { get; init; }
    public string? Engine { get; init; }
    public string? EngineVersion { get; init; }
}


// handling client after parsing
var result = DeviceDetector.Parse(userAgent);

if (result.Client is BrowserInfo browser)
{
  
}


2nd approach - it's similar to the 1st approach, but it is extended with an enum that represents the concrete type
c#
public interface IClientInfo
{
    ClientType Type {get; init; }
    string Name { get; init; }
    string? Version { get; init; }
}

public sealed class BrowserInfo : IClientInfo
{
    public ClientType Type {get; init; }
    public string Name { get; init; }
    public string? Version { get; init; }
    public BrowserCode Code { get; init; }
    public string? Family { get; init; }
    public string? Engine { get; init; }
    public string? EngineVersion { get; init; }
}

var result = DeviceDetector.Parse(userAgent);

// handling client after parsing
if (result.Client.Type == ClientType.Browser)
{
  var browser = (BrowserInfo)result.Client;
}
Was this page helpful?