Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using Autofac;
- namespace Example.Common
- {
- public static class IoC
- {
- private static IContainer _container { get; set; }
- private static ContainerBuilder builder = new ContainerBuilder();
- static List<Assembly> allAssemblies = new List<Assembly>();
- public static IContainer Container
- {
- get
- {
- if (_container == null)
- {
- Init();
- }
- return _container;
- }
- }
- public static void Init()
- {
- LoadEveryAssembly();
- foreach (Assembly a in allAssemblies)
- {
- builder.RegisterAssemblyTypes(a)
- .Where(t => t.Name.EndsWith("Context")).AsSelf().SingleInstance();
- builder.RegisterAssemblyTypes(a)
- .Where(t => t.Name.EndsWith("Repository"));
- builder.RegisterAssemblyTypes(a)
- .Where(t => t.Name.EndsWith("Manager"))
- .AsImplementedInterfaces()
- .AsSelf();
- builder.RegisterAssemblyTypes(a)
- .Where(t => t.Name.EndsWith("Query")).AsImplementedInterfaces()
- .AsSelf();
- builder.RegisterAssemblyTypes(a)
- .Where(t => t.Name.EndsWith("Service"))
- .AsImplementedInterfaces()
- .AsSelf();
- }
- _container = builder.Build();
- }
- private static void LoadEveryAssembly()
- {
- string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- foreach (string dll in Directory.GetFiles(path, "*.dll"))
- allAssemblies.Add(Assembly.LoadFile(dll));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement