Skip to content

Instantly share code, notes, and snippets.

@roubachof
Last active October 16, 2015 11:55
Show Gist options
  • Save roubachof/2ce930aac4ddcfcf13df to your computer and use it in GitHub Desktop.
Save roubachof/2ce930aac4ddcfcf13df to your computer and use it in GitHub Desktop.
My IocProvider issues with custom implementation
using Cirrious.CrossCore;
using Cirrious.CrossCore.IoC;
using Cirrious.MvvmCross.ViewModels;
using MvvmCross.Plugins.Messenger;
using %COMPANYNAME%.Core.Services;
using %COMPANYNAME%.Core.Services.Impl;
namespace %COMPANYNAME%.Core
{
/// <summary>
/// Entry point of core assembly.
/// </summary>
public class App : Cirrious.MvvmCross.ViewModels.MvxApplication
{
/// <summary>
/// Initializes this instance.
/// </summary>
public override void Initialize()
{
RegisterTypes();
var applicationCoordinator = Mvx.Resolve<IApplicationCoordinatorService>();
RegisterAppStart(new AppStart(applicationCoordinator));
}
/// <summary>
/// Registers the types.
/// </summary>
private void RegisterTypes()
{
Mvx.RegisterSingleton<IMvxMessenger>(new MvxMessengerHub());
// Registers all services by naming convention
CreatableTypes()
.EndingWith("Service")
.AsInterfaces()
.RegisterAsLazySingleton();
}
}
}
using System;
using Cirrious.CrossCore.Core;
using Cirrious.CrossCore.IoC;
using Ninject;
using Ninject.Modules;
using %COMPANYNAME%.Dal;
using %COMPANYNAME%.Exceptions;
namespace %COMPANYNAME%.Core.MvvmCross
{
public class NinjectMvxIocProvider : MvxSingleton<IMvxIoCProvider>, IMvxIoCProvider
{
private static bool isSingletonBuilt;
private readonly IKernel kernel;
public NinjectMvxIocProvider()
{
PlatformException.ThrowIf(isSingletonBuilt, "Only one instance of NinjectMvxIocProvider can be built");
if (!ThreadContext.IsCurrentDependencyContainerExist())
{
ThreadContext.InstanciateDependencyContainer();
}
kernel = ThreadContext.DependencyContainer.Resolve<IKernel>();
isSingletonBuilt = true;
}
public NinjectMvxIocProvider(INinjectSettings settings, params INinjectModule[] modules)
{
PlatformException.ThrowIf(isSingletonBuilt, "Only one instance of NinjectMvxIocProvider can be built");
kernel = new StandardKernel(settings, modules);
isSingletonBuilt = true;
}
public void CallbackWhenRegistered(Type type, Action action)
{
}
public void CallbackWhenRegistered<T>(Action action)
{
}
public bool CanResolve(Type type)
{
return (bool)kernel.CanResolve(type);
}
public bool CanResolve<T>() where T : class
{
return kernel.CanResolve<T>();
}
public object Create(Type type)
{
return kernel.Get(type);
}
public T Create<T>() where T : class
{
return kernel.Get<T>();
}
public object GetSingleton(Type type)
{
return kernel.Get(type);
}
public T GetSingleton<T>() where T : class
{
return kernel.Get<T>();
}
public object IoCConstruct(Type type)
{
return kernel.Get(type);
}
public T IoCConstruct<T>() where T : class
{
return (T)IoCConstruct(typeof(T));
}
public void RegisterSingleton(Type tInterface, Func<object> theConstructor)
{
kernel.Bind(tInterface).ToMethod(context => theConstructor()).InSingletonScope();
}
public void RegisterSingleton(Type tInterface, object theObject)
{
kernel.Bind(tInterface).ToConstant(theObject).InSingletonScope();
}
public void RegisterSingleton<TInterface>(Func<TInterface> theConstructor) where TInterface : class
{
kernel.Bind<TInterface>().ToMethod(context => theConstructor()).InSingletonScope();
}
public void RegisterSingleton<TInterface>(TInterface theObject) where TInterface : class
{
kernel.Bind<TInterface>().ToConstant(theObject).InSingletonScope();
}
public void RegisterType(Type tFrom, Type tTo)
{
kernel.Bind(tFrom).To(tTo);
}
public void RegisterType(Type t, Func<object> constructor)
{
kernel.Bind(t).ToMethod(context => constructor());
}
public void RegisterType<TInterface>(Func<TInterface> constructor) where TInterface : class
{
kernel.Bind<TInterface>().ToMethod(context => constructor());
}
public object Resolve(Type type)
{
return kernel.Get(type);
}
public T Resolve<T>() where T : class
{
return kernel.Get<T>();
}
public bool TryResolve(Type type, out object resolved)
{
resolved = kernel.TryGet(type);
return (resolved != null);
}
public bool TryResolve<T>(out T resolved) where T : class
{
resolved = kernel.TryGet<T>();
return (resolved != null);
}
void IMvxIoCProvider.RegisterType<TFrom, TTo>()
{
kernel.Bind<TFrom>().To<TTo>();
}
}
}
using System.Collections.Generic;
using System.Reflection;
using Android.Content;
using Cirrious.CrossCore;
using Cirrious.CrossCore.Converters;
using Cirrious.CrossCore.IoC;
using Cirrious.CrossCore.Platform;
using Cirrious.MvvmCross.Droid.Platform;
using Cirrious.MvvmCross.ViewModels;
using SQLite.Net.Interop;
using SQLite.Net.Platform.XamarinAndroid;
using %COMPANYNAME%.Android.ValueConverters;
using %COMPANYNAME%.Core.MvvmCross;
using SupportLibrary = Android.Support;
namespace %COMPANYNAME%.Android
{
public class Setup : MvxAndroidSetup
{
public Setup(Context applicationContext) : base(applicationContext)
{
}
protected override IMvxApplication CreateApp()
{
return new Core.App();
}
protected override void InitializeFirstChance()
{
base.InitializeFirstChance();
Mvx.RegisterSingleton<ISQLitePlatform>(new SQLitePlatformAndroid());
}
protected override void InitializeLastChance()
{
base.InitializeLastChance();
RegisterValueConverters();
}
protected override IList<Assembly> AndroidViewAssemblies
{
get
{
var assemblies = base.AndroidViewAssemblies;
assemblies.Add(typeof(SupportLibrary.Design.Widget.NavigationView).Assembly);
assemblies.Add(typeof(SupportLibrary.Design.Widget.FloatingActionButton).Assembly);
assemblies.Add(typeof(SupportLibrary.V7.Widget.Toolbar).Assembly);
assemblies.Add(typeof(SupportLibrary.V4.Widget.DrawerLayout).Assembly);
assemblies.Add(typeof(SupportLibrary.V4.View.ViewPager).Assembly);
assemblies.Add(typeof(Cirrious.MvvmCross.Droid.Support.RecyclerView.MvxRecyclerView).Assembly);
return assemblies;
}
}
protected override IMvxTrace CreateDebugTrace()
{
return new DebugTrace();
}
protected override IMvxIoCProvider CreateIocProvider()
{
return new NinjectMvxIocProvider();
}
private void RegisterValueConverters()
{
var registry = Mvx.Resolve<IMvxValueConverterRegistry>();
registry.AddOrOverwrite("FavoriteDrawable", new FavoriteDrawableValueConverter());
}
}
}
@roubachof
Copy link
Author

As you can see, if I don't register ValueConverter and Messenger manually, they are not registered in the container. But all others Mvx components are well-registered (well so far).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment