Register factory class in Autofac using web.config

I was looking for a way to register a factory class in autofac using XmlConfiguration , but the documentation seems a bit subtle.

What I want to do is do the following in my configuration:

builder.Register(c => MyFactory.GetMyObject()).As<IMyObject>();

Is there a good way to do this?

+3
source share
3 answers

The way to do this is to encapsulate your registration into an Autofac module, which can be registered from XML. (The modules are described here: http://code.google.com/p/autofac/wiki/StructuringWithModules )

Your module will look something like this:

public class MyModule : Module
{
  protected override void Load(ContainerBuilder builder)
  {
    builder.Register(c => MyFactory.GetMyObject()).As<IMyObject>();
  }
}

And the XML for registering it:

<module type="MyModule" />
+3
source

Autofac , , , .

, - Spring.NET Castle. , , XML.

0

, -, web.config, .

, , , , Global.asax. , .

0

Source: https://habr.com/ru/post/1711749/


All Articles