如何在 C# 和 Visual Studio 中为 Mac 实现数据模型?

How to Implement a Data Model in C# and Visual Studio for Mac?

我来自 Xcode 的世界,那里不需要使用 SQLite,因为它们提供了 CoreData 以便更容易处理实体和属性。因此,在我尝试创建数据模型而不使用 GUI 时,我迷路了。

下图是我想做的事情:

这是我迷路的代码。

using System;
using SQLite;

namespace NameAnimalPlants
{
    public class DataModel
    {
        #region Computed Propoperties

        // I want this separated 
        public string ANameTextField { get; set; } = "";
        public string BNameTextField { get; set; } = "";
        public string CNameTextField { get; set; } = "";

        // I want this separated
        public string AAnimalTextField { get; set; } = "";
        public string BAnimalTextField { get; set; } = "";
        public string CAnimalTextField { get; set; } = "";

        // I want this separated
        public string APlantTextField { get; set; } = "";
        public string BPlantTextField { get; set; } = "";
        public string CPlantTextField { get; set; } = "";

        public SQLiteConnection Conn { get; set; }
        #endregion

        #region Constructors
        public DataModel()
        {
        }

        // How should I implement this method?
        public DataModel (string firstRow, string secondRow, string thirdRow)
        {
            this.ANameTextField = firstRow;
            this.BNameTextField = secondRow;
            this.CNameTextField = thirdRow;
        }
        #endregion
    }
}

如何在我的数据模型中分离每个实体?我应该如何正确实施每种方法?非常感谢与该示例相关的任何帮助或教程。

根据您问题的上下文,您似乎正在编写 Xamarin 移动应用程序,因此我将在该假设下回答。此外,从您的问题中不清楚您到底想对数据做什么,或者您的数据实体应该如何相互关联,但我会尝试提供一些可能有助于为您指明方向的指示。

我要做的第一件事是将数据模型本身与存储该数据的东西分开。因此,您将有两个 类 - 保存数据的 DataModel 和负责对数据库进行 CRUD 操作的 DataService。将它们分开后可能看起来像这样。

这是数据模型:

using System;

namespace NameAnimalPlants
{
    public class DataModel
    {
        public string ANameTextField { get; set; };
        public string BNameTextField { get; set; };
        public string CNameTextField { get; set; };

        public string AAnimalTextField { get; set; };
        public string BAnimalTextField { get; set; };
        public string CAnimalTextField { get; set; };

        public string APlantTextField { get; set; };
        public string BPlantTextField { get; set; };
        public string CPlantTextField { get; set; };

        public DataModel (string aNameTextField, string bNameTextField, string 
cNameTextField, string aAnimalTextField, string bAnimalTextField, string cAnimalTextField, string aPlantTextField, string bPlantTextField, string cPlantTextField)
        {
            this.ANameTextField = aNameTextField;
            this.BNameTextField = bNameTextField;
            this.CNameTextField = cNameTextField;
            this.AAnimalTextField = aAnimalTextField;
            this.BAnimalTextField = bAnimalTextField;
            this.CAnimalTextField = cAnimalTextField;
            this.APlantTextField = aPlantTextField;
            this.BPlantTextField = bPlantTextField;
            this.CPlantTextField = cPlantTextField;
        }
    }
}

这是数据服务:

using System;
using SQLite;

namespace NameAnimalPlants
{
    private SQLiteConnection _conn;

    public class DataService
    {
        public DataService (SQLiteConnection _conn)
        {
            _conn = conn;
        }

        public DataModel GetByNameText(string nameText)
        {
            //This is just an example of the type of function you would put in your data service.  You can add whatever functions you need to support the types of queries you need. 

            //Add some code here to query SQLite tables by nameText
        }

        public bool Save(DataModel dataModelObject)
        {
            //This is just an example of the type of function you would put in your data service.  You can add whatever functions you need to support saving a data model object to your database.  You may want a single Save method that knows how to determine if it is a UPDATE or an INSERT, or you may want to separate that out into two functions. 

            //Add some code here to update/insert the dataModelObject to the SQLite tables, and then return a bool (or some other sort of result object) indicating the success of that save.
        }

        public bool Delete(DataModel dataModelObject)
        {
            //Add some code here to delete the dataModelObject from the SQLite tables, and then return a bool (or some other sort of result object) indicating the success of that delete.
        }
    }
}

下面是您在应用中使用它们的方式,例如保存对象:

DataModel myDataModelObject = new DataModel("Some aNameTextField text", "Some bNameTextField text", "Some cNameTextField text", "Some aAnimalTextField text", "Some bAnimalTextField text", "Some cAnimalTextField text", "Some aPlantTextField text", "Some bPlantTextField text", "Some cPlantTextField text");

DataService myDataService = new DataService(new SQLiteConnection("some connection string"));

myDataService.Save(myDataModelObject);

如果您发现自己不喜欢使用 SQLite,则可能需要考虑使用 NoSQL 选项将数据存储在移动设备上,例如 Realm。它非常容易使用,您可以简单地按原样存储您的 DataModel 对象。如果你这样做,那么你所要做的就是将 Realm Nuget 包安装到你的移动项目中。您需要为每个 Xamarin.Android、Xamarin.iOS 和 PCL 项目执行此操作(如果您使用的是 Xamarin.Forms)。然后你所要做的就是让你的 DataModel 像这样从 RealmObject 派生......

namespace NameAnimalPlants
{
    public class DataModel : RealmObject
    {
        //Everything else in this class - properties, constructors, etc. - stays exactly the same as before.
    }
}

然后在您的 DataService 中删除 SQLite 引用并添加一个 Realm 引用,您的 Save 方法看起来就像这样简单:

public bool Save(DataModel dataModelObject)
{
    try
    {
        Realm db = Realm.GetInstance();
        db.Write(() =>
        {
            db.Add(newEntry);
        });
    }
    catch (Exception ex)
    {
        return false;
    }        
    return true;
}

我建议您在 SQLite 道路上走得太远之前至少先了解一下 Realm。您可以在此处获取更多信息:

Realm Mobile Database

您的核心数据模型的每个实体:

成为 C# class:

public class Animal
{
    public string aAnimal { get; set; }
    public string bAnimal { get; set; }
    public string cAnimal { get; set; }
}

How should I implement this method?

您可以添加专门的构造函数来初始化您的对象:

public class Animal
{
    public Animal() { }

    public Animal(string aAnimal, string bAnimal, string cAnimal)
    {
        this.aAnimal = aAnimal;
        this.bAnimal = bAnimal;
        this.cAnimal = cAnimal;
    }

    public string aAnimal { get; set; }
    public string bAnimal { get; set; }
    public string cAnimal { get; set; }
}

用法:

var conn = new SQLite.SQLiteConnection("coredata.sqlite");
conn.CreateTable<Animal>();
var aNewAnimal = new Animal("Stack", "Over", "Flow");
conn.Insert(aNewAnimal);

var retrivedAnimal = conn.Table<Animal>().First();