# 23.5、快捷操作

## 如何在实例类/静态类中使用接口，比如仓储？

由于我们目前采用的是自动抽象工厂模式，也就是面向接口编程，对象都是通过 **依赖注入/控制反转**创建的，所以我们通常在 类的构造函数中注入对象

但是，有一些特殊情况，我们需要在静态类或实例类中也能够使用这些接口，而非通过构造函数注入的方式。

* 获取数据库操作`仓储实例`对象

```csharp
var repository = AppGlobal.GetRepository<TableEntity>();
var entities = repository.GetAll(u=>u.Id > 1).ToList();
```

* 获取 `Autofac`注入的接口实例对象

```csharp
var service = AppGlobal.AutofacContainer.Resolve<IHoaApplicationService>();
```

* 获取 `ASP.NET Core` 注入的对象，比如内存缓存

```csharp
var _cache = AppGlobal.ServiceProvider.GetService<IMemoryCache>();
```

* 获取多上下文对象 `IDynamicRepository<TEntity, DbContextIdentifier>`

```csharp
var dynamicRepository = AppGlobal.GetDynamicRepository<TableEntity, DbContextIdentifier>(nameof(DbContextIdentifier));
```

* 获取选项配置

```csharp
var options = AppGlobal.GetOptions<AppOptions>();
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://monksoul.gitbook.io/hoa/qitagongneng/kuaijiecaozuo.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
