开发者

How to decouple process in business layer

开发者 https://www.devze.com 2023-03-25 02:41 出处:网络
I am facing a problem that, for some business processes the sequence of invoking business objects and methods may change frequently. So I came up with something similar to the below:(Sorry somehow I c

I am facing a problem that, for some business processes the sequence of invoking business objects and methods may change frequently. So I came up with something similar to the below:(Sorry somehow I can't post image..., I tried to express them in the below text)

Business Objects: Object1, Object2

Methods: M1, M2, M3, M4

Processes: P1 (M1 > M2 > M3), P2 (M2 > M3 > if M3 return true then M4 else end)

In this case I am using .NET 3.5. I create some classes to represent processes, which contains those sequences I mentioned. It works. But the problem is I need to compile every time when process changed. It would be much better if I could configure it by some sort of XML.

I have heard about jBPM for Java, Workflow Foundation for .NET but not sure if they fit my needs, or would they be overkill. I even don't what keyword to search in Google. Could anyone advice wh开发者_如何学Pythonat technology I should use to solve this issue? Or just point me to some websites or books? Thanks in advance.


A common way to decouple software layers is by using interfaces as stated by Dependency Inversion Principle. In you case you could abstract the process concept using an interface and implement the logic in the implementation of that interface. when you need change the logic of the process you can create a new implementation of that interface. You can use any IoC framework to inject what implementation you want to use

below is showed just a simple way to do that:

 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();
        }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消