Created
December 13, 2016 05:39
-
-
Save madhav-ocean/7edc0977c372c0f78b36677db7902e32 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Reflection; | |
using SimpleInjector; | |
using System.Linq; | |
using MyApp.ViewModels; | |
using MyApp.Infrastructure.Navigation; | |
using MyApp.Models; | |
using MyApp.Services.RestClient; | |
using MyApp.Services; | |
using System.Collections.Generic; | |
namespace MyApp.Infrastructure | |
{ | |
public class BootStrap | |
{ | |
static BootStrap() | |
{ | |
RegisterDependencies(); | |
} | |
public static Container IocContainer { get; set; } | |
private static void RegisterDependencies() | |
{ | |
var container = new Container(); | |
container.RegisterSingleton<MyAppCacheDatabase>(); | |
container.RegisterSingleton<AppOverall>(); | |
container.Register<App>(); | |
var currentdomain = typeof(string).GetTypeInfo().Assembly.GetType("System.AppDomain").GetRuntimeProperty("CurrentDomain").GetMethod.Invoke(null, new object[] { }); | |
var getassemblies = currentdomain.GetType().GetRuntimeMethod("GetAssemblies", new Type[] { }); | |
var assemblies = getassemblies.Invoke(currentdomain, new object[] { }) as Assembly[]; | |
var definedtypes = assemblies.SelectMany(a => a.DefinedTypes).ToList(); | |
//Register Viewmodels | |
var viewmodeltypes = definedtypes.Where(t => t.IsClass && !t.IsSealed && t.BaseType == typeof(BaseViewModel)); | |
foreach (var viewModel in viewmodeltypes) | |
{ | |
container.Register(viewModel.AsType()); | |
} | |
RegisterService(container, definedtypes); | |
var nav = new NavigationService(definedtypes.ToList()); | |
container.RegisterSingleton<IExtNavigationService>(nav); | |
IocContainer = container; | |
} | |
private static void RegisterService(Container container, List<TypeInfo> definedtypes) | |
{ | |
container.Register(typeof(ICacheService<>), typeof(CacheService<>), Lifestyle.Transient); | |
container.RegisterSingleton<IApiClient, ApiClient>(); | |
var serviceTypes = definedtypes.Where(t => t.IsClass && !t.IsSealed && t.Namespace == "MyApp.Services" && !t.IsGenericType).ToList(); | |
var interfaceTypes = definedtypes.Where(t => t.IsInterface && !t.IsSealed && t.Namespace == "MyApp.Services" && !t.IsGenericType).ToList(); | |
foreach (var service in serviceTypes) | |
{ | |
var interfacetype = interfaceTypes.FirstOrDefault(x => x.Name == $"I{service.Name}"); | |
if(interfacetype != null) | |
{ | |
container.Register(interfacetype.AsType(), service.AsType(), Lifestyle.Transient); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment