Getting Started With Autofac

2023-12-11 03:32
文章标签 started autofac getting

本文主要是介绍Getting Started With Autofac,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Getting Started

入门指南

The basic pattern for integrating Autofac into your application is:

  • Structure your app with inversion of control (IoC) in mind.
  • Add Autofac references.
  • At application startup…
  • Create a ContainerBuilder.
  • Register components.
  • Build the container and store it for later use.
  • During application execution…
  • Create a lifetime scope from the container.
  • Use the lifetime scope to resolve instances of the components.

将Autofac集成到你的应用中基本步骤如下:

  • 考虑使用IoC构建您的应用程序.
  • 添加Autofac引用.
  • 在程序启动的地方…
  • 创建对象ContainerBuilder.
  • 注册组件.
  • 生成容器并存储以供以后使用.
  • 在应用程序执行期间…
  • 从容器中创建一个生命周期.
  • 使用生命周期获取组件的实例.

This getting started guide walks you through these steps for a simple console application. Once you have the basics down, you can check out the rest of the wiki for more advanced usage and integration information for WCF, ASP.NET, and other application types.

本入门指南将指导您完成一个简单的控制台应用程序的这些步骤。. 一旦掌握了基本知识,您可以查看wiki的其他部分比如:高级用法,与WCF, ASP.NET, 其它类型的程序的集成方法.

Structuring the Application

构建应用程序

The idea behind inversion of control is that, rather than tie the classes in your application together and let classes “new up” their dependencies, you switch it around so dependencies are instead passed in during class construction. Martin Fowler has an excellent article explaining dependency injection/inversion of control if you want more on that.

Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制. 如果你想了解更多Martin Fowler 有一篇很好的文章,解释了依赖注入/控制的反转 .

For our sample app, we’ll define a class that writes the current date out. However, we don’t want it tied to the Console because we want to be able to test the class later or use it in a place where the console isn’t available.

对于示例应用程序,我们将定义一个写入当前日期的类。但是,我们不希望它与控制台绑定,因为我们希望稍后能够测试该类,或者在控制台不可用的地方使用它。

We’ll also go as far as allowing the mechanism writing the date to be abstracted, so if we want to, later, swap in a version that writes tomorrow’s date, it’ll be a snap.

我们还将尽可能允许抽象化编写日期的机制,所以如果我们以后想交换一个写明天日期的版本,那就很简单了。

We’ll do something like this:

如下为示例代码

using System;namespace DemoApp
{// This interface helps decouple the concept of// "writing output" from the Console class. We// don't really "care" how the Write operation// happens, just that we can write.public interface IOutput{void Write(string content);}// This implementation of the IOutput interface// is actually how we write to the Console. Technically// we could also implement IOutput to write to Debug// or Trace... or anywhere else.public class ConsoleOutput : IOutput{public void Write(string content){Console.WriteLine(content);}}// This interface decouples the notion of writing// a date from the actual mechanism that performs// the writing. Like with IOutput, the process// is abstracted behind an interface.public interface IDateWriter{void WriteDate();}// This TodayWriter is where it all comes together.// Notice it takes a constructor parameter of type// IOutput - that lets the writer write to anywhere// based on the implementation. Further, it implements// WriteDate such that today's date is written out;// you could have one that writes in a different format// or a different date.public class TodayWriter : IDateWriter{private IOutput _output;public TodayWriter(IOutput output){this._output = output;}public void WriteDate(){this._output.Write(DateTime.Today.ToShortDateString());}}
}
Now that we have a reasonably structured (if contrived) set of dependencies, let’s get Autofac in the mix!

现在我们有了一个合理的结构化(如果设计的)依赖集,让我们和Autofac结合使用!

Add Autofac References

增加Autofac引用

The first step is to add Autofac references to your project. For this example, we’re only using core Autofac. Other application types may use additional Autofac integration libraries..

第一个是引用Autofac到你的项目中 .例如, 我们使用 core 版本的Autofac. 其它类型的程序引用对应类型版本的Autofac.

The easiest way to do this is through NuGet. The “Autofac” package has all the core functionality you’ll need.

最简单的方法是通过NuGet。“Autofac”软件包具有您所需的所有核心功能。

../_images/gsnuget.png

Application Startup

应用程序的启动

At application startup, you need to create a ContainerBuilder and register your components with it. A component is an expression, .NET type, or other bit of code that exposes one or more services and can take in other dependencies.

