谈到Model View Presenter模式之后的随笔[讨论版]

       ModelViewPresenter 模式已经推出快二年了吧,今天公司新的兄弟,午饭时和我谈到有关于在项目中是否可以使用MVP模式,然后呼啦啦和我讲了许多,我听了蛮有感处的,但结果让这位兄弟写的时候,他却写了大半个下午,还没个MVP影子,Jezz!  我只好花上10几个分钟的时间写了一个Demo。
        本来想不发到Blogs中,但是还是和大家分享一下,
        第一,让初学者有关的例子可以参照,毕竟读代码,要比看书有感觉,
        第二,也可以让高手们指点指点,有哪些不足,然后看看高手们能不能就有关于MVP模式,在项目中的应用谈谈他们的看法。
        第三,有关于MVP模式的文章cnblogs中好像很少,因为我只看到过两篇,一篇是介绍MVP模式的,一篇是翻译的吧。
   *嗯,不管大家对以下的代码部分有什么疑义,都可以进行评论,希望可以达到共同学习,互相学习的目的,评出你的观点,说出你的理解。
                                                     (希望不要说粗口,这样就不好了。)

以下是代码部分:

ModelLayer:


namespace MVPDesign.ModelLayer
{
    
public class XmlData
    
{
        
/// <summary>
        
/// 根据Id取得Xml中的数据
        
/// </summary>
        
/// <typeparam name="T">返回的Type</typeparam>
        
/// <typeparam name="V">Id的Type</typeparam>
        
/// <param name="id"></param>
        
/// <returns>XmlData</returns>

        public static T GetDataById<T, V>(V id) where T : XmlData
        
{
            XmlDocument xmldocument 
= new XmlDocument();

            xmldocument.Load(
"XmlData.xml");
            String path 
= "/XmlData/Data[@id={0}]/{1}";
            XmlNode titleNode 
= xmldocument.SelectSingleNode(String.Format(path, id, "Title"));
            XmlNode textNode 
= xmldocument.SelectSingleNode(String.Format(path, id, "Text"));

            XmlData xmlData 
= new XmlData();
            xmlData.title 
= titleNode.InnerText;
            xmlData.text 
= textNode.InnerText;
            
return xmlData as T;
        }


        
internal String title;
        
/// <summary>
        
/// 相对应于XML文件中的Title
        
/// </summary>

        public String Title
        
{
            
get return title; }
        }


        
internal String text;
        
/// <summary>
        
/// 相对应于XML文件中的Text
        
/// </summary>

        public String Text
        
{
            
get return text; }
        }

    }

}


ViewLayer:
namespace MVPDesign.ViewLayer
{
    
public interface IDataView
    
{
        Int32 DataId 
get; }
        String Title 
set; }
        String Text 
set; }
    }


    
public class DataPresenter
    
{
        
internal readonly IDataView view;

        
public DataPresenter(IDataView view) this.view = view; }

        
public void RetrieveData()
        
{
            XmlData myPost 
= XmlData.GetDataById<XmlData,Int32>(view.DataId);
            view.Title 
= myPost.Title;
            view.Text 
= myPost.Text;
        }

    }

}

PresenterLayer:
using MVPDesign.ViewLayer;
namespace Presenter
{
    
public partial class PresenterLayer : Page, IDataView
    
{
        
private DataPresenter dataPresenter;

        
protected void Page_Load(object sender, EventArgs e)
        
{
            dataPresenter 
= new DataPresenter(this);
        }


        
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        
{
            dataPresenter.RetrieveData();
        }


        Int32 IDataView.DataId
        
{
            
get return Convert.ToInt32(this.DDL.SelectedValue); }
        }


        String IDataView.Title
        
{
            
set this.lblTitle.Text = value; }
        }


        String IDataView.Text
        
{
            
set this.lblText.Text = value; }
        }

    }

}


XMLFile:
<?xml version="1.0" encoding="utf-8" ?>
<XmlData>
    
<Data id="1">
        
<Title>Linq</Title>
        
<Text>Linq To Memery Object</Text>
    
</Data>
    
<Data id="2">
        
<Title>BLinq</Title>
        
<Text>Linq To Web</Text>
    
</Data>
    
<Data id="3">
        
<Title>XLinq</Title>
        
<Text>Linq To Xml</Text>
    
</Data>
    
<Data id="4">
        
<Title>DLinq</Title>
        
<Text>Linq To DataBase</Text>
    
</Data>
    
<Data id="5">
        
<Title>SLinq</Title>
        
<Text>Linq To Stream</Text>
    
</Data>
</XmlData>

PresenterForm:
<form id="form1" runat="server">
    
<table style="width: 659px; height: 481px">
        
<tr>
            
<td style="width: 178px">
                
<asp:Label ID="lblChoose" runat="server" Text="Please Choose ,Feeling MVP" Width="195px"></asp:Label><br />
                
<asp:DropDownList ID="DDL" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
                    
<asp:ListItem Text="Please Choose Option" Value="0"></asp:ListItem>
                    
<asp:ListItem Text="Linq" Value="1"></asp:ListItem>
                    
<asp:ListItem Text="BLinq" Value="2"></asp:ListItem>
                    
<asp:ListItem Text="XLinq" Value="3"></asp:ListItem>
                    
<asp:ListItem Text="DLinq" Value="4"></asp:ListItem>
                    
<asp:ListItem Text="SLinq" Value="5"></asp:ListItem>
                
</asp:DropDownList>
            
</td>
            
<td>
                
<asp:Label ID="lblDisplayTitle" runat="server" Text="DisplayTitleFromXml"></asp:Label><br />
                
<asp:Label ID="lblTitle" runat="server" Font-Size="Larger" ForeColor="Red"></asp:Label>
            
</td>
            
<td>
                
<asp:Label ID="lblDisplayText" runat="server" Text="DisplayTextFromXml"></asp:Label><br />
                
<asp:Label ID="lblText" runat="server" Font-Size="Larger" ForeColor="Blue"></asp:Label>
            
</td>
        
</tr>
    
</table>
    
</form>

程序最终运行截图:


嗯,差不多,写完了。
posted @ 2007-09-26 21:26  RicoRui  阅读(2648)  评论(29编辑  收藏  举报