使用 PowerShell 的存储库模式
这是我关心的问题,
我的问题并不是真正的 PowerShell,而是我必须使用它来实现持久性的方式。我经常遇到有超过 4 个命令来执行逻辑或应用持久性的情况。
例如:
配置 Dns 区域时,我得到正常的 New/Get/Set/Remove-DnsZone 和 Suspend/Unsuspend-DnsZone。我的队友目前处理它的方式是,他们在存储库上创建了一个额外的方法来处理额外的命令。
在我看来,行为应该在对象上,并且在存储库上执行 Create 或 Update 时应该调用 Suspend/Unsuspend-DnsZone 。
你怎么认为?
编辑 根据要求,我将添加一些代码来演示我正在谈论的内容。
这是一个假示例,其 Powershell 包装器的实现几乎相似。 这是我的队友在存储库上执行的方法:
public void Suspend(DnsZone zone)
{
_powerShell.Execute("Suspend-Zone", new Dictionary<string, object>{{"Name", zone.Name}});
}
因此,在您的逻辑中的某个地方,要暂停您必须调用的区域
var myZone = new DnsZone{ Name= "xyz"};
_zones.Suspend(myZone);
,相反,我倾向于 DnsZone 应该有一个属性或方法。因此逻辑不在存储库中,而是在对象本身中。即使存储库必须将其转换为挂起区域调用。
var myZone = _zones.FindByName("nyname");
myZone.Suspend();
_zones.Save(myZone);
哈
Here is my concern,
My problem is not really with PowerShell, but the way I have to use it to implement the persistence. I often get cases where there is more than 4 commands to do the logic or to apply persistence.
For example:
When configuring a Dns Zone, I get the normal New/Get/Set/Remove-DnsZone and Suspend/Unsuspend-DnsZone. How my teammates are currently handling it is that they make an extra method on the repository to handle the extra commands.
It seems to me like the behavior should instead be on the object and that Suspend/Unsuspend-DnsZone should be called when Create or Update is executed on the repo.
What do you think?
EDIT
As requested, I'll put some code to demonstrate what I am talking about.
This is a fake example with an almost similar implementation of our Powershell wrapper.
This is a method on the repository that my teammates do:
public void Suspend(DnsZone zone)
{
_powerShell.Execute("Suspend-Zone", new Dictionary<string, object>{{"Name", zone.Name}});
}
So somewhere in your logic, to suspend a zone you have to call
var myZone = new DnsZone{ Name= "xyz"};
_zones.Suspend(myZone);
Instead, what I prone is that DnsZone should have a property or a method. So that the logic is not in the repo but in the object itself. Even though the repo has to translate that into a Suspend-Zone call.
var myZone = _zones.FindByName("nyname");
myZone.Suspend();
_zones.Save(myZone);
hth
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样做似乎会破坏许多管道功能,而正是这些功能使 Powershell 可以轻松地从命令行使用。如果您消除了 Suspend-DNSZone cmdlet 以支持对象方法,则您无法执行此操作,
您必须执行此操作
,恕我直言,这不是改进。
It seems like doing that would break a lot of the pipelining functionality that makes Powershell so easy to use from the comnmand line. If you eliminated the Suspend-DNSZone cmdlet in favor of an object method, you couldn't do
You'd have to do
Not an improvement, IMHO.