数据列表的类包装器

时间:2017-09-11 14:53:03

标签: c# .net oop

我需要一些帮助来构建一个流畅的接口(类包装器),它允许我从数据库中的曲线集(在myWell中)创建(并命名)一个2D双数组。理想情况下,用户可以在执行以下语法(或类似的东西)时创建2D双数组:

 Double [,] CurveDoubleName = CreateIPmathCurves.CreateCurve(comboBox1.Text).CurveName("NPHIL").Make()

comboBox1数据来自名为frmMain的WindowsForm。这就是我到目前为止所拥有的:

namespace UserProgram
{
        public class CreateIPmathCurve : frmMain, ICanNameCurve, ICanLocateCurve, ICanMakeCurve
        {
        private string _comboBoxName;
        private string _CurveName;
        private string _data;

        // Private constructor - Only allow instantiated functions to create an IP math Curve

        private CreateIPmathCurve()
        { }

        // Instantiating functions

        public static ICanNameCurve CreateCurve()
        {
            return new CreateIPmathCurve();
        }

        // Chaining functions 
        private CreateIPmathCurve(string data) { _data = data; } //private constructor prevents instantiation of your builder

        public ICanLocateCurve SetCurveName(string CurveName)
        {
            _CurveName = CurveName;
            return this; // this allows chaining.
        }

        public ICanMakeCurve SetComboBoxName(string comboBoxName)
        {
            _comboBoxName = comboBoxName;
            return this; // this allows chaining.
        }

        // ending functions

        public DataObject CreateCurveDoublArrayObj(string comboBoxName)
        {
            try
            {

                // User will need to populate comboBox.. example: comboBox1.text
                char[] delimiter = { ':' };
                Curve1inText = comboBoxName;

                string[] crvIn1 = Curve1inText.Split(delimiter);

                string CurveSet = crvIn1[0];
                string Curve = crvIn1[1];

                ICurveSet InCurveSet = myWell.FindCurveSet(CurveSet);
                ICurve InMyCurve = myWell.FindCurve(Curve);

                if (InMyCurve == null)
                {
                    MessageBox.Show("You need to input a curve");
                }

                ILogReading[] In = InMyCurve.LogReadings.ToArray();

                double[,] CurveDouble = new double[In.Length, 2];

                int j = 0;

                foreach (ILogReading reading in InMyCurve.LogReadings)
                {
                    CurveDouble[j, 0] = reading.Depth;
                    CurveDouble[j, 1] = reading.Value;
                    j++;
                }

                return new DataObject(CurveDouble);
            }

            catch (Exception ex)
            {
                MessageBox.Show("Error building Double Array\n" + ex.Message + "\n" + ex.StackTrace);
                return null;
            }

        }

        public double[,] MakeCurve()
        {
            //this is where CurveName input ("NPHIL") should name the 2D double array listed from dataobject.
            // I receive a "non-invocable member 'DataObject' cannot be used like a method" error....

            _CurveName = DataObject(_comboBoxName);
            return this;

            // I also receive a "cannot implicity convert type UserProgram.CreateIPmathCurve' to 'double [*,*]"... 
        }
    }

    // using interfaces to enforce Fluent Interface grammar -- can't make curve if you don't know the location of curve, etc...
    public interface ICanNameCurve
    {
        ICanLocateCurve SetCurveName(string CurveName);
    }

    public interface ICanLocateCurve
    {
        ICanMakeCurve SetComboBoxName(string comboBoxName);
    }

    public interface ICanMakeCurve
    {
        DataObject CreateCurveDoublArrayObj(string comboBoxName);
    }

}

你能帮我创建一个允许用户命名数据对象的CurveName()函数吗? (我尝试了一个getter-setter访问器,但我认为我没有正确地执行它,或者缺乏对它如何工作的强烈概念性理解)

最后,您是否还可以帮我创建一个将所有这些放在一起的Make()函数?

2 个答案:

答案 0 :(得分:1)

要构建流畅的API,您需要找到一种方法来聚合所有数据,直到您调用(HTMLElement)mutation.target为止。

一种简单的方法是编写一个mutations来保存所有数据。此构建器具有每个可配置字段的setter方法。

这里的关键点是每个setter方法返回Make对象,允许你链接调用。

Builder

您现在可以使用构建器来配置曲线。

Builder

我们有一个静态public class FluentCurveBuilder { private string _name; private string _data; private string _color; private FluentCurveBuilder(string data) { _data = data; } // private constructor prevents instantiation of your builder public FluentCurveBuilder SetName(string name) { _name = name; return this; // this allows chaining. } public FluentCurveBuilder SetColor(string color) { _color = color; return this; // this allows chaining. } public static FluentCurveBuilder CreateCurve(string data) { return new FluentCurveBuilder(data); } public Curve Make() { // Creation logic goes here } } 方法,它返回一个新的构建器。所有其他方法只应将数据存储在实例字段中并返回此构建器。

var curve = FluentCurveBuilder.CreateCurve("Data") .SetName("Test") .Make(); 方法中,您现在可以访问所有以前的汇总数据,并且可以构建CreateCurve

答案 1 :(得分:0)

我认为我正在分析我的问题 - 简单的解决方案是创建一个返回2D双数组的方法,然后在调用函数时为对象指定一个名称...所以使用这个:

public double [,] CreateIPmathCurve(string comboBoxName)
    {

        string CurveInText = "";
        char[] delimiter = { ':' };
        CurveInText = comboBoxName;

        string[] crvIn = CurveInText.Split(delimiter);

        string CurveSet = crvIn[0];
        string Curve = crvIn[1];

        ICurveSet InCurveSet = myWell.FindCurveSet(CurveSet);
        ICurve InMyCurve = myWell.FindCurve(Curve);

        if (InMyCurve == null)
        {
            MessageBox.Show("You need to input a curve");
        }

        ILogReading[] In = InMyCurve.LogReadings.ToArray();

        double[,] CurveDouble = new double[In.Length, 2];

        int j = 0;

        foreach (ILogReading reading in InMyCurve.LogReadings)
        {
            CurveDouble[j, 0] = reading.Depth;
            CurveDouble[j, 1] = reading.Value;
            j++;
        }

        return CurveDouble;

允许我用这个创建数组:

double[,] NPHIL = CreateIPmathCurve(comboBox1.Text);

感谢所有帮助我做这项努力的人!