混合后如何将控制器与模型分开?
我不确定这是否完全是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为到目前为止你做得很好。你的数据层绝对不应该引用 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
).