8000 [建议] 请将单例改为对象 · Issue #84 · Mirai-NET-Shelter/Mirai.Net · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[建议] 请将单例改为对象 #84

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bunnyi116 opened this issue Oct 6, 2023 · 3 comments
Open

[建议] 请将单例改为对象 #84

bunnyi116 opened this issue Oct 6, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@bunnyi116
Copy link
bunnyi116 commented Oct 6, 2023

https://github.com/SinoAHpx/Mirai.Net/blob/35814dc6d3ef69d952f96568c5ba4c2a0860b4b6/Mirai.Net/Sessions/MiraiBot.cs#L46

在Http下,该项目使用了大量的MiraiBot单例进行HTTP请求,对于多个MiraiBot对象没办法单独使用,只能使用最新的一个Bot实例。

大部分在 Mirai.Net.Sessions.Http.Managers 、Mirai.Net.Utils.Internal.MiraiHttpUtils 下

拿MiraiHttpUtils中的一个举例

https://github.com/SinoAHpx/Mirai.Net/blob/35814dc6d3ef69d952f96568c5ba4c2a0860b4b6/Mirai.Net/Utils/Internal/MiraiHttpUtils.cs#L52-L64

该段引用了静态单例实例,MiraiBot.Instance.HttpSessionKey,从而导致无法对不同的MiraiBot对象进行操作。

所以我觉得应该改为以下格式,应该传入MiraiBot实例,改为可以单独使用的静态方法,然后添加this关键字弄成扩展方法,这样Mirai对象可以直接点出该方法进行使用

    internal static async Task<string> GetAsync(this MiraiBot bot, string url, bool withSessionKey = true)
    {
        var result = withSessionKey
            ? await url
                .WithHeader("Authorization", $"session {bot.HttpSessionKey}")
                .GetAsync()
            : await url.GetAsync();

        var response = await result.GetStringAsync();
        bot.EnsureSuccess(response, $"url={url}");

        return response;
    }

这只是一个建议,我是无所谓的,只是觉得不合理。静态拓展方法不应该使用单例Bot,应该使用参数进行传参,否则就没必要封装一个静态拓展方法。

@bunnyi116 bunnyi116 added the enhancement New feature or request label Oct 6, 2023
@bunnyi116
Copy link
Author
bunnyi116 commented Oct 6, 2023

如果设计之初只是为单例,那把构造器屏蔽掉,使用单例MiraiBot获取。

能 new 多个 MiraiBot 实例,但静态拓展方法却无法支持emm...

@bunnyi116
Copy link
Author
bunnyi116 commented Oct 6, 2023

@ksharperd
Copy link
Collaborator

ur right, we can consider create some factory method to help create a MiraiBot instance and make MiraiBot's ctor private.
maybe i will take into it after finish #89.

keep it open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants
0