ViewModel<T> constructor with no parameter and public ApplicationContext? #3211
Answered
by
rockfordlhotka
michaelcsikos
asked this question in
Ideas
-
I'm hoping to write some more generic classes to reduce the disappointing amount of repetition I have, particularly with public abstract class ViewModelListBase<T, C, VM>
: ViewModel<T>
where T : BusinessListBase<T, C>
where C : IEditableBusinessObject
where VM : ViewModel<C>, new()
{
public ViewModelListBase(ApplicationContext applicationContext)
: base(applicationContext)
{
}
List<VM> _list = null;
public List<VM> List
{
get
{
if (_list == null)
{
if (Model != null)
{
_list = new List<VM>();
foreach (var item in Model)
{
// This can't be done with generics:
// var vm = new VM(ApplicationContext);
// No parameterless constructor...
var vm = new VM();
// and this is not accessible currently:
vm.ApplicationContext = ApplicationContext;
vm.Model = item;
_list.Add(vm);
}
}
}
return _list;
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
rockfordlhotka
Nov 28, 2022
Replies: 1 comment 1 reply
-
You can probably use |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
michaelcsikos
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can probably use
ApplicationContext.CreateInstanceDI
instead of thenew
keyword.