Add support for self scope

This commit is contained in:
TheXamlGuy
2024-05-12 12:22:04 +01:00
parent 1dc4da48bb
commit a44391f4d5
4 changed files with 33 additions and 17 deletions
@@ -1,4 +1,5 @@
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
@@ -140,11 +141,18 @@ public partial class ObservableCollectionViewModel<TViewModel> :
return item;
}
public TViewModel Add<T>()
public TViewModel Add<T>(bool scope = false)
where T :
TViewModel
{
T? item = Factory.Create<T>();
IServiceFactory? factory = null;
if (scope)
{
IServiceScope serviceScope = Provider.CreateScope();
factory = serviceScope.ServiceProvider.GetRequiredService<IServiceFactory>();
}
T? item = factory is not null ? factory.Create<T>() : Factory.Create<T>();
Add(item);
return item;