How should I model my code to maximize code re-use in this specific situation?

后端 未结 7 1599
清歌不尽
清歌不尽 2020-12-11 02:26

Updated: See end of question for how I implemented the solution.

Sorry for the poorly-worded question, but I wasn\'t sure how best to ask it. I\'m not sure

7条回答
  •  失恋的感觉
    2020-12-11 03:13

    I would say use Dependecy Injection. Basically, you pass an abstraction of the send method.

    Something like:

    interface IVendorMessageSender
    {
        void SendMessage(Vendor v);
    }
    
    public class OutboxManager 
    {
        IVendorMessageSender _sender;
    
        public  OutboxManager(IVendorMessageSender sender)
        {
            this._sender = sender; //Use it in other methods to call the concrete implementation
        }
    
        ...
    }
    

    Another approach, as already mentioned, inheritance.

    In either case: try to remove DB retrieval code from this class. Use another abstraction for that (ie: passing an IDataProvider interface or something like that to the constructor). It will make your code more testable.

提交回复
热议问题