首先,请告诉我问题不清楚的地方,因为我不习惯在网上提问
Problem in a Nutshell:
The framework makes it very easy to create editors for a viewmodel with simple scalar properties. However it does not scale well when we want to edit viewmodels that have lists of other objects. The framework should not assume the availability of a session neither javascript.
Code Sample
I posted all the meaningful code here, which should give much better context than describing the problem with words. I kept it as simple as possible:
namespace Simple{
class SimpleController{
public Html CreateA(){
return View(new A());
}
// this is usually an http post responding to a button under the displayed listing of Bs in the main form
[HttpPost]
public Html CreateA(A a, FormCollection collection){
// *** This is only necessary in the case of lists ***
// *** what happens in the "MoreInvolved" case is
// terrifying. I didn t post any attempt. ***
if(collection["button"] == "AddB"){
a.listB.Add(new B());
return View(a);
}
else{
// save a to database
// return redirect either to list or to details of created object
}
}
}
class A {
public string a1;
public string a2;
public string a3;
public List<B> listB;
}
class B {
public string b1;
public string b2;
}
}
namespace MoreInvolved {
class A {
public string a1;
public string a2;
public string a3;
public List<B> listB;
}
class B {
public string b1;
public string b2;
public List<C> listC;
}
class C {
public string c1;
public string c2;
}
}
对于这个简单的案例,代码中没有包含这些视图,但它已经过测试,运行良好。我使用了表达式树来简化它,但这并没有发布在代码中。然而,在更为复杂的案件中,我还没有完成,除了思考离奇和奇怪之外,我也没有设想出一个简单的解决方案
In the code above, the simple case is working as I expect. I can add B
s to an A
without using any session or Javascript. However I had to add a sub-route in the controller s CreateA()
to check when I ve actually pressed an add button.
In the more involved scenario, even more changes to the controller become necessary. The subroute will have to decide which B
a new C
needs to be added to.
它显示了两个案例,其中一个我已经测试过效果良好。假设使用的视图返回的html一旦发布就会被绑定回深层对象中。
The Problem
Notice, in the code, how once we are in the more involved case, the programmer is immediately stomped with how a new object should be inserted when dealing with a second level child. Specifically, I don t want to have an if condition to add a C
into a specific B
within the controller for A
. It s annoying if I had to write the same code if List<B>
existed under another parent, say D
.
Is the control model totally obliterated from MVC, doesn t that mean some things are extremely harder to do?
I believe I am flirting with the need for user controls and some way to create controls that are reusable so that I can have a controller for B
with its children (C
s) then reuse all of that when implementing a CRUD interface for A
.
There s an imbalance between the ease of creating editors for scalar types, or an object consisting of scalar types on one hand and editors for lists of objects on the other The real issue is, there are automatic editors for scalar types, but none for lists of them. The solutions that I came up with are redundant, and introduce seemingly unnecessary additional code and headache.
最后一点:我之所以这么麻烦,是因为我试图看看我能在多大程度上推动无状态状态,并保持代码的干净和在MVC模式的限制范围内。我希望有更多模式失败的地方的例子,而不是所有类似营销的模式材料。这样,我们就可以立即发现在哪里不使用某些面向模式的产品。我相信,普通的ASP.NET会使用它的视图状态和事件流模型轻松地处理我的情况