Best Practice for Writing a POS System

后端 未结 4 897
耶瑟儿~
耶瑟儿~ 2021-02-13 11:49

I\'m putting together a basic Point of Sale (POS) system in C# that needs to print to a receipt printer and open a cash drawer. Do I have to use the Microsoft Point of Service S

4条回答
  •  醉酒成梦
    2021-02-13 11:53

    This is probably a slightly different answer to what you were looking for(!)...

    When working with "external interfaces" (e.g. printers, cash draws etc) always abstract things. You probably want to implement strategies - Pattern Strategy.

    You make an interface for the cash draw:

    public interface ICashDrawer
    {
        void Open();
    }
    

    The provide implementations:

    • one strategy is a class that uses COM to open the draw
    • another is something as simple as a class that does a Debug.WriteLine call so you don't need a cash draw connected to your PC during development

    e.g.

    public class ComPortCashDrawer : ICashDrawer
    {
        public void Open()
        {
            // open via COM port etc
        }
    }
    
    public class DebugWriterCashDrawer : ICashDrawer
    {
        public void Open()
        {
            Debug.WriteLine("DebugWriterCashDrawer.Open() @ " + DateTime.Now);
        }
    }
    

    Again for printing you have a print interface that takes the data:

    public interface IRecieptPrinter
    {
        bool Print(object someData);
    }
    

    then you make one or more implementations.

    • Basic printer
    • Specialized label printer
    • a text based one that saves to a file...

    e.g.

    public class BasicRecieptPrinter : IRecieptPrinter
    {
        public bool Print(object someData)
        {
            // format for a basic A4 print
            return true; // e.g. success etc
        }
    }
    
    public class SpecificXyzRecieptPrinter : IRecieptPrinter
    {
        public bool Print(object someData)
        {
            // format for a specific printer
            return true; // e.g. success etc
        }
    }
    
    public class PlainTextFileRecieptPrinter : IRecieptPrinter
    {
        public bool Print(object someData)
        {
            // Render the data as plain old text or something and save 
             // to a file for development or testing.
            return true; // e.g. success etc
        }
    }
    

    With respect to the SDK, if down the track you find you need it for some reason you write implementations using the SDK. Over time you may end up with several ways to interface with different external devices. The client may get a new cash draw one day etc etc.

    Is that clear, I can flesh out what I mean if you want but you probably get my drift.

    Your app sets itself up with the respective implementations at startup, you may want to take a look at Dependency injection as well and you will find things easier if you use a container to resolve the types.

    var printer = container.Resolve();
    

    PK :-)

提交回复
热议问题