• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

net-daemon / netdaemon / 17477775006

04 Sep 2025 10:02PM UTC coverage: 83.95% (-0.1%) from 84.091%
17477775006

Pull #1326

github

web-flow
Merge c789af775 into 25a52e371
Pull Request #1326: Initial version of Func style Apps

859 of 1145 branches covered (75.02%)

Branch coverage included in aggregate %.

17 of 22 new or added lines in 4 files covered. (77.27%)

4 existing lines in 2 files now uncovered.

3362 of 3883 relevant lines covered (86.58%)

534.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/src/AppModel/NetDaemon.AppModel/Internal/AppFactories/FuncAppFactory.cs
1
using System.Reflection;
2

3
namespace NetDaemon.AppModel.Internal.AppFactories;
4

5
internal class FuncAppFactory : IAppFactory
6
{
7
    private readonly Func<IServiceProvider, object?> _func;
8

9
    private FuncAppFactory(Func<IServiceProvider, object?> func, Type type, string? id, bool? focus)
154✔
10
    {
11
        _func = func;
154✔
12

13
        Id = id ?? GetAppId(type);
154✔
14
        HasFocus = focus ?? GetAppFocus(type);
154✔
15
    }
154✔
16

17
    private static string GetAppId(Type type)
18
    {
19
        var attribute = type.GetCustomAttribute<NetDaemonAppAttribute>();
153✔
20
        var id = attribute?.Id ?? type.FullName;
153✔
21

22
        if (string.IsNullOrEmpty(id))
153!
23
        {
24
            throw new InvalidOperationException($"Could not get app id from {type}");
×
25
        }
26

27
        return id;
153✔
28
    }
29

30
    private static bool GetAppFocus(Type type)
31
    {
32
        return type.GetCustomAttribute<FocusAttribute>() is not null;
147✔
33
    }
34

35
    public object? Create(IServiceProvider provider)
36
    {
37
        return _func.Invoke(provider);
103✔
38
    }
39

40
    public string Id { get; }
399✔
41

42
    public bool HasFocus { get; }
109✔
43

44
    // TODO: Remove
45
    public static FuncAppFactory Create<TAppType>(Func<IServiceProvider, TAppType> func,
46
        string? id = default, bool? focus = default) where TAppType : class
47
    {
UNCOV
48
        return new FuncAppFactory(func, typeof(TAppType), id, focus);
×
49
    }
50

51
    public static FuncAppFactory Create(Delegate @delegate, string? id = null, bool hasFocus = false)
52
    {
53
        return new FuncAppFactory(sp => ResolveDelegateParamsFromServiceProvider(@delegate, sp),
9✔
54
            @delegate.Method.ReturnType, // TODO: do not base id and focus on the return type for delegate apps
7✔
55
            id, hasFocus);
7✔
56
    }
57

58
    private static object? ResolveDelegateParamsFromServiceProvider(Delegate @delegate, IServiceProvider provider)
59
    {
60
        // Pass any arguments to the delegate method by resolving them from the service provider
61
        var args = @delegate.Method.GetParameters().Select(p => provider.GetService(p.ParameterType)).ToArray();
4✔
62
        return @delegate.DynamicInvoke(args);
2✔
63
    }
64

65
    public static FuncAppFactory Create(Type type, string? id = default, bool? focus = default)
66
    {
67
        return new FuncAppFactory(CreateFactoryFunc(type), type, id, focus);
147✔
68
    }
69

70
    private static Func<IServiceProvider, object> CreateFactoryFunc(Type type)
71
    {
72
        return provider => ActivatorUtilities.CreateInstance(provider, type);
248✔
73
    }
74
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc