Reputation: 449
I'm developing 2 applications. One of them using Prism.Autofac in WPF and the other one is developed with Prism.Unity in Xamarin Forms.
Both apps are working, but I have one PCL where I'm using compiler sentences to use Prism.Autofac or Prism.Unity.Forms according to the case.
Well I'm trying to implement Autofac in both apps, but I can't how to register types on Xamarin.Forms.
For example, with Unity I'm doing this:
Container.RegisterInstance(Xamarin.Forms.DependencyService.Get<SomeService>());
But with Autofac the "RegisterInstance" method it doesn't exists. I'm trying many ways to achieve this and I found that ContainerBuilder class has that method, but if I make this:
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterInstance(Xamarin.Forms.DependencyService.Get<SomeService>());
Using a ContainerBuilder instance, PRISM Container doesn't recognize my custom classes/Services like a registered instance, so the ViewModel's constructor can't be created.
So, My question is: How can I use RegisterInstance and RegisterType methods only with PRISM Container (without ContainerBuilder instance)? and if it's not possible and I have to create an instance of ContainerBuilder, how PRISM Container could "register" my custom types?
Thanks in advance
Upvotes: 2
Views: 2449
Reputation: 5799
Autofac for Prism Forms has been hugely problematic. In order to register types in v6.X with Prism.Autofac.Forms you would have code something like:
public class App : PrismApplication
{
protected override void RegisterTypes()
{
var builder = new ContainerBuilder();
Container.RegisterTypeForNavigation<MainPage>()
builder.RegisterType<MyService>().As<IMyService>();
builder.Update( Container );
}
}
Beginning with v7.0 this has been updated. Note that as one of the comments below indicates Autofac has deprecated the use of builder.Update
as the Autofac community wants to make the container immutable. This is makes it a generally poor choice for Prism applications that want to make use of Prism's Modularization. With Prism 7, the implementation of Autofac's base Application class has been updated to take this into account, and provide you a single ContainerBuilder that is used for Prism's base registrations as well as any of your specific types both in shared code and your IPlatformInitializer
. Keep in mind that the Container is not actually built and available until you have reached OnInitialized
. The updated registrations would look like:
public class App : PrismApplication
{
protected override void RegisterTypes()
{
Builder.RegisterTypeForNavigation<MainPage>()
Builder.RegisterType<MyService>().As<IMyService>();
}
}
You can read up on the v7 changes here:
https://dansiegel.net/post/2017/08/02/breaking-changes-for-prism-autofac-users
Upvotes: 2
Reputation: 953
Update is now obsolete, the best way for me is to create a module, look:
First create a Module in your project:
public class ExampleModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<LoginP>().As<ILoginP>().InstancePerDependency();
base.Load(builder);
}
}
In your presentation Layer, include the Module example (xamarin.Android mainActivity.cs):
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainer container)
{
var builder = new ContainerBuilder();
builder.RegisterModule<ExampleModule>();
builder.Update(container);
}
}
Upvotes: 0