【问题标题】:Clean ways to to read multiple httpContext calls in web forms class在 Web 表单类中读取多个 httpContext 调用的简洁方法
【发布时间】:2019-10-26 08:04:48
【问题描述】:

我想知道其他人是否可以选择将以下内容写为“清洁代码”

另外...这是用 VB 编写的,但我很高兴收到 C# 中的建议

(我只是在记事本中写的...可能无法编译,但这是我质疑的原理)

public class MyTest

    public sub new()
    end sub

    public sub SaveSomething(message as string)
        save($"{GetSessionId} == {GetIdentityName} == {message}")   
        if configurationmanager.appsettings("allowSomething") then
            doSomethingElse()
        end if
    end function

    private function GetSessionId as string
        if httpcontext.current?.session?.sessionID isNot Nothing then
            return httpcontext.current.session.sessionID
        else
            return ""
        end if
    end function

    private function GetIdentityName as string
        if httpcontext.current?.user?.identity?.name isNot Nothing then
            return httpcontext.current.user.identity.name
        else
            return ""
        end if
    end function

    private sub save(message)
        dim filePath as string = configurationmanager.appsettings("filePath")
        'some code here to save
    end sub

    private sub doSomethingElse()
        'some code here
    end sub
end class

现在,我想取出所有“httpContext”和“ConfigurationManager”引用(更干净,因此我可以进行单元测试等...)

我考虑过的一种方法是为这四种情况创建提供程序类,例如

_SessionIdProvider as ISessionIdProvider
_IdentityNameProvider as INameProvider
_FilePathProvider as IPathProvider
_AppSettingsAllowSomethingProvider as IAllowSomethingProvider

并注入所有这些!我现在觉得很乱

我已经考虑过一个辅助类,所以要注入一个对象,我可以使用它来获取这些值。但是解决方案中的其他地方可能需要 sessionID,例如,其他不在此辅助类中的值,所以要么有重复的代码,要么有一个庞大的帮助类

有什么想法吗?像这样注入太多依赖会不会很乱?

【问题讨论】:

    标签: c# vb.net dependency-injection inversion-of-control clean-architecture


    【解决方案1】:

    我不认为四个依赖太多,但它可能处于边缘......我建议你遵循Dependency Inversion Principle并让客户端类(MyTest)'设计'它的接口需要。

    由于所讨论的方法都没有任何参数,但是,您可以将所有依赖关系减少到 primitive dependencies

    在 C# 中可能如下所示:

    public class MyTest
    {
        public MyTest(string sessionId, string identityName, bool allowSomething, string filePath)
        {
            SessionId = sessionId;
            IdentityName = identityName;
            AllowSomething = allowSomething;
            FilePath = filePath;
        }
    
        public string SessionId { get; }
        public string IdentityName { get; }
        public bool AllowSomething { get; }
        public string FilePath { get; }
    
        public void SaveSomething(string message)
        {
            Save($"{SessionId} == {IdentityName} == {message}");
            if (allowSomething))
                DoSomethingElse();
        }
    
        private void Save(message)
        {
            var filePath = FilePath;
            // some code here to save
        }
    
        private void DoSomethingElse()
        {
            // some code here
        }
    }
    

    SessionIdIdentityNameAllowSomething 这三个值看起来像是属于与身份验证和授权相关的一组值,因此也许它们更适合作为Parameter Object

    【讨论】:

      猜你喜欢
      • 2011-08-28
      • 2013-08-10
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多