Alemow

NuGet NuGet

License MIT

GitHub stars GitHub forks GitHub watchers

Alemow is an annotation based IoC boostrapper, working with Autofac, and reducing registering complexity heavily.

Basics

Alemow currently only targets netstandard2.0, and only Autofac is integrated for now.

Usages

NuGet

Install-Package Alemow.Autofac

Register

    // var builder = new ContainerBuilder();
    builder.AutoRegister(new SimpleAssemblySelector(Assembly.GetEntryAssembly()));

or

    // var builder = new ContainerBuilder();
    builder.AutoRegisterBaseDirectory(AssemblyLoadContext.Default.LoadFromAssemblyPath, excludes: Enumerables.List(@"System.*", @"Microsoft.*")); // use Assembly.LoadFile for dotnetfx

ComponentAttribute

AutoRegister will load all defined types from IAssemblySelector.Find with [ComponentAttribute] annotated.

    [Component(asSelf: true, asImplementedInterfaces: true),
     Scope(Scope.Singleton),
     //As(typeof(IService)),
     Key("A", typeof(IService))]
    public class Service : IService
    {
        [Inject] private readonly IOtherService _otherService;

        [ConfigValue("api:key")] private readonly string _apiKey;

        public Service(IRepository repository)
        {
            //
        }
    }

ConfigurationAttribute

    [Configuration]
    public class DataSourceConfiguration : Module
    {
        [Bean(false, false),
         Scope(Scope.PerDependency),
         As(typeof(IDataSource)),
         Key("DataSource", typeof(IDataSource))]
        public IDataSource DefineDataSource([ConfigValue("foo")] string foo, [Inject] IConfiguration configuration, IComponentContext context)
        {
            return new DataSource();
        }

        protected override void Load(ContainerBuilder builder)
        {
            //
        }
    }

Feature

You can define any features from IAutoRegisterFeature and enable with builder.AutoRegister(/**/).Enable(feature);, features will be applied to all [ComponentAttribute] types.