混合后如何将控制器与模型分开?

发布于 2024-12-11 06:59:53 字数 1642 浏览 0 评论 0原文

我不确定这是否完全是 MVC 模式,但我想做的是将所有数据层(简而言之,就是将序列化为 XML 文件的所有内容)与其操作分开。仅仅为了实现 MVC 而实现 MVC 确实不是我的目标。因此,如果这不完全是 MVC,我也可以接受。

比如说,我有这个类(这些只是试图说明我的问题的示例类,而不是我的实际类):

public class MixedSubClass
{
    public string SomeData {get;set;}

    public void DoSomeActionWhichRequiresControls(Control someControl)
    {
        // do stuff            
    }
}

public class MixedClass
{
    private Control _SomeControl;

    public List<MixedSubClass> _SubClasses = new List<MixedSubClass>();
    public List<MixedSubClass> SubClasses { get { return _SubClasses; } }

    public MixedClass(Control someControl)
    {
        _SomeControl = someControl;
    }

    public void DoSomeMoreActionsWhichRequiresControls()
    {
        foreach (var subClass in SubClasses)
        {
            subClass.DoSomeActionWhichRequiresControls(_SomeControl);
        }
        // Do more stuff
    }
}

因此,当我序列化 MixedClass 时,只有公共字段被序列化,这就是我想。但现在我想要一个唯一的数据层,它甚至不需要 Windows.Forms 程序集。我想坚持这一点:

public class MixedSubClass
{
    public string SomeData {get;set;}
}

public class MixedClass
{
    public List<MixedSubClass> _SubClasses = new List<MixedSubClass>();
    public List<MixedSubClass> SubClasses { get { return _SubClasses; } }
}

并将所有这些放入一个独立的程序集中。但我现在的问题是如何把这件事转回到之前的事情。我首先想到的是使用扩展方法,但是,正如您在我的示例代码中看到的,有时我需要存储一些不可序列化的值,例如 Control _SomeControl。另外,在现实生活中,这些列表是列表、树和类似的更复杂的东西的列表,所以在开始之前我需要一些良好的基础(实际上我已经有一个项目,其中包含我的所有数据层本身,它可以很好地编译而无需 < code>Windows.Forms,但现在我无法将其重新组合在一起)。

你会如何处理这个问题?我不应该以这种方式分离数据层吗?

I'm not sure if this is exactly a MVC pattern, but what I'm trying to do is separate all the data layer (which in a few words is all that would be serialized to an XML file), from its actions. Implementing MVC just for the sake of it is really not my aim here. So if this is not exactly MVC, I'm fine with that.

Say, for instance, I have this classes (these are just sample classes that try to illustrate my problem, not my actual classes):

public class MixedSubClass
{
    public string SomeData {get;set;}

    public void DoSomeActionWhichRequiresControls(Control someControl)
    {
        // do stuff            
    }
}

public class MixedClass
{
    private Control _SomeControl;

    public List<MixedSubClass> _SubClasses = new List<MixedSubClass>();
    public List<MixedSubClass> SubClasses { get { return _SubClasses; } }

    public MixedClass(Control someControl)
    {
        _SomeControl = someControl;
    }

    public void DoSomeMoreActionsWhichRequiresControls()
    {
        foreach (var subClass in SubClasses)
        {
            subClass.DoSomeActionWhichRequiresControls(_SomeControl);
        }
        // Do more stuff
    }
}

So when I serialize MixedClass, only public fields get serialized, which is what I want. But now I want to have an only data layer, which doesn't even require the Windows.Forms assembly. I want to stay with this:

public class MixedSubClass
{
    public string SomeData {get;set;}
}

public class MixedClass
{
    public List<MixedSubClass> _SubClasses = new List<MixedSubClass>();
    public List<MixedSubClass> SubClasses { get { return _SubClasses; } }
}

And put all this into an independent assembly. But my problem now is how to turn this back to the previous thing. First thing I thought was using extension methods, but, as you can see in my sample code, sometimes I need to store some value that is not serializable, such as the Control _SomeControl. Also, on real life, these lists are lists of lists, trees and more complicated stuff like that so I need a some good foundation before I get started (actually I already have a project with all my data layer by itself which compiles fine without Windows.Forms, but now I'm having trouble putting it back together).

How would you handle this? Should I just not separate the data layer this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软甜啾 2024-12-18 06:59:53

我认为到目前为止你做得很好。你的数据层绝对不应该引用 UI 控件(无论是 winform、web 等)。

虽然很难根据您发布的内容提供准确的代码示例,但您需要添加一组类似控制器的类,将数据(也称为模型)映射到用户界面(又称为视图)。它们应该保存对数据和 UI 对象的引用,并且是执行您删除的工作的对象 (DoSomeActionWhichRequiresControls)。

I think you are doing fine so far. Your data layer should absolutley be free of references to UI controls (whether they be winform, web, etc).

While it is hard to give an exact code sample based on what you've posted, you need to add a set of controller-like classes that map your data (aka model) onto your user-interface (aka view). They should hold a reference to both the data and UI objects and be the ones to perform th work that you stripped out (DoSomeActionWhichRequiresControls).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文