IoC-MEF injection problem - wpf

I use Caliburn.Micto as MVVM framework for my WPF application and also MEF for injection.
UML of my application look like this: http://i54.tinypic.com/2n1b4mx.png
My scenario is: I create in view-model-1 (in project is LogOnViewModel) new view-model-2 (in my project is MessengerViewModel) with shell-view-model method.
I need pass object from view-model-1 to constructor of view-model-2.
I use MEF on injection class from external assembly which is loaded in boostraper class.
On creation of new view-models I use abstract factory pattern, here is my implementation:
/// <summary>
/// Factory interfaces
/// </summary>
public interface IViewModelFactory
{
ILogOnViewModel CreateLogOnViewModel(IShellViewModel shellViewModel);
IMessengerViewModel CreateMessengerViewModel(IShellViewModel shellViewModel, PokecAccount account);
}
/// <summary>
/// Concrent implementation of factory
/// </summary>
[Export(typeof(IViewModelFactory))]
public class DefaulFactoryViewModel:IViewModelFactory
{
#region Implementation of IViewModelFactory
//create start up view-model
public ILogOnViewModel CreateLogOnViewModel(IShellViewModel shellViewModel)
{
return new LogOnViewModel(shellViewModel);
}
//this method create new view model
//it is used in LogOnViewModel
public IMessengerViewModel CreateMessengerViewModel(IShellViewModel shellViewModel, PokecAccount account)
{
return new MessengerViewModel(shellViewModel, account);
}
}
I use this factory class in my shell-view-model. Shell-view-model class look like this:
/// <summary>
/// Shell model interface
/// </summary>
public interface IShellViewModel
{
//create start up view-model
void ShowLogOnView();
//this method create new view model
//it is used in LogOnViewModel
void ShowMessengerView(PokecAccount account);
}
[Export(typeof(IShellViewModel))]
public class ShellViewModel : Conductor<IScreen>, IShellViewModel
{
//factory interface
private readonly IViewModelFactory _factory;
[ImportingConstructor]
public ShellViewModel(IViewModelFactory factory)
{
//inject factory
_factory = factory;
//show startup view model
ShowLogOnView();
}
public void ShowLogOnView()
{
//create LogOnViewModel class with factory
var model = _factory.CreateLogOnViewModel(this);
ActivateItem(model);
}
/// <summary>
/// Create MessengerViewModel
/// </summary>
/// <param name="account">account in this case is send from LogOnViewModel class </param>
public void ShowMessengerView(PokecAccount account)
{
//create MessengerViewModel class with factory
var model = _factory.CreateMessengerViewModel(this, account);
ActivateItem(model);
}
}
}
Start up view-model. LogOnViewModel class:
public interface ILogOnViewModel : IScreen, IDataErrorInfo
{
string Nick { get; set; }
string Password { get; set; }
bool CanLogOn { get; set; }
void LogOn(string nick, string password);
}
public class LogOnViewModel : Screen, ILogOnViewModel
{
/// <summary>
/// inject class from external assembly
/// after creation of this class is still null
/// </summary>
[Import]
public IPokecConnection PokecConn { get; set; }
private readonly IShellViewModel _shellViewModel = null;
private PokecAccount _account = null;
public LogOnViewModel(IShellViewModel shellViewModel)
{
_shellViewModel = shellViewModel;
_account = new PokecAccount();
}
//CREATE NEW VIEW MODEL
public void CreateNewView()
{
//create new view-model (MessengerViewModel)
_shellViewModel.ShowMessengerView(_account);
}
}
MessengerViewModel class:
public interface IMessengerViewModel : IScreen
{
BitmapImage AvatarImage { get; set; }
string AvatarStatus { get; set; }
KeyValuePair<string, Friend> SelectedFriend { get; set; }
}
public class MessengerViewModel : Screen, IMessengerViewModel
{
[Import]
private IPokecService _pokecService;
[Import]
private IPokecConnection _pokecConn;
private IShellViewModel _shellViewModel = null;
private PokecAccount _account = null;
public MessengerViewModel(IShellViewModel shellViewModel, PokecAccount account)
{
_shellViewModel = shellViewModel;
_account = account;
}
}
I have problem with injection into view-model class. On creation of view-model classes I use factory pattern, but I need inject in this class also from external assembly.
For example: After creation of LogOnVieModel class is IPokecConnection PokecConn{ get; set;} still null.
What is the most suitable solution in my case? Where is it problem ? Thank for help.