在应用程序启动时,您需要创建ContainerBuilder生成器并注册你的组件。一个组件是一个表达式,.NET类型,或其他实现了依赖项的公开一个或多个服务。

In simple terms, think about a .NET type that implements an interface, like this:

简单地说,一个.Net类实现一个接口,如下所示:

public class SomeType : IService
{
}
You could address that type in one of two ways:

您可以用两种方式之一来解决该类型问题:

  • As the type itself, SomeType
  • As the interface, an IService
  • 类型本身, SomeType
  • 接口,  IService

 In this case, the component is SomeType and the services it exposes are SomeType and IService.

在这种情况下,组件是SomeType,它公开的服务是SomeTypeIService

In Autofac, you’d register that with a ContainerBuilder something like this:

 在Autofac中,你可以注册一个ContainerBuilder 如下代码所示:

// Create your builder.
var builder = new ContainerBuilder();// Usually you're only interested in exposing the type
// via its interface:
builder.RegisterType<SomeType>().As<IService>();// However, if you want BOTH services (not as common)
// you can say so:
builder.RegisterType<SomeType>().AsSelf().As<IService>();

 For our sample app, we need to register all of our components (classes) and expose their services (interfaces) so things can get wired up nicely.

对于我们的示例应用程序,我们需要注册我们所有的组件(类)并公开它们的服务(接口),以便事情可以很好地连接起来。

We also need to store the container so it can be used to resolve types later.

我们还需要存储该容器,以便它以后可以用于解析类型。

