This class manages instantiations and updates of view models as requested by browser clients.
Inheritance: IDisposable
示例#1
0
        /// <summary>
        /// Adds a dynamic dotNetify view model.
        /// </summary>
        /// <param name="app">Application builder.</param>
        /// <param name="vmName">View model name.</param>
        /// <param name="propertyBuilder">Delegate that returns an object for building the view model's properties.</param>
        private static IApplicationBuilder MapVM <T>(this IApplicationBuilder app, string vmName, Delegate propertyBuilder) where T : BaseVM, new()
        {
            vmName          = !string.IsNullOrWhiteSpace(vmName) ? vmName : throw new ArgumentNullException(nameof(vmName));
            propertyBuilder = propertyBuilder ?? throw new ArgumentNullException(nameof(propertyBuilder));

            // Make sure "UseDotNetify()" is called first.
            if (!_useDotNetify)
            {
                app.UseWebSockets();
                app.UseDotNetify(config => config.UseFilter <AuthorizeFilter>());
            }

            VMController.Register(vmName, _ =>
            {
                using (var scope = app.ApplicationServices.CreateScope())
                {
                    var args = new List <object>();
                    foreach (var parameter in propertyBuilder.Method.GetParameters())
                    {
                        var instance = scope.ServiceProvider.GetRequiredService(parameter.ParameterType);
                        args.Add(instance);
                    }

                    var propertySource = propertyBuilder.DynamicInvoke(args.ToArray());
                    if (propertySource is Task)
                    {
                        var resultProperty = propertySource.GetType().GetProperty("Result");
                        propertySource     = resultProperty.GetValue(propertySource);
                    }

                    return(VMBuilder.Build <T>(propertySource, propertyBuilder.Method.GetCustomAttributes()));
                }
            });

            return(app);
        }
示例#2
0
 public void RegisterEntryAssembly() => VMController.RegisterAssembly(Assembly.GetEntryAssembly());
示例#3
0
 public void RegisterAssembly(string assemblyName) => VMController.RegisterAssembly(Assembly.Load(new AssemblyName(assemblyName)));
示例#4
0
 public void RegisterAssembly(Assembly assembly) => VMController.RegisterAssembly(assembly);
 /// <summary>
 /// Register a specific view model class type.
 /// </summary>
 public IDotNetifyConfiguration Register <T>() where T : INotifyPropertyChanged
 {
     VMController.Register <T>();
     return(this);
 }
 /// <summary>
 /// Register view model classes in an assembly that are subtypes of a certain type.
 /// </summary>
 public void RegisterAssembly <T>(string assemblyName) where T : INotifyPropertyChanged => VMController.RegisterAssembly <T>(Assembly.Load(new AssemblyName(assemblyName)));
 /// <summary>
 /// Register view model classes in an assembly that are subtypes of a certain type.
 /// </summary>
 public void RegisterAssembly <T>(Assembly assembly) where T : INotifyPropertyChanged => VMController.RegisterAssembly <T>(assembly);
 /// <summary>
 /// Register a specific runtime view model class type.
 /// </summary>
 public IDotNetifyConfiguration Register(string typeName, Func <object[], INotifyPropertyChanged> factory)
 {
     VMController.Register(typeName, factory);
     return(this);
 }
示例#9
0
        public void VMController_OnRequestVM()
        {
            var vmController = new VMController(TestResponse);
             vmController.OnRequestVM("conn1", typeof(UnitTestVM).Name);

             Assert.AreEqual("conn1", _connectionId);
             Assert.AreEqual(typeof(UnitTestVM).Name, _vmId);
             var vmData = JsonConvert.DeserializeObject<UnitTestVM>(_vmData);
             Assert.IsNotNull(vmData);
             Assert.AreEqual("John", vmData.FirstName);
             Assert.AreEqual("Smith", vmData.LastName);
             Assert.AreEqual("John Smith", vmData.FullName);
             Assert.AreEqual(25, vmData.Age);

             vmController.Dispose();
        }
示例#10
0
        public void VMController_OnUpdateVM()
        {
            var vmController = new VMController(TestResponse);
             vmController.OnRequestVM("conn1", typeof(UnitTestVM).Name);

             vmController.OnUpdateVM("conn1", typeof(UnitTestVM).Name, new Dictionary<string, object>() { { "LastName", "Doe" }, { "Age", 42 } });

             Assert.AreEqual("conn1", _connectionId);
             Assert.AreEqual(typeof(UnitTestVM).Name, _vmId);
             var vmData = (JObject)JsonConvert.DeserializeObject(_vmData);
             Assert.AreEqual("John Doe", vmData["FullName"]);

             vmController.OnRequestVM("conn1", typeof(UnitTestVM).Name);
             Assert.AreEqual("conn1", _connectionId);
             Assert.AreEqual(typeof(UnitTestVM).Name, _vmId);
             vmData = (JObject)JsonConvert.DeserializeObject(_vmData);
             Assert.AreEqual("Doe", vmData["LastName"]);
             Assert.AreEqual(42, vmData["Age"]);

             vmController.Dispose();
        }