The factory pattern you are using doesn't do any composition outside of composing the ViewScreenModel class itself. You need to tell MEF to compose your view models, if they are not being created through injection. Update your factory class to compose the instance before returning it;
public ILogOnViewModel CreateLogOnViewModel
{
var model = new LogOnViewModel();
var container = // set this to your reference of CompositionContainer
container.ComposeParts(model);
return model;
}
...where Bootstapper.Container is your instance of CompositionContainer.
On another note, why have you made another account, instead of using your current one

Related

WPF PRISM 6 DelegateComand ObservesCanExecute

Thanks in advance!
How should I use ObservesCanExecute in the DelegateCommand of PRISM 6?
public partial class UserAccountsViewModel: INotifyPropertyChanged
{
public DelegateCommand InsertCommand { get; private set; }
public DelegateCommand UpdateCommand { get; private set; }
public DelegateCommand DeleteCommand { get; private set; }
public UserAccount SelectedUserAccount
{
get;
set
{
//notify property changed stuff
}
}
public UserAccountsViewModel()
{
InitCommands();
}
private void InitCommands()
{
InsertCommand = new DelegateCommand(Insert, CanInsert);
UpdateCommand = new DelegateCommand(Update,CanUpdate).ObservesCanExecute(); // ???
DeleteCommand = new DelegateCommand(Delete,CanDelete);
}
//----------------------------------------------------------
private void Update()
{
//...
}
private bool CanUpdate()
{
return SelectedUserAccount != null;
}
//.....
}
Unfortunatelly, I'm not familiar with expressions in c#. Also, I thought this would be helpful to others.
ObservesCanExecute() works “mostly like” the canExecuteMethod parameter of DelegateCommand(Action executeMethod, Func<bool> canExecuteMethod).
However, if you have a boolean property instead of a method, you don't need to define a canExecuteMethod with ObservesCanExecute.
In your example, suppose that CanUpdate is not a method, just suppose that it's a boolean property.
Then you can change the code to ObservesCanExecute(() => CanUpdate) and the DelegateCommand will execute only if the CanUpdate boolean property evaluates to true (no need to define a method).
ObservesCanExecute is like a “shortcut” over a property instead of having to define a method and having passing it to the canExecuteMethod parameter of the DelegateCommand constructor.

Unit Testing ICommand with NSubstitute

I have the following ViewModel
public class MyViewModel : IMyViewModel
{
private readonly IMyModel myMode;
private ICommand _myCommand;
public MyViewModel(IMyModel model)
{
_model = model;
}
public ICommand MyCommand
{
get { return _myCommand ?? (_myCommand = new RelayCommand(x => MyMethod())); }
}
private void MyMethod()
{
_model.SomeModelMethod();
}
}
where IMyViewModel is defind as
public interface IMyViewModel
{
ICommand MyCommand { get; }
}
and my interface for the model is defined as
public interface IMyModel
{
void SomeOtherCommand();
}
Now in my unit test (using NSubstitute) I want to check that when MyCommand is invoked my model receives a call to its method SomeModelMethod. I've tried:
[TestMethod]
public void MyViewModel_OnMyCommand_CallsSomeOtherMethodOnModel()
{
var model = Substitute.For<IMyModel>();
var viewModel = Substitute.For<IMyViewModel>();
viewModel.MyCommand.Execute(null);
model.Received().SomeOtherMethod();
}
but this doesn't currently work. How do I best test that my Model method is called when a command on my ViewModel is invoked?
Not sure why you're mocking IMyViewModel here. You said you wanted to test whether SomeOtherMethod is invoked when you execute the command in MyViewModel.
You shouldn't be mocking the MyViewModel here.
[TestMethod]
public void MyViewModel_OnMyCommand_CallsSomeOtherMethodOnModel()
{
var model = Substitute.For<IMyModel>();
var viewModel = new MyViewModel(model);
viewModel.MyCommand.Execute(null);
model.Received().SomeOtherMethod();
}
P.S: I'm not familiar with nsubstitute. But the idea is still same (you shouldn't mock MyViewModel). Make sure you're using the right methods in nsubstitute.

