clarification
This article is part of the OverallAuth 2.0 series of articles, which is updated weekly with an article in the series (Complete System Development from 0 to 1).
I will try to be very detailed in this system article, so that it can be understood by novice and veteran alike.
DESCRIPTION: OverallAuth 2.0 is a simple, easy to understand and powerful Permissions + Visual Process Management system.
Friendly reminder: this article is part of a series of articles, before reading that article, it is recommended to read the previous articles to better understand the structure of the project.
If you are interested, please follow me (*^▽^*).
Follow me. Beat me if you can't learn.
About Autofac
Although Microsoft has its own Ioc framework, I would say Autofac should be considered the best Ioc framework I've ever used.
With its high performance, flexibility, and support for Aop, it's a developer's dream.
autofac, it can effectively reduce the complex system, code coupling, improve code maintainability.
Not only that, it makes it easy to replace dependencies, thus improving testing efficiency.
Installing Autofac
1. In the startup program, install the latest version of Autofac and...
Creating libraries
1、DomainService:Domain service class
2. Infrastructure: infrastructure classes
3. CoreDomain: core domain class
If you read each article in the series, you'll see that we have an underlying architecture that uses the DDD domain-driven model design pattern to build the backend.
The system architecture is shown in Fig:
With the system architecture in place, we started using Autofac
Create AutofacPlugIn class file
Create the AutofacPlugIn class file in the system PlugIn file with the following contents:
/// <summary> /// Autofac Plugin /// </summary> public class AutofacPlugIn : { /// <summary> /// Overriding Autofac's Load method /// </summary> /// <param name="containerBuilder"></param> protected override void Load(ContainerBuilder containerBuilder) { //Service line program set Assembly service = ("DomainService"); Assembly intracface = ("Infrastructure"); //Items must end in xxx (service).Where(n => ("Service") && !) .InstancePerLifetimeScope().AsImplementedInterfaces(); (intracface).Where(n => ("Repository") && !) .InstancePerLifetimeScope().AsImplementedInterfaces(); } }
Code Explanation:
Get the DomainService and Infrastructure assemblies and inject them in bulk.
It's worth noting that we gave qualifications when we created the classes. That is, the classes below the assembly must end with Service and Repository, otherwise autofac will throw an error.
Of course if there is something that ends in xxx, then there must be something that begins with xxx. Easy to fix with StartsWith.
Composition of Autofac in Program
net8 autofac registration and the past a little out, you need to add the following code in the Program
var builder = (args); //Customizing Autofac Middleware (new AutofacServiceProviderFactory()).ConfigureContainer<ContainerBuilder>(builder => { <AutofacPlugIn>(); });
After the above configuration, autofac has been configured, is not very simple, next test.
utilization
Create ISysUserRepository and SysUserRepository warehousing interfaces and interface implementations in the Infrastructure class
The structure is as follows:
The content code is as follows:
ISysUserRepository
/// <summary> /// User Services Warehouse Interface /// </summary> public interface ISysUserRepository { /// <summary> /// Testing Autofac /// </summary> /// <returns></returns> string TestAutofac(); }
SysUserRepository
/// <summary> /// User Service Warehouse Interface Implementation /// </summary> public class SysUserRepository : ISysUserRepository { /// <summary> /// Testing Autofac /// </summary> /// <returns></returns> public string TestAutofac() { return "Autofac used successfully"; } }
Create ISysUserService and SysUserService service interfaces and implementations in the DomainService
The structure is as follows:
The content code is as follows
ISysUserService
/// <summary> /// User Service Interface /// </summary> public interface ISysUserService { /// <summary> /// Testing Autofac /// </summary> /// <returns></returns> string TestAutofac(); }
SysUserService
/// <summary> /// User Service Interface implementation /// </summary> public class SysUserService : ISysUserService { #region Construct instantiation private readonly ISysUserRepository _sysUserRepository; public SysUserService(ISysUserRepository sysUserRepository) { _sysUserRepository = sysUserRepository; } #endregion /// <summary> /// Testing Autofac /// </summary> /// <returns></returns> public string TestAutofac() { return _sysUserRepository.TestAutofac(); } }
beta (software)
Call the interface in the SysUserController controller (the controller, which has already been built in the previous article, for those who didn't read the previous article, it's okay to comment on a webapi controller).
The code is as follows:
#region constructive realism (math.) /// <summary> /// customer service /// </summary> public ISysUserService _userService; /// <summary> /// constructor /// </summary> /// <param name="userService"></param> public SysUserController(ISysUserService userService) { _userService = userService; } #endregion /// <summary> /// Get Token /// </summary> /// <param name="userName">user ID</param> /// <param name="password">cryptographic</param> [HttpGet] [AllowAnonymous] public string GetToken(string userName, string password) { var loginResult = (new LoginInput { UserName = userName, Password = password }); return ?? string.Empty; } /// <summary> /// Testing Autofac /// </summary> /// <returns></returns> [HttpGet] [AllowAnonymous] public string TestAutofac() { return _userService.TestAutofac(); }
The above code has to instantiate the interface through Autofac first.
Open swagger and start testing
As I said before, our interface must end with xxx, so let's change that convention now and see if we can call it successfully.
Modify the code as follows:
/// <summary> /// Overriding Autofac's Load method /// </summary> /// <param name="containerBuilder"></param> protected override void Load(ContainerBuilder containerBuilder) { //Service line program set Assembly service = ("DomainService"); Assembly intracface = ("Infrastructure"); //Items must end in xxx (service).Where(n => ("Service111111111") && !) .InstancePerLifetimeScope().AsImplementedInterfaces(); (intracface).Where(n => ("Repository2222222") && !) .InstancePerLifetimeScope().AsImplementedInterfaces(); }
As you can see, we changed the end with Service, Repository to Service111111111, Repository2222222, that is, our domain service class and the base storage class are not in line with the renaming specification, aufofac should report an error.
Testing:
These are the uses of aufofac in the content of this issue.
In the next series, will use the combined use of autofac + aop, interested friends, please blog garden attention, microblogging attention, thanks for watching.
Source code address: /yangguangchenjie/overall-auth2.0-web-api
Help me Star, please.
If you are interested, please pay attention to my weibo public number (*^▽^*).
Follow me: a full-stack multi-terminal treasure blogger, regularly sharing technical articles, irregularly sharing open source projects. Follow me to bring you to know a different program world!