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

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.

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

Ninject Registration with Silverlight and Prism

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");

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.

Ria Service Generated Code Not Accessible in Silverlight Code

I have the following Ria Service define:
namespace SilverlightTest.Web
{
[EnableClientAccess()]
public class ContactService : LinqToEntitiesDomainService<AdventureWorksEntities>
{
public IQueryable<Contact> GetContactSearch(string lastName)
{
ContactRepository rep = new ContactRepository();
return rep.SearchByLastName(lastName);
}
}
}
When I compile the solution, my SilverlightTest project does create the SilverlightTest.Web.g.cs file and the appropriate Context objects are created when I look at it. However, when I attempt to import the SilverlightTest.Web namespace to access the Data Context class for the above service, it says it cannot find the Web namespace.
The only difference I can see between what I'm doing and many examples that are out there on the web is that my AdventureWorksEntities data context is located in a separate business object dll. I tried to query the context directly instead of using the Repository Pattern I'm attempting to do and it also isn't work working.
Any ideas? Is it possible to have Ria Services access a separate DLL handling data access or does it HAVE to be in the same project?
I've been able to put the Ria service in a separate project before, although I do remember having issues. Not sure exactly what it was, but I would check two things: your references and your web.config (in the hosting website). When you add a ria service to a web project it does some things behind the scenes that wire everything up correctly.
Could try adding a service to your web project temporarily and see what it adds.
It seems that Resharper does not recognize the .gs files and their name spaces. If you disable R# or just code without intelisense it works.

Unity to return new instance of service

I have come across a bit of a problem while using Unity and WPF. The scenario is I have a WPF application which follows the MVVM design pattern. A have a module called ViewKDI. Within this module I have a service called ViewKDIService, the ViewKDIService service utilises another service called UserService.
Every time I load the module ViewKDI I want Unity to return me a new instance of both the ViewKDIService and the UserService.
I have put the below in the shell bootstrapper:
Container.RegisterType<IUserService, UserService>();
In the ViewKDI module I have put the following:
Container.RegisterType<IViewKDIService, ViewKDIService>();
Each time the ViewKDI module loads the ViewKDIService constructor is called. However the UserService constructor is only called the first time, this means that I am not getting a new instance of UserService.
I require unity to give me a new instance of UserService too so that I can manage this session separately from the rest of the application.
Any ideas?
Thanks
Faisal
Unity's default behaviour is to create a new instance of each object each time one is requested, so you shouldn't be seeing this behaviour.
From what I can gather from the source code and MSDN documentation (this is a good read), you can specify a "lifetime manager" object when you register a type to tell Unity how the type should be constructed and cached. Using the TransientLifetimeManager (which essentially does no caching) will cause Unity to re-create the class each time. So try this:
Container.RegisterType<IUserService, UserService>(new TransientLifetimeManager());
... and see if it creates a new UserService each time.

Resources