I have a class object in one view model. I would like to pass this object state to another View Model in MVVM

In AttachmentViewModel I have the following code
public ICommand EditAttachmentCommand { get; private set; }
public AttachmentsViewModel()
{
EditAttachmentCommand = new ActionCommand<AvailAttachment>(EditAttachment);
}
private void EditAttachment(AvailAttachment attachment)
{
var attachmentDetailsViewModel = Router.ResolveViewModel<AttachmentDetailsViewModel>(true, ViewModelTags.ATTACHMENT_DETAILS_VIEWMODEL);
attachmentDetailsViewModel.NavigateToAttachment(attachment.ArticleId);
EventAggregator.Publish(ViewTags.ATTACHMENT_DETAILS_VIEW.AsViewNavigationArgs());
EventAggregator.Publish(new CurrentViewMessage(ContentView.Attachment));
}
In my MainViewModel I have the following code:
public ICommand SessionAttachmentCommand { get; private set; }
public MainMenuViewModel()
{
SessionAttachmentCommand = new ActionCommand<AvailAttachment>(EditAttachment);
}
private void EditAttachment(AvailAttachment attachment)
{
var attachmentDetailsViewModel = Router.ResolveViewModel<AttachmentDetailsViewModel>(true, ViewModelTags.ATTACHMENT_DETAILS_VIEWMODEL);
attachmentDetailsViewModel.NavigateToAttachment(attachment.ArticleId);
EventAggregator.Publish(ViewTags.ATTACHMENT_DETAILS_VIEW.AsViewNavigationArgs());
EventAggregator.Publish(new CurrentViewMessage(ContentView.Attachment));
}
I would like to pass the object state of AvailAttachment class from AttachmentsViewModel to MainMenuViewModel. presently null value comes for AvailAttachment's attachment object in MainMenuViewModel. I am debugging a code written by someone. This is a silverlight project using MVVM model. How do I do that?
Any help is appreciated. Thanks

MEF problem with import

models from shell-view-model with abstract factory pattern. I need inject in view-models classes from external assembly. If I use abstract factory pattern on creation view-models. Problem is imported classes in view-models are null.
Shell-view-models look like this:
public interface IViewModelFactory
{
ILogOnViewModel CreateLogOnViewModel(IShellViewModel shellViewModel);
IMessengerViewModel CreateMessengerViewModel(IShellViewModel shellViewModel);
}
[Export(typeof(IViewModelFactory))]
public class DefaulFactoryViewModel:IViewModelFactory
{
#region Implementation of IViewModelFactory
public ILogOnViewModel CreateLogOnViewModel(IShellViewModel shellViewModel)
{
return new LogOnViewModel(shellViewModel);
}
public IMessengerViewModel CreateMessengerViewModel(IShellViewModel shellViewModel)
{
return new MessengerViewModel(shellViewModel);
}
#endregion
}
public interface IShellViewModel
{
void ShowLogOnView();
void ShowMessengerView();
}
[Export(typeof(IShellViewModel))]
public class ShellViewModel : Conductor<IScreen>, IShellViewModel
{
private readonly IViewModelFactory _factory;
[ImportingConstructor]
public ShellViewModel(IViewModelFactory factory)
{
_factory = factory;
ShowLogOnView();
}
public void ShowLogOnView()
{
var model = _factory.CreateLogOnViewModel(this);
// var model = IoC.Get<LogOnViewModel>();
ActivateItem(model);
}
public void ShowMessengerView()
{
var model = _factory.CreateMessengerViewModel(this);
ActivateItem(model);
}
}
Some view-model.:
public class LogOnViewModel : Screen,ILogOnViewModel
{
[Import]//inject class from external assembly
private IPokecConnection _pokecConn;
private readonly IShellViewModel _shellViewModel=null;
private User _user=null;
public LogOnViewModel(IShellViewModel shellViewModel)
{
_shellViewModel = shellViewModel;
_user = new User();
}
}
variable _pokecConn are null becasuse I use abstract factory on creation new view-models.
if I use in shell-view model this:
var model = IoC.Get<LogOnViewModel>();
instead this:
var model = _factory.CreateLogOnViewModel(this);
and add Export attribute on view-models classes it works good, but I would like use abstract factory, and inject in view-model only classes from extrenal assembly.
It exist solution on this problem, or I must create view-models from IoC and export all class? Thanl for advance.
EDITED :
MEF BOOTSTRAPER CLASS:
public class MefBootStrapper : Bootstrapper<IShellViewModel>
{
#region Fields
private CompositionContainer _container;
#endregion
#region Overrides
protected override void Configure()
{ // configure container
#if SILVERLIGHT
_container = CompositionHost.Initialize(
new AggregateCatalog(AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>()));
#else
var catalog =
new AggregateCatalog(
AssemblySource.Instance.Select(x => new AssemblyCatalog(x)).OfType<ComposablePartCatalog>());
//add external DLL
catalog.Catalogs.Add(
new AssemblyCatalog(string.Format(
CultureInfo.InvariantCulture, "{0}{1}", System.IO.Directory.GetCurrentDirectory(), #"\Pokec_Toolkit.dll")));
_container = new CompositionContainer(catalog);
#endif
var batch = new CompositionBatch();
batch.AddExportedValue<IWindowManager>(new WindowManager());
batch.AddExportedValue<IEventAggregator>(new EventAggregator());
batch.AddExportedValue(_container);
_container.Compose(batch);
_container.SatisfyImportsOnce(this);
}
protected override object GetInstance(Type serviceType, string key)
{
string contract = string.IsNullOrEmpty(key) ? AttributedModelServices.GetContractName(serviceType) : key;
var exports = _container.GetExportedValues<object>(contract);
if (exports.Count() > 0)
return exports.First();
throw new Exception(string.Format("Could not locate any instances of contract {0}.", contract));
}
protected override IEnumerable<object> GetAllInstances(Type serviceType)
{
return _container.GetExportedValues<object>(AttributedModelServices.GetContractName(serviceType));
}
protected override void BuildUp(object instance)
{
_container.SatisfyImportsOnce(instance);
}
#endregion
}
Did you forget the attribute ImportingConstructor for the LogOnViewModel constructor?
EDIT: Import property always null (MEF import issue)

MVVM : how to pass parameter to ViewModel's constructor

I'm using L. Bugnion's MVVM Light Framework.
What are some of the recommended approaches to pass parameters such as Customer's ID to ViewModel's constructor?
Edit:
The parameter I need for each ViewModel is not something that is shared across models. it is something unique to each viewmodel instance.
//Create a container class to pass via messenger service
public class CarSelectedArgs
{
#region Declarations
public Car Car { get; set; }
#endregion
#region Constructor
public CarSelectedArgs(Car car)
{
Car = car;
}
#endregion
}
//example of view model sending message.
public class SendingViewModel : ViewModelBase
{
private Car _car;
public Car SelectedCar
{
get { return _car; }
set
{
_car = value;
if (value != null)
{
//messenger will notify all classes that have registered for a message of this type
Messenger.Default.Send(new CarSelectedArgs(value));
}
}
}
}
//Example of ViewModel registering to recieve a message
public class SampleViewModel : ViewModelBase
{
#region Constructor
public SampleViewModel()
{
Messenger.Default.Register<CarSelectedArgs>(this, OnCarSelected);
}
#endregion
#region LocalMethods
void OnCarSelected(CarSelectedArgs e)
{
var NewCar = e.Car;
}
#endregion
}
For me the whole point of using MVVM Light is to avoid injecting anything into the constructor of a View Model. MVVM Light provides a Messaging facility that allows you to send your parameters to a listener registered inside of the View Model.
For example, this is my View Model from my WordWalkingStick project using VSTO and WPF:
using System;
using System.Xml.Linq;
using GalaSoft.MvvmLight.Messaging;
namespace Songhay.Wpf.WordWalkingStick.ViewModels
{
using Songhay.Office2010.Word;
using Songhay.OpenXml;
using Songhay.OpenXml.Models;
using Songhay.Wpf.Mvvm;
using Songhay.Wpf.Mvvm.ViewModels;
/// <summary>
/// View Model for the default Client
/// </summary>
public class ClientViewModel : ViewModelBase
{
/// <summary>
/// Initializes a new instance of the <see cref="ClientViewModel"/> class.
/// </summary>
public ClientViewModel()
{
if(base.IsInDesignMode)
{
#region
this._flatOpcSourceString = ApplicationUtility
.LoadResource(
new Uri("/Songhay.Wpf.WordWalkingStick;component/PackedFiles/FlatOpcToHtml.xml",
UriKind.Relative));
this._xhtmlSourceString = ApplicationUtility
.LoadResource(
new Uri("/Songhay.Wpf.WordWalkingStick;component/PackedFiles/FlatOpcToHtml.html",
UriKind.Relative));
#endregion
}
else
{
this._flatOpcSourceString = "Loading…";
this._xhtmlSourceString = "Loading…";
//Receive MvvmLight message:
Messenger.Default.Register(this,
new Action<GenericMessage<TransformationMessage>>(
message =>
{
var tempDocFolder =
Environment.ExpandEnvironmentVariables("%UserProfile%/Desktop/");
var inputPath = tempDocFolder + "temp.docx";
var outputPath = tempDocFolder + "temp.html";
var flatOpcDoc =
XDocument.Parse(message.Content.TransformationResult);
OpenXmlUtility.TransformFlatToOpc(flatOpcDoc, inputPath);
this.FlatOpcSourceString = flatOpcDoc.Root.ToString();
var settings = new SonghayHtmlConverterSettings()
{
PageTitle = "My Page Title " + DateTime.Now.ToString("U"),
UseEntityMap = false
};
OpenXmlUtility.WriteHtmlFile(inputPath, outputPath, settings);
var xhtmlDoc = XDocument.Load(outputPath);
this.XhtmlSourceString = xhtmlDoc.Root.ToString();
}));
}
}
/// <summary>
/// Gets or sets the flat opc source string.
/// </summary>
/// <value>The flat opc source string.</value>
public string FlatOpcSourceString
{
get
{
return _flatOpcSourceString;
}
set
{
_flatOpcSourceString = value;
base.RaisePropertyChanged("FlatOpcSourceString");
}
}
/// <summary>
/// Gets or sets the XHTML source string.
/// </summary>
/// <value>The XHTML source string.</value>
public string XhtmlSourceString
{
get
{
return _xhtmlSourceString;
}
set
{
_xhtmlSourceString = value;
base.RaisePropertyChanged("XhtmlSourceString");
}
}
string _flatOpcSourceString;
string _xhtmlSourceString;
}
}
You can see that MVVM Light is messaging (not injecting) values into the constructor (Messenger.Default.Register) with its Messenger.
Request anything you want, via injection, using interfaces.
If you have settings shared across models, instantiate a singleton containing the values and expose them via ISomethingProvider and ISomethingEditor interfaces.
Here is what I do:
ViewModel needs to show a car window with car id passed as parameter:
ViewModel -> message to codebehind for view to open window. Message sends id.
Essentially in code behind:
var vm = new viewmodel(id);
var view = new view();
view.datacontext = vm;
view.show();
my viewmodel has a constructor that takes in an id.
In the case of writing tests against the viewmodel I sometimes create an overload of the viewmodel constructor that takes an ISomething as a parameter. I have the default constructor call the second one with a default implementation of ISomething. In case of the test I call the constructor with a test implementation. I know it's not the best method, because it creates a dependency between the two classes... but sometimes you'll have to take the easy path...
public class SomeViewModel
{
private ISomething internalSomething;
public void SomeViewModel():this(new Something()){}
public void SomeViewModel(ISomething something)
{
this.internalSomething = something;
}
}
Update
Creating a view in xaml can be like this:
<UserControl xmlns="...."
xmlns:Example="SomeNamespace">
<UserControl.DataContext>
<Example:SomeViewModel />
</UserControl.DataContext>
<Grid>
...
</Grid>
</UserControl>

Resources