Alemow
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)
{
//
}
}
[ComponentAttribute]
indicates this type will be registered automatically;- Scope,
As
,Keyed
,WithMetadata
features from Autofac are supported by different attributes; [InjectAttribute]
annotated fields are resolved whenOnActivating
;[ConfigValueAttribute]
annotated fields are resolved by registeredIConfiguration
whenOnActivating
, different types are supported such asint
,IList<string>
, etc;- The
ILifetimeScope
is attached to the resolved instance byLifetimeScopeAttacher.Instance
; - Other Autofac features such as
PropertiesAutowired
and interceptors are not activated for[ComponentAttribute]
types, see Feature below.
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)
{
//
}
}
[ConfigurationAttribute]
indicates this type will be parsed automatically, if this type implementsAutofac.Core.IModule
, the type will be registered as a module;[BeanAttribute]
indicates this method will be used as the creator and the result will be returned as the resolved instance;[InjectAttribute]
and[ConfigValueAttribute]
are supported when resolving parameters;- Specific type as
IComponentContext
is also supported; - Method should be non-generic.
Feature
You can define any features from IAutoRegisterFeature
and enable with builder.AutoRegister(/**/).Enable(feature);
, features will be applied to all [ComponentAttribute]
types.