从Caller方法控制服务流

时间:2018-03-23 16:25:04

标签: c# .net design-patterns coding-style

从此服务的调用方控制服务的工作方式是否正确?我今天遇到这样的问题,不知道我的解决方案是否合适。希望更高级的人可以给我一个反馈。这是我的问题:

我有方法Foo的服务:

public class Service
{
    public int? Foo(int? number, bool throwException)
    {
        int? result = number;

        if (result == null)
        {
              var result = ...
              if (result == null && throwException)
              {
                    throw new Exception("ZZZZ");
              }
        }

        return result
    }
}

现在在Caller类中,我有2个方法可以调用此服务:

public class Caller
{
    public void Test1()
    {
        ...         
        Service.Foo(number, TRUE);
        ...
    }

    public void Test2()
    {
        ...         
        Service.Foo(number, FALSE);
        ...
    }

正如您所看到的,对于Test1我发送标志throwException为TRUE,而对于Test2为FALSE,控制服务流如何从调用者的方法进行操作是不错的做法?

这就是我试图避免的:

public class Caller
{
    public void Test1()
    {
        var a = Service.Foo1();
        if (a == something)
        {
              throw ARgumentException("ZZZ");
        }

        var b = Service.Foo2();
        if (b == something2)
        {
             throw ArgumentException("ZZZ2");
        }

        ...
    }
}

0 个答案:

没有答案