Skip to content

Commit 995b474

Browse files
author
v-anzha
committed
simple class for aop & swagger for enum
1 parent caa3a6d commit 995b474

9 files changed

Lines changed: 87 additions & 17 deletions

File tree

Blog.Core.Model/Love.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace Blog.Core.Model
77
/// </summary>
88
public class Love
99
{
10+
public virtual string SayLoveU()
11+
{
12+
return "I ♥ U";
13+
}
1014
/// <summary>
1115
/// id
1216
/// </summary>

Blog.Core.Model/ResponseEnum.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Text;
5+
6+
namespace Blog.Core.Model
7+
{
8+
public enum ResponseEnum
9+
{
10+
/// <summary>
11+
/// 无权限
12+
/// </summary>
13+
[Description("无权限")]
14+
NoPermissions = 401,
15+
/// <summary>
16+
/// 找不到指定资源
17+
/// </summary>
18+
[Description("找不到指定资源")]
19+
NoFound = 404,
20+
/// <summary>
21+
/// 找不到指定资源
22+
/// </summary>
23+
[Description("服务器错误")]
24+
ServerError = 500
25+
}
26+
}

Blog.Core/Blog.Core.Model.xml

Lines changed: 18 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Blog.Core/Blog.Core.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Blog.Core/Controllers/TopicDetailController.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public async Task<MessageModel<string>> Post([FromBody] TopicDetail topicDetail)
113113
// PUT: api/TopicDetail/5
114114
[HttpPut]
115115
[Route("update")]
116+
[AllowAnonymous]
116117
public async Task<MessageModel<string>> Put(int id, [FromBody] TopicDetail topicDetail)
117118
{
118119
var data = new MessageModel<string>();

Blog.Core/Controllers/ValuesController.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,33 @@ public class ValuesController : ControllerBase
2525
{
2626
private IMapper _mapper;
2727
private IAdvertisementServices _advertisementServices;
28+
private Love _love;
2829

2930
/// <summary>
3031
/// ValuesController
3132
/// </summary>
3233
/// <param name="mapper"></param>
3334
/// <param name="advertisementServices"></param>
34-
public ValuesController(IMapper mapper, IAdvertisementServices advertisementServices)
35+
public ValuesController(IMapper mapper, IAdvertisementServices advertisementServices, Love love)
3536
{
3637
// 测试 Authorize 和 mapper
3738
_mapper = mapper;
3839
_advertisementServices = advertisementServices;
40+
_love = love;
3941
}
4042
/// <summary>
4143
/// Get方法
4244
/// </summary>
4345
/// <returns></returns>
4446
// GET api/values
4547
[HttpGet]
46-
public ActionResult<IEnumerable<string>> Get()
48+
public async Task<MessageModel<ResponseEnum>> Get()
4749
{
50+
var data = new MessageModel<ResponseEnum>();
51+
4852
_advertisementServices.ReturnExp();
4953

50-
return new string[] { "value1", "value2" };
54+
return data;
5155
}
5256
/// <summary>
5357
/// Get(int id)方法
@@ -59,6 +63,8 @@ public ActionResult<IEnumerable<string>> Get()
5963
[AllowAnonymous]
6064
public ActionResult<string> Get(int id)
6165
{
66+
var loveu = _love.SayLoveU();
67+
6268
return "value";
6369
}
6470
/// <summary>

Blog.Core/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"Blog.Core": {
2020
"commandName": "Project",
2121
"launchBrowser": true,
22-
"applicationUrl": "http://localhost:5000",
22+
"applicationUrl": "http://localhost:8081",
2323
"environmentVariables": {
2424
"ASPNETCORE_ENVIRONMENT": "Development"
2525
}

Blog.Core/Startup.cs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using Blog.Core.Common;
1616
using Blog.Core.Filter;
1717
using Blog.Core.Log;
18+
using Blog.Core.Model;
1819
using log4net;
1920
using log4net.Config;
2021
using log4net.Repository;
@@ -151,8 +152,6 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
151152

152153

153154
//就是这里
154-
155-
156155
var xmlPath = Path.Combine(basePath, "Blog.Core.xml");//这个就是刚刚配置的xml文件名
157156
c.IncludeXmlComments(xmlPath, true);//默认的第二个参数是false,这个是controller的注释,记得修改
158157

@@ -290,13 +289,9 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
290289
builder.RegisterType<BlogCacheAOP>();//可以直接替换其他拦截器
291290
builder.RegisterType<BlogLogAOP>();//这样可以注入第二个
292291

293-
//var assemblysServices1 = Assembly.Load("Blog.Core.Services");
294-
295-
296292
// ※※★※※ 如果你是第一次下载项目,请先F6编译,然后再F5执行,※※★※※
297-
// ※※★※※ 因为解耦,bin文件夹没有以下两个dll文件,会报错,所以先编译生成这两个dll ※※★※※
298-
299293

294+
#region Service.dll 注入,有对应接口
300295
//获取项目绝对路径,请注意,这个是实现类的dll文件,不是接口 IService.dll ,注入容器当然是Activatore
301296
var servicesDllFile = Path.Combine(basePath, "Blog.Core.Services.dll");
302297
var assemblysServices = Assembly.LoadFile(servicesDllFile);//直接采用加载文件的方法
@@ -308,11 +303,34 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
308303
.InstancePerLifetimeScope()
309304
.EnableInterfaceInterceptors()//引用Autofac.Extras.DynamicProxy;
310305
// 如果你想注入两个,就这么写 InterceptedBy(typeof(BlogCacheAOP), typeof(BlogLogAOP));
311-
.InterceptedBy(typeof(BlogCacheAOP));//允许将拦截器服务的列表分配给注册。
306+
.InterceptedBy(typeof(BlogCacheAOP));//允许将拦截器服务的列表分配给注册。
307+
#endregion
312308

309+
#region Repository.dll 注入,有对应接口
313310
var repositoryDllFile = Path.Combine(basePath, "Blog.Core.Repository.dll");
314311
var assemblysRepository = Assembly.LoadFile(repositoryDllFile);
315312
builder.RegisterAssemblyTypes(assemblysRepository).AsImplementedInterfaces();
313+
#endregion
314+
315+
#region 其他注入
316+
317+
#region 没有接口的 dll 层注入
318+
319+
////因为没有接口层,所以不能实现解耦,只能用 Load 方法。
320+
////var assemblysServicesNoInterfaces = Assembly.Load("Blog.Core.Services");
321+
////builder.RegisterAssemblyTypes(assemblysServicesNoInterfaces);
322+
323+
#endregion
324+
325+
#region 没有接口的单独类 class 注入
326+
////只能注入该类中的虚方法
327+
builder.RegisterAssemblyTypes(Assembly.GetAssembly(typeof(Love)))
328+
.EnableClassInterceptors()
329+
.InterceptedBy(typeof(BlogLogAOP));
330+
331+
#endregion
332+
333+
#endregion
316334

317335
//将services填充到Autofac容器生成器中
318336
builder.Populate(services);

Blog.Core/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//Redis缓存
1111
"RedisCaching": {
1212
"Enabled": true,
13-
"ConnectionString": "127.0.0.1:6379"
13+
"ConnectionString": "127.0.0.1:6319"
1414
},
1515
//数据库配置
1616
"SqlServer": {

0 commit comments

Comments
 (0)