如何在业务层中解耦流程

时间:2011-08-03 09:40:09

标签: business-rules

我遇到的问题是,对于某些业务流程,调用业务对象和方法的顺序可能会经常更改。所以我提出了类似下面的内容:(抱歉我不能发布图像......,我试着在下面的文字中表达它们)

Business Objects: Object1,Object2

方法: M1,M2,M3,M4

处理: P1(M1> M2> M3),P2(M2> M3>如果M3返回真,则M4否则结束)

在这种情况下,我使用的是.NET 3.5。我创建了一些代表进程的类,其中包含我提到的那些序列。有用。但问题是每次进程发生变化时我都需要编译。如果我可以通过某种XML来配置它会好得多。

我听说过jBPM for Java,Workflow Foundation for .NET但不确定它们是否符合我的需求,或者它们是否有点过分。我甚至没有在谷歌搜索什么关键词。任何人都可以建议我应该用什么技术来解决这个问题?或者只是指向一些网站或书籍?提前谢谢。

1 个答案:

答案 0 :(得分:0)

解耦软件层的常用方法是使用Dependency Inversion Principle所述的接口。在您的情况下,您可以使用接口抽象流程概念,并在该接口的实现中实现逻辑。 当您需要更改流程的逻辑时,您可以创建该接口的新实现。您可以使用任何IoC框架来注入您想要使用的实现

下面的

只是一种简单的方法:

 public interface IMethod
    {
        void M1();
        string M2();
        void M3();
        void M4();
    }

    public interface IProcess
    {
        IMethod Method { get; set; }
        void P1();
        void P2();
    }

    public class Process : IProcess
    {
        public IMethod Method
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public void P1()
        {
            Method.M1();
            Method.M2();
        }

        public void P2()
        {
            if(Method.M2()==string.Empty)
            {
                Method.M3();
            }
        }
    }

    public class AnotherProcess : IProcess
    {
        public IMethod Method
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public void P1()
        {
         Method.M4();
        }

        public void P2()
        {
            Method.M2();
            Method.M4();
        }
    }

    public class UseProcess
    {
        private IProcess _process;

        //you can inject the process dependency if you need use a different implementation

        public UseProcess(IProcess process)
        {
            _process = process;
        }

        public void DoSomething()
        {
            _process.P1();
        }
    }
相关问题