ABP framework extends ASP.NET Core's distributed caching system.
ABP framework extends ASP.NET Core's distributed caching system.
## IDistributedCache Interface
## `IDistributedCache` Interface
ASP.NET Core defines the `IDistributedCache` interface to get/set cache values. But it has some difficulties:
ASP.NET Core defines the `IDistributedCache` interface to get/set cache values. But it has some difficulties:
@ -17,7 +17,7 @@ ASP.NET Core defines the `IDistributedCache` interface to get/set cache values.
See [ASP.NET Core's distributed caching document](https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed) for more information.
See [ASP.NET Core's distributed caching document](https://docs.microsoft.com/en-us/aspnet/core/performance/caching/distributed) for more information.
## IDistributedCache<TCacheItem> Interface
## `IDistributedCache<TCacheItem>` Interface
ABP framework defines the generic `IDistributedCache<TCacheItem>` interface in the [Volo.Abp.Caching](https://www.nuget.org/packages/Volo.Abp.Caching/) package. `TCacheItem` is the type of the object stored in the cache.
ABP framework defines the generic `IDistributedCache<TCacheItem>` interface in the [Volo.Abp.Caching](https://www.nuget.org/packages/Volo.Abp.Caching/) package. `TCacheItem` is the type of the object stored in the cache.
@ -82,4 +82,4 @@ Other methods of the `IDistributedCache<BookCacheItem>` are same as ASP.NET Core
@ -75,7 +75,7 @@ Hit F5 (or Ctrl+F5) to run the application. It will have an output like shown be
#### Using EF Core Update-Database Command
#### Using EF Core Update-Database Command
Ef Core has `Update-Database` command which creates database if necessary and applies pending migrations. Right click to the `.Web` project and select **Set as StartUp Project**:
Ef Core has `Update-Database` command which creates database if necessary and applies pending migrations. Right click to the `.HttpApi.Host` project and select **Set as StartUp Project**:
``AppModule`` is a good name for the startup module for an application.
``AppModule`` is a good name for the startup module for an application.
ABP packages define module classes and a module can depend on another module. In the code above, our ``AppModule`` depends on ``AbpAspNetCoreMvcModule`` (defined by Volo.Abp.AspNetCore.Mvc package). It's common to add a ``DependsOn`` attribute after installing a new ABP nuget package.
ABP packages define module classes and a module can depend on another. In the code above, the ``AppModule`` depends on the ``AbpAspNetCoreMvcModule`` (defined by [Volo.Abp.AspNetCore.Mvc](https://www.nuget.org/packages/Volo.Abp.AspNetCore.Mvc) package). It's common to add a ``DependsOn`` attribute after installing a new ABP nuget package.
Instead of Startup class, we are configuring ASP.NET Core pipeline in this module class.
Instead of the Startup class, we are configuring ASP.NET Core pipeline in this module class.
## The Startup Class
## The Startup Class
Next step is to modify Startup class to integrate to ABP module system:
Next step is to modify Startup class to integrate to ABP module system:
Changed ``ConfigureServices`` method to return ``IServiceProvider`` instead of ``void``. This change allows us to replace AspNet Core's Dependency Injection with another framework (see Autofac integration section below). ``services.AddApplication<AppModule>()`` adds all services defined in all modules beginning from the ``AppModule``.
``services.AddApplication<AppModule>()`` adds all services defined in all modules starting from the ``AppModule``.
``app.InitializeApplication()`` call in ``Configure`` method initializes and starts the application.
``app.InitializeApplication()`` in ``Configure`` method initializes and starts the application.
## Hello World!
## Run the Application!
The application above does nothing. Let's create an MVC controller does something:
That's all! Run the application, it will just work as expected.
````C#
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
namespace BasicAspNetCoreApplication.Controllers
{
public class HomeController : AbpController
{
public IActionResult Index()
{
return Content("Hello World!");
}
}
}
````
If you run the application, you will see a "Hello World!" message on the page.
Derived ``HomeController`` from ``AbpController`` instead of standard ``Controller`` class. This is not required, but ``AbpController`` class has useful base properties and methods to make your development easier.
## Using Autofac as the Dependency Injection Framework
## Using Autofac as the Dependency Injection Framework
While AspNet Core's Dependency Injection (DI) system is fine for basic requirements, Autofac provides advanced features like Property Injection and Method Interception which are required by ABP to perform advanced application framework features.
While AspNet Core's Dependency Injection (DI) system is fine for basic requirements, [Autofac](https://autofac.org/) provides advanced features like Property Injection and Method Interception which are required by ABP to perform advanced application framework features.
Replacing AspNet Core's DI system by Autofac and integrating to ABP is pretty easy.
Replacing AspNet Core's DI system by Autofac and integrating to ABP is pretty easy.
@ -143,40 +125,29 @@ public class AppModule : AbpModule
}
}
````
````
3. Change ``services.AddApplication<AppModule>();`` line in the ``Startup`` class as shown below:
3. Update `Program.cs` to use Autofac:
````C#
services.AddApplication<AppModule>(options =>
{
options.UseAutofac(); //Integrate to Autofac
});
````
4. Update `Program.cs` to not use the `WebHost.CreateDefaultBuilder()` method since it uses the default DI container: