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