mardi 9 juillet 2019

Is there a way to implement State design pattern for view element in Asp.net MVC?

I'm still fairly new to .Net MVC, and I'm currently developping an app which at some point needs to draw some charts. These charts can be of different types (pie, bar, line, etc...). These charts are defined by a meta class, instances of which are stored in my database (name, data source url [the actual numerical infos come from an API], type). Currently the type is stored as a string tag and a switch in my view determines which draw fonction to call (drawPieChart, drawBarChart, etc...).

This reminds me a lot of what State Design Pattern is supposed to simplify (in this case, the "state" would be the chart type). So I started implementing my classes in my ViewModel, but I got stuck when it came to the actual draw() function :

    class PieChart : IChartType
    {
        public void draw(Chart ctx)
        {
            // how to draw stuff in the view ?
        }
    }

    public class Chart
    {
        private IChartType chartType { get; set; }
        public void draw()
        {
            this.chartType.draw(this);
        }
    }

    public interface IChartType{
        void draw(Chart ctx);
    }


I'm using KendoUI for Jquery in the view to draw stuff currently.

So my question is, is there a way to implement the draw() function ? (I believe I'm not supposed to implement anything in the model, right ?)

Thank you for your help !

Aucun commentaire:

Enregistrer un commentaire