【发布时间】: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