使用 InSingletonScope 和 Ninject 注入 Facebook SDK 包装器是否正确?

发布于 2024-11-27 20:33:54 字数 1235 浏览 5 评论 0原文

我使用以下代码编写了 C# Facebook SDK 的包装器:

public interface IFacebookService
{
    Uri GetLoginUri(string returnUrl);

    FacebookResult OAuth(string url, string code);

    void Post(string message, string accessToken);
}

实现如下所示:

public class FacebookService : IFacebookService
{
    public Uri GetLoginUri(string returnUrl)
    {
        var oAuthClient = new FacebookOAuthClient(FacebookApplication.Current);

        // code removed for simplicity

        return uri;
    }

    public FacebookResult OAuth(string url, string code)
    {
        FacebookOAuthResult oauthResult;

        if (FacebookOAuthResult.TryParse(url, out oauthResult))
        {
            // code removed for simplicity
        }

        return result;
    }

    public void Post(string message, string accessToken)
    {
        var client = new FacebookClient(accessToken);

        // code removed for simplicity
    }
}

我使用 Ninject 将其注入到需要使用 oauth 执行登录并发布到用户 Facebook 墙的地方。

我目前使用以下配置进行了此配置:

kernel.Bind<IFacebookService>().To<FacebookService>().InSingletonScope();

使用 InSingletonScope 是否正确?由于这是第三方库,我不确定这是否是正确的方法,因为所有线程都将共享该实例。

I have written a wrapper around the C# Facebook SDK usign the following code:

public interface IFacebookService
{
    Uri GetLoginUri(string returnUrl);

    FacebookResult OAuth(string url, string code);

    void Post(string message, string accessToken);
}

The implementation looks like:

public class FacebookService : IFacebookService
{
    public Uri GetLoginUri(string returnUrl)
    {
        var oAuthClient = new FacebookOAuthClient(FacebookApplication.Current);

        // code removed for simplicity

        return uri;
    }

    public FacebookResult OAuth(string url, string code)
    {
        FacebookOAuthResult oauthResult;

        if (FacebookOAuthResult.TryParse(url, out oauthResult))
        {
            // code removed for simplicity
        }

        return result;
    }

    public void Post(string message, string accessToken)
    {
        var client = new FacebookClient(accessToken);

        // code removed for simplicity
    }
}

I use Ninject to inject this where needed to perform login using oauth and posting to the users Facebook wall.

I currently have this configured using:

kernel.Bind<IFacebookService>().To<FacebookService>().InSingletonScope();

Is it correct to use InSingletonScope? Since this is a third party library I am not sure if this is the right approach since all threads will share the instance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖果控 2024-12-04 20:33:54

我对Facebook SDK一无所知。但作为一般规则,如果实现是无状态的,那么单例是正确的范围。我认为您的实施就是这种情况。

I have no knowledge about the Facebook SDK. But as a general rule if the implementation is stateless then singleton is the correct scope. I assume this is the case with your implementation.

小女人ら 2024-12-04 20:33:54

我认为没有必要创建一个新的接口来处理 FacebookOAuthClient.GetLoginUrl 或 FacebookOAuthResult.Parse

GetLoginUrl 和 FacebookOAuthClient 的 ParseResult 已经是虚拟的。 (确保您至少使用 v5.0.35 才能获取 FacebookOAuthClient.ParseResult,它与 FacebookOAuthResult.Parse 执行相同的操作。)

然后使用 ninject 的构造函数注入为 FacebookOAuthClient 设置 IFacebookApplication。

您还可以使用构造函数注入来设置 FacebookClient 的访问令牌。 (除非你想有更简单的方法来发布到墙上等等。)
或者您也可以不使用访问令牌,并设置 FacebookClient.AccessToken = "access_token"

i don't think it is necessary to create a new interface to deal with FacebookOAuthClient.GetLoginUrl or FacebookOAuthResult.Parse

GetLoginUrl and ParseResult of FacebookOAuthClient are already virtual. (make sure you are at least on v5.0.35 to get the FacebookOAuthClient.ParseResult which does the same thing as FacebookOAuthResult.Parse.)

Then use constructor injection of ninject to set the IFacebookApplication for FacebookOAuthClient.

you could also use contructor injection to set the access token for FacebookClient. (that is unless u want to have easier methods for posting to wall and so on.)
Or you could also do without the access token, and set FacebookClient.AccessToken = "access_token"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文