using System;
using Autofac;namespace DemoApp
{public class Program{private static IContainer Container { get; set; }static void Main(string[] args){var builder = new ContainerBuilder();builder.RegisterType<ConsoleOutput>().As<IOutput>();builder.RegisterType<TodayWriter>().As<IDateWriter>();Container = builder.Build();// The WriteDate method is where we'll make use// of our dependency injection. We'll define that// in a bit.WriteDate();}}
}

 Now we have a container with all of the components registered and they’re exposing the proper services. Let’s make use of it.

现在我们有了一个注册了所有组件的容器,它们正在公开适当的服务。让我们好好利用一下它吧。

Application Execution

应用程序的执行

During application execution, you’ll need to make use of the components you registered. You do this by resolving them from a lifetime scope.

在应用程序执行期间,您需要使用您注册的组件。您可以组件的生命周期内解析它并使用。

The container itself is a lifetime scope, and you can technically just resolve things right from the container. It is not recommended to resolve from the container directly, however.

容器本身有一个生命周期,技术上可以从容器中直接获取组件。但是,不建议直接从容器中解析。

When you resolve a component, depending on the instance scope you define, a new instance of the object gets created. (Resolving a component is roughly equivalent to calling “new” to instantiate a class. That’s really, really oversimplifying it, but from an analogy perspective it’s fine.) Some components may need to be disposed (like they implement IDisposable) - Autofac can handle disposing those components for you when the lifetime scope is disposed.

解析组件时,根据定义的实例范围,将创建对象的新实例。(解析一个组件大致相当于调用“new”来实例化一个类。这真的,真的太过简化了,但从类比的角度来看还可以。)某些组件可能需要释放(比如实现接口IDisposable)——Autofac可以在处理生命周期范围时为您处理处理这些组件。

However, the container lives for the lifetime of your application. If you resolve a lot of stuff directly from the container, you may end up with a lot of things hanging around waiting to be disposed. That’s not good (and you may see a “memory leak” doing that).

但是,该容器将使用应用程序的生命周期。如果你直接从容器中解决了很多东西,你可能会有很多东西等待被处理。这并不好(你可能会看到“内存泄漏”)。

Instead, create a child lifetime scope from the container and resolve from that. When you’re done resolving components, dispose of the child scope and everything gets cleaned up for you.

相反,从容器创建子生命周期范围,并从其解析。完成组件解析后,处理子范围,并为您清理所有内容。

(When you’re working with the Autofac integration libraries, this child scope creation is largely done for you so you don’t have to think about it.)

(在使用Autofac集成库时,此子生命周期已经为你完成了大部分功能,因此您不必考虑它。)

For our sample app, we’ll implement the “WriteDate” method to get the writer from a scope and dispose of the scope when we’re done.

对于我们的示例应用程序,我们将实现“WriteDate”方法,以从一个范围中获取写入程序,并在完成后做了相关的处理。

namespace DemoApp
{public class Program{private static IContainer Container { get; set; }static void Main(string[] args){// ...the stuff you saw earlier...}public static void WriteDate(){// Create the scope, resolve your IDateWriter,// use it, then dispose of the scope.using (var scope = Container.BeginLifetimeScope()){var writer = scope.Resolve<IDateWriter>();writer.WriteDate();}}}
}
Now when you run your program…

现在,运行你的程序

  • The WriteDate method creates a lifetime scope from which it can resolve dependencies. It does this to avoid any memory leaks - if IDateWriter or its dependencies are disposable, they will be automatically disposed when the scope is disposed.
  • The WriteDate method manually resolves an IDateWriter from the lifetime scope. (This is “service location.”) Internally…
    • Autofac sees that IDateWriter maps to TodayWriter so starts creating a TodayWriter.
    • Autofac sees that the TodayWriter needs an IOutput in its constructor. (This is “constructor injection.”)
    • Autofac sees that IOutput maps to ConsoleOutput so creates a new ConsoleOutput instance.
    • Autofac uses the new ConsoleOutput instance to finish constructing the TodayWriter.
    • Autofac returns the fully-constructed TodayWriter for WriteDate to consume.
  • The call to writer.WriteDate() goes to the brand new TodayWriter.WriteDate() since that’s what was resolved.
  • The Autofac lifetime scope is disposed. Any disposable items that were resolved from that lifetime scope are also disposed.
  • WriteDate 方法创建一个生命周期范围,它可以从中解决依赖关系。它这样做是为了避免任何内存泄漏——如果IDateWriter或其依赖项是一次性的,它们将在释放范围时自动释放。
  • WriteDate 方法从生命周期范围内手动解析IDateWriter。(这是“service location.”)内部信息…
    • Autofac 查看 IDateWriter 映射 TodayWriter 关系,创建了一个 TodayWriter.
    • Autofac 查看 TodayWriter 需要 IOutput 在构造函数中. (这是 “构造注入.”)
    • Autofac 查找  IOutput 映射 ConsoleOutput关系,创建了 一个 ConsoleOutput 实例.
    • Autofac 使用 ConsoleOutput 完成构造 TodayWriter.
    • Autofac 返回构造好的 TodayWriter 给WriteDate使用.
  • 这个调用 writer.WriteDate() 为新的 TodayWriter.WriteDate() 从而解决了该问题.
  • Autofac 生命周期范围已被释放. 任何可以释放的子项也会相应的被释放.

Later, if you want your application to write a different date, you could implement a different IDateWriter and then change the registration at app startup. You don’t have to change any other classes. Yay, inversion of control!

稍后,如果希望应用程序编写不同的日期,则可以实现不同的 IDateWriter ,然后在应用程序启动时更改注册。您不必更改任何其他类。很帅,控制反转!

Note: generally speaking, service location is largely considered an anti-pattern (see article). That is, manually creating scopes everywhere and sprinkling use of the container through your code is not necessarily the best way to go. Using the Autofac integration libraries you usually won’t have to do what we did in the sample app above. Instead, things get resolved from a central, “top level” location in the application and manual resolution is rare. Of course, how you design your app is up to you.

注:一般来说,服务位置主要被认为是一种反模式的(见文章)。也就是说,在到处手动创建范围,并通过代码零星地使用容器并不一定是最好的方法。使用Autofac集成库,通常不必执行上面示例应用程序中所做的操作。相反,从应用程序中的中央“顶级”位置解决,手动解决很少。当然,你如何设计你的应用程序取决于你自己。

Going Further

更进一步

The sample app gives you an idea of how to use Autofac, but there’s a lot more you can do.

示例应用程序让您了解如何使用Autofac,但您可以做更多。

  • Check out the list of integration libraries to see how to integrate Autofac with your application.
  • Learn about the ways to register components that add flexibility.
  • Learn about Autofac configuration options that allow you to better manage your component registrations.
  • 查阅 集成库列表 来查找 Autofac 如何与你的应用程序集成.
  • 学会更灵活的 注册组件 .
  • 学习 Autofac 配置选项 更好的管理你的组件注册.

Need Help?

需要帮助?

  • You can ask questions on StackOverflow.
  • You can participate in the Autofac Google Group.
  • There’s an introductory Autofac tutorial on CodeProject.
  • We have advanced debugging tips if you want to dive deep.
  • 你可以在 StackOverflow 上提问.
  • 你可以在 Autofac Google Group 练习.
  • 这时有相关的 Autofac 材料 .
  • 如果你想深入了解,我们有 高级调试指南.

Building from Source

从源码编译

The source code along with Visual Studio project files is available on GitHub. Build instructions and details on contributing can be found in the Contributor Guide.

在 GitHub 上有源码. 在 贡献文档 上可以可以找到构建说明和关于贡献的详细信息.

这篇关于Getting Started With Autofac的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/479319

相关文章

Ubuntu 18启动失败 Started Hold until boot procss finishes up

原因: 启动ubuntu 的时候,磁盘空间不够了。 解决方法: 启动Ubuntu 的时候,选择Advanced options for Ubuntu 然后选择recovery 之后选择clean 清理之后,就可以打开了。

启动Eclipset提示: java was started but returned exit code=13

启动Eclipset提示: java was started but returned exit code=13 今天启动Eclipse时打不开,提示信息如下:  【解决办法】 这种情况一般是JDK版本和Eclipse版本不一致造成的,例如JDK是32位,Eclipse是64位。 卸载掉32位的JDK重新安装64的JDK即可。

Autofac和CommunityToolkit中IOC的区别

Autofac和CommunityToolkit中IOC的区别 Autofac和CommunityToolkit中IOC的区别Autofac1、‌定义与功能‌:2、集成与扩展‌:3、学习曲线 CommunityToolkit中的IOC总结 Autofac和CommunityToolkit中IOC的区别 Autofac和CommunityToolkit中的IOC(控制反转)在概念

Getting RateLimitError while implementing openai GPT with Python

题意:“在使用 Python 实现 OpenAI GPT 时遇到 RateLimitError 错误。” 问题背景: I have started to implement openai gpt model in python. I have to send a single request in which I am getting RateLimitError. “我开始在 Py

Tutorial : Getting Started with Kubernetes on your Windows Laptop with Minikube

https://rominirani.com/tutorial-getting-started-with-kubernetes-on-your-windows-laptop-with-minikube-3269b54a226#.d9lmuvzf2 本文的注意事项: 1, 截止到2017.01.20, window上的kubernetes依然是实验性的, 存在各种不可预知的bug

adb server version (31) doesn't match this client (40); killing... daemon started successfully

adb多个版本导致引发的问题 使用adb connect ip 连接局域网的手机的时候,总是报faile to connect ip ? 以前都是通过局域网wifi 连接手机,调试。但是最近一段时间总出现faile to connect xxxx.各种百度和 google 都么有找到解决方法。 然而,功夫不负有心人,在今天领导让调试创维的盒子的时候,需要使用到adb命令,使用adb GUI 可视

zabbix出现active check configuration update from [127.0.0.1:10051] started to fail (cannot connect to

出现active check configuration update from [127.0.0.1:10051] started to fail (cannot connect to [[127.0.0.1]:10051]: [111] Connection refused),直接编辑zabbix_agentd.conf(vi /usr/local/zabbix/etc/zabbix_agen

【Hadoop】Flume NG Getting Started(Flume NG 新手入门指南)翻译

新手入门 Flume NG是什么? 有什么改变? 获得Flume NG 从源码构建 配置 flume-ng全局选项flume-ng agent选项flume-ng avro-client 选项 提供反馈 Flume NG是什么? Flume NG的目标是比Flume OG在简单性,大小和容易部署上有显著性地提高。为了实现这个目标,Flume NG将不会兼容Flume OG.我们目

ASP .NET Core 中的 Autofac 依赖注入

介绍 Autofac 是适用于 .NET 应用程序(包括 ASP.NET Core)的流行依赖注入 (DI) 容器。Autofac 等 DI 容器通过提供注册和解析依赖关系的方法来帮助管理应用程序不同组件之间的依赖关系。 为什么需要Autofac,它能实现什么? 控制反转 (IoC): Autofac 实现了控制反转 (IoC) 原则,允许您反转创建和管理对象实例的控制。Autofac 将依

Huggingface Transformers库学习笔记(一):入门(Get started)

前言 Huggingface的Transformers库是一个很棒的项目,该库提供了用于自然语言理解(NLU)任务(如分析文本的情感)和自然语言生成(NLG)任务(如用新文本完成提示或用另一种语言翻译)的预先训练的模型。其收录了在100多种语言上超过32种预训练模型。这些先进的模型通过这个库可以非常轻松的调取。同时,也可以通过Pytorch和TensorFlow 2.0进行编写修改等。 本系列学