将多边形对象从WPF应用程序存储到数据库?

时间:2022-02-22 16:58:05

I'm currently working on a WPF-application (C#, using the Prism framework) that will contain a drawing pane where a user can draw polygons using his mouse.

我目前正在开发一个WPF应用程序(C#,使用Prism框架),它将包含一个绘图窗格,用户可以使用鼠标绘制多边形。

But I'm wondering how I can make these drawings persistent.

但我想知道如何使这些图纸持久。

I'm guessing that the best object to contain those drawings are the Polygon object?

我猜测包含这些图纸的最佳对象是Polygon对象?

I would also like to have those Polygons have certain attributes, such as a color etc. (if need be I can do this by defining my own Polygon-inherited object).

我还希望这些多边形具有某些属性,例如颜色等(如果需要,我可以通过定义我自己的多边形继承对象来实现)。

But I'm especially wondering how I can make this kind of information persistent in a database? (I'm using SQLite at the moment).

但我特别想知道如何在数据库中保持这种信息的持久性? (我现在正在使用SQLite)。

I want to be able to store Polygon information, including information such as the fill-color of that Polygon etc. in a database.

我希望能够在数据库中存储Polygon信息,包括Polygon等的填充颜色等信息。

I've been googling this, but I'm not finding something useful. Could someone point me in the right direction?

我一直在谷歌搜索,但我找不到有用的东西。有人能指出我正确的方向吗?

Thanks for any help!

谢谢你的帮助!

1 个解决方案

#1


0  

You have to create model classes in order to store the necessary information, which you would like to store.

您必须创建模型类才能存储您想要存储的必要信息。

I think it might be the best solution to use Entity Framework, so you wouldn't have to bother with creating the model classes, you could use the existing ones. But, in your case, it might saves a lot of unnecessary information into your tables.

我认为它可能是使用Entity Framework的最佳解决方案,因此您不必费心去创建模型类,您可以使用现有的模型类。但是,在您的情况下,它可能会将大量不必要的信息保存到表中。


If you are creating your own model classes, you can add a helper Extension Method to .net types, such as Polygon. That method would generate a storable model class from the .net class, which can be saved to database.

如果要创建自己的模型类,可以将辅助扩展方法添加到.net类型,例如Polygon。该方法将从.net类生成一个可存储的模型类,该类可以保存到数据库中。

For example:
Model class:

例如:模型类:

public class PolygonDbFormat  // the class which contains the information you want to store
{
    public string Name { get; set; }
    public Brush Fill { get; set; }
}

Defining the extension method:

定义扩展方法:

public static class PolygonEx
{
    public static PolygonDbFormat AsDbFormat(this Polygon polygon)
    {
        return new PolygonDbFormat
        {
            Name = polygon.Name,
            Fill = polygon.Fill
        };
    }
}

Then you can save the Polygon class to the database this way:

然后,您可以通过以下方式将Polygon类保存到数据库:

var dbFormat = yourPolygon.AsDbFormat();
_yourDbManager.AddEntry(dbFormat); // your implementation to store the data in database

#1


0  

You have to create model classes in order to store the necessary information, which you would like to store.

您必须创建模型类才能存储您想要存储的必要信息。

I think it might be the best solution to use Entity Framework, so you wouldn't have to bother with creating the model classes, you could use the existing ones. But, in your case, it might saves a lot of unnecessary information into your tables.

我认为它可能是使用Entity Framework的最佳解决方案,因此您不必费心去创建模型类,您可以使用现有的模型类。但是,在您的情况下,它可能会将大量不必要的信息保存到表中。


If you are creating your own model classes, you can add a helper Extension Method to .net types, such as Polygon. That method would generate a storable model class from the .net class, which can be saved to database.

如果要创建自己的模型类,可以将辅助扩展方法添加到.net类型,例如Polygon。该方法将从.net类生成一个可存储的模型类,该类可以保存到数据库中。

For example:
Model class:

例如:模型类:

public class PolygonDbFormat  // the class which contains the information you want to store
{
    public string Name { get; set; }
    public Brush Fill { get; set; }
}

Defining the extension method:

定义扩展方法:

public static class PolygonEx
{
    public static PolygonDbFormat AsDbFormat(this Polygon polygon)
    {
        return new PolygonDbFormat
        {
            Name = polygon.Name,
            Fill = polygon.Fill
        };
    }
}

Then you can save the Polygon class to the database this way:

然后,您可以通过以下方式将Polygon类保存到数据库:

var dbFormat = yourPolygon.AsDbFormat();
_yourDbManager.AddEntry(dbFormat); // your implementation to store the data in database