Ninject Registration with Silverlight and Prism - silverlight

I am using Ninject as my bootstrapper (mainly because of convention based registration and its fluent API).
We are using Prism 4 Navigation Framework RequestNavigateAsync call to navigate from one page to the other. The API looks into the container for named instance of the object and resolve the view / viewmodel that it needs to navigate to. Here's Unity syntax for this.
Its recommened to use
container.RegisterType("InboxView"
instead of container.RegisterType("InboxView")
In Ninject, how can I get similar effect so that it gels with Navigation framework easily?
Can you help provide some code / documentation which shows how to register named instances in Ninject (that might help).

Assuming this is your syntax in Unity
var container = new UnityContainer();
container.RegisterType<object, InboxView>("InboxView");
The equivalent syntax in Ninject is
var kernel = new StandardKernel();
kernel.Bind<object>().To<InboxView>().Named("InboxView");

Related

WPF WCF Entity Framework Can not view Classes in WPF project

I am wprking on a project and I have 2 projects in one solution. I added my WCF project and inside it I added an entity framework. Also in Service1.svc I can reach the classes and use them for saving like
public bool saveuser(User user)
{
}
For example User is the name of my database and Its pretty fine working in WCF part but at WPF part Im trying to access this class again to fill this class's object and I using MyClass.ServiceReference1; on the top and Inside one of my method I can not reach this class. What should be the solution why I can not access my class instead I added this WCF service as a service reference and enabled async operations.
Im newbie and thanks
Sincerely

Bootstrapping Windsor Castle IoC in WPF application

I am used to using Windsor Castle IoC with MVC web applications so I'm a little familiar with registering components and bootstrapping the application.
I am now trying to create my first WPF application and I'm a little lost....
In a typical MVC project I would register all my components in Application_Start in the Global.asax file.
Also, in the Global.asax I would do something like this :
_container = new WindsorContainer();
var myControllerFactory = new MyControllerFactory(_container.Kernel);
ControllerBuilder.Current.SetControllerFactory(myControllerFactory);
In MyControllerFactory which inherits from DefaultControllerFactory I would resolve the Controller given to me, thus all the controllers being used would have their dependencies resolved automatically.
How could I do this in a WPF application ?
I created a class called ContainerBootstrapper that registers my components, and now I'm wondering how I use this in my MainWindow so every time I call a component, it's dependencies are resolved automatically...
Unlike MVC, WPF does not provide framework for decoupled UI development out of the box. There are several out there (such as Caliburn.Micro which implements the MVVM pattern) that sit on top of WPF and provide integration for a variety of IoC containers as well as other features you may find useful in WPF development.
Alternatively, you will need to manage the container directly. i.e. instantiate and initialise the container in App.xaml.cs or MainWindow.xaml.cs and ensure that new object construction via your container's Resolve() function. If it is a non-trivial app, you will probably want to integrate this into your app's navigation routines.

Sending abstract class with DomainService WCF service to Silverlight

I was using a simple WCF service with silverlight but I wanted to validate data with annotations and I didn't want to write a whole new layer in silverlight project. So I decided to switch to using a DomainService, created through generating code in the silverlight project.
Now comes the trouble. I have a parent class and about 10 derived classes. Through WCF, I was able to just use the base class. Now I am trying to use a DomainService with the base class decorated with the KnownType attribute. The problem is now those attributes get replicated in silverlight client and a compilation error is thrown. Anybody know how to use DomainService with inheritance? I want to deliver only the information from the base class.
I don't completely follow what your problem is, but this is a great tutorial on how to use Domain Services in Silverlight, and the example includes an abstract base class for all entities, similar to what I think you're doing.

WPF Prism Inject same viewmodel instance into two views

So I have two separate views in a WPF Prism app. How can I inject the same instance of a ViewModel into both the views via Dependency Injection?
(I assume you are using Unity as you mentioned DI directly and are not saying "Import" or similar)
Hi,
You will have to register the ViewModel as a singleton (using a ContainerControlledLifetimeManager) in the container and have it injected as usual. This ensures that you will get the same instance whenever you request for it.
A valid approach in this scenario might be to use a named registration in case you want to get the same VM injected elsewhere, but you don't need the same instance.
This article explains how to do this.

Does Caliburn provide a way to get a reference to the container from anywhere?

I'm currently messing around with Rob Eisenberg's Caliburn framework. When looking at the documentation that is provided on http://caliburn.codeplex.com there is an example of how to resolve a Caliburn service from the container.
It's something along the lines of this:
SimpleContainer container = new SimpleContainer();
CaliburnFramework
.ConfigureCore(container)
.WithCommonDialogs()
.WithPresentationFramework()
.Start();
var service = container.GetInstance(typeof (IService)) as Service;
However what I am missing is a way to get a reference to the container anywhere in the app. Like this:
var service = Caliburn.Container.GetInstance(typeof(IService))as Service;
Do I have to build a custom static class that holds a reference to the container or is there something already built into Caliburn?
Thanks in advance and best regards!
The latest trunk version of Caliburn automatically registers the container on framework startup as an service locator. You just have to reference Microsoft.Practices.ServiceLocation on your code and then ask the ServiceLocator for a instance of your service.
var service = ServiceLocator.Current.GetInstance<IService>();
Hope that helps.

Resources