
上QQ阅读APP看书,第一时间看更新
Creating the resolver
The resolver will be responsible for creating our objects for us based on the type that we request. Let's create the resolver:
- In the root of the .NET Standard library project, create a new file called Resolver.cs.
- Add the following code to the file:
using Autofac;
namespace DoToo
{
public static class Resolver
{
private static IContainer container;
public static void Initialize(IContainer container)
{
Resolver.container = container;
}
public static T Resolve<T>()
{
return container.Resolve<T>();
}
}
}
The container property of the IContainer type is defined in Autofac and represents a container that holds the configuration on how to resolve types. The Initialize method takes an instance of an object that implements the IContainer interface and assigns it to the container property. The Resolve method uses the container to resolve a type to an instance of an object. While it might seem strange to use this at first, it will become much easier with experience.