设为首页 收藏本站
查看: 758|回复: 0

[经验分享] 探索MVP(Model-View-Presenter)设计模式在SharePoint平台下的实现

[复制链接]

尚未签到

发表于 2015-9-24 10:27:22 | 显示全部楼层 |阅读模式
  

  对于SharePoint Developers来说,往往会过多的去关注SharePoint平台和工具,而把设计模式和代码的可测试性放在了一个较低的优先级。这并不是说SharePoint Developers对设计模式不感兴趣,而是缺乏在SharePoint平台下使用设计模式的经验。所以本篇Blog正如题目所示:探索MVP(Model-View-Presenter)设计模式在SharePoint平台下的实现。利用MVP设计模式,可以尽量让我们的项目分离关注点、易测试、可重用。在实现MVP时,我也会加入Repository和Service Locator这两种设计模式,Repository可以理解为一个仓储,相当于数据访问层(DAL),而Service Locator扮演了IoC角色,IoC类似一个工厂(容器),工厂内部注册了很多依赖关系,IoC容器正式使用这种依赖关系从而动态的注入(又称依赖注入)提供你所需要的实例,这样可以有效的实现解耦,即分离关注点。
  


MVP模式
  在SharePoint平台下,如开发SharePoint Farm Solution,如果不对代码进行重构,往往会出现这样的代码:
DSC0000.png
  很明显这样把所有的逻辑都杂揉在UI Logic,特别是在团队开发时,即不利于测试,也不利于分工协作。而且对于SharePoint而言,开发机性能若低,调试是苦不堪言的,其耗时难以想象。所以前期如能通过单元测试解决Bug,将大大的节约时间。幸运的是,MVP设计模式的出现,对于Web Part的开发,是非常适合的。MVP的特点是很好的分离了关注点,各司其职。把上图稍作更改如下所示:
DSC0001.png
  可以看到的是UI Logic处理的业务逻辑交给了Presenter,而UI彻底解放了,只单纯的做显示层(View)。
  

Repository Design Pattern
  从上图可以看出,Presenter并不是直接去访问SharePoint数据层( SharePoint List),而是通过了一个Repository 去间接访问,而Repository Model 封装了数据层。
DSC0002.png

  到这一步,看似完美,但实则还是在原地踏步。因为Presenter和Repository还是紧耦合着,这就好像负责Presenter的 A程序员必须要等负责Repository 的B程序员完成才能工作。

  谁叫他们紧耦合在一起呢?
  在团队开发中,我们需要的是互相独立,所以需要让负责Presenter的程序员可以使用MockRepository来做测试,这样就不会影响进度了,幸运的是,基于接口的设计,可以让我完成这个愿景。具体的实现如下:
DSC0003.png

SharePoint Service Locator Design Pattern
  仔细分析上图,Presenter还是没有解耦,因为这必须要在Presenter中把某个Repository的实例创建出来,所以Presenter还是依赖了Repository这个项目程序集。这对测试没有好处,(正如前面所分析的那样,开发Presenter 的A程序员必须可以在单元测试里使用MockRepository来测试,而在真实的项目里使用B 程序员开发的AnyRepository)。
  那么有没有一种方式能彻底将Presenter和Repository解耦呢?
  当然有,如依赖注入,本篇博客介绍的是由Microsoft Patterns and Practices 专门为SharePoint开发的IoC容器:SharePoint Service Locator。

什么是IoC容器

  传统的控制流,从客户端创建服务时,必须指定一个特定服务实现(并且对服务的程序集添加引用),IoC容器所做的就是完全将这种关系倒置过来(倒置给IoC容器),将服务注入到客户端代码中,这是一种推得方式(依赖注入)。术语"控制反转",即客户放弃代码的控制,将其交给IoC容器,也就是将控制从客户端代码倒置给容器,所以又有人称作好莱坞原则"不要打电话过来,我们打给你"。实际上,IoC就是使用IoC容器将传统的控制流(客户端创建服务)倒置过来,将服务注入到客户端代码中。

  总之一句话,客户端代码能够只依赖接口或者抽象类或基类或其他,而不关心运行时由谁来提供具体实现。
  使用IoC容器如SharePoint Service Locator,首先配置依赖关系(即当向Ioc容器询问特定的类型时将返回一个具体的实现),所以这又叫依赖注入。

MVP在项目中的实践

  有了上面的分析,那么就来设计漂亮的代码:


  • 模块化代码
  • 松耦合,无依赖
  • 代码重用
  • 独立的单元测试



  • 首先创建IVew,单纯的给UI界面"取"数据和"显示"数据



  public interface IEmployeeView
{
string Country { get; }
IEnumerable<EmployeeModel> EmplyeeList { set; }
bool NotEmployeesFoundMessageVisible { set; }
}


    • 接着WebPart实现IView




[ToolboxItemAttribute(false)]
public partial class VisualWebPart1 : WebPart,IEmployeeView
{
// Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
// using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
// for production. Because the SecurityPermission attribute bypasses the security check for callers of
// your constructor, it's not recommended for production purposes.
// [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
private EmployeePresenter _presenter;
public VisualWebPart1()
{
IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent(SPContext.Current.Site);
IEmployeeRepository employeeRepository = serviceLocator.GetInstance<IEmployeeRepository>();
_presenter = new EmployeePresenter(this, employeeRepository);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeControl();
}
protected void Page_Load(object sender, EventArgs e)
{
_presenter.GetEmployees();
}
public string Country
{
get { return HttpContext.Current.Request["country"] }
}
public IEnumerable<Model.EmployeeModel> EmplyeeList
{
set
{
rptDataSource.DataSource = value;
rptDataSource.DataBind();
}
}
public bool NotEmployeesFoundMessageVisible
{
set { lblMessage.Visible = value; }
}
}


  • 接着对BaseRepository的设计



public abstract class BaseRepository<T>
{
protected SPWeb _web;
public BaseRepository()
{
}
public BaseRepository(SPWeb web)
{
_web = web;
}
protected IEnumerable<T> GetEntities(SPListItemCollection items)
{
List<T> list =null;
if (items.Count>0)
{
list = new List<T>();
foreach (SPListItem item in items)
{
list.Add(GetEntity(item));
}
}
return list;
}
protected abstract T GetEntity(SPListItem item);
}

  • 正如前面分析的那样,基于接口的设计能更好的做单元测试,所以创建IRepository



public interface IEmployeeRepository
{
IEnumerable<EmployeeModel> GetEmployeeByCountry(string country);
}

  • 实现Repository



public class EmployeeRepository:BaseRepository<EmployeeModel>,IEmployeeRepository
{
public EmployeeRepository():base()
{
}
public EmployeeRepository(SPWeb web):base(web)
{
}
public IEnumerable<EmployeeModel> GetEmployeeByCountry(string country)
{
SPWeb web = _web ?? SPContext.Current.Web;
SPList list = web.Lists.TryGetList("Employee");
IEnumerable<EmployeeModel> employeeEntitiesList = null;
if (list!=null)
{
SPQuery query = new SPQuery();
query.ViewFields = string.Concat("<FieldRef Name='Title'/>", "<FieldRef Name='CountryField'/>");
query.ViewFieldsOnly = true;
if (!string.IsNullOrEmpty(country))
{
query.Query = @"<Where>
<Eq>
<FieldRef Name='CountryField'/>
<Value Type='Lookup'>" + country + @"</Value>
</Eq>
</Where>";
}
else
{
query.Query = "";
}
SPListItemCollection employeeListColl = list.GetItems(query);
employeeEntitiesList = GetEntities(employeeListColl);
}
return employeeEntitiesList;
}
protected override EmployeeModel GetEntity(SPListItem item)
{
return new EmployeeModel() {
Name = item["Title"].ToString(),
Country = item["CountryField"].ToString()
};
}
}


  • 因为Presenter与Repository彻底解耦,故在Presenter中,根据构造函数动态注入View和Repository



public class EmployeePresenter
{
private IEmployeeView _view;
private IEmployeeRepository _repository;
public EmployeePresenter(IEmployeeView view,IEmployeeRepository repository)
{
_view = view;
_repository = repository;
}
public void GetEmployees()
{
string country= _view.Country;
if (string.IsNullOrEmpty(country))
{
return;
}
var employees = _repository.GetEmployeeByCountry(country);
if (HasEmployeeFound(employees))
{
ShowEmployees(employees);
}
else
{
ShowEmployeeNotFoundMessage();
}
}
private void ShowEmployees(IEnumerable<EmployeeModel> employees)
{
_view.EmplyeeList = employees;
_view.NotEmployeesFoundMessageVisible = false;
}
private void ShowEmployeeNotFoundMessage()
{
_view.NotEmployeesFoundMessageVisible = true;
}
private bool HasEmployeeFound(IEnumerable<EmployeeModel> employees)
{
if (employees!=null)
{
return employees.Count() > 0;
}
return false;
}
}


  • 关键点来了,在Feature中向SharePoint Service Locator依赖注册(IRepositoy/Repositoy)



  public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent(site);
IServiceLocatorConfig serviceLocatorConfig = serviceLocator.GetInstance<IServiceLocatorConfig>();
serviceLocatorConfig.Site = site;
serviceLocatorConfig.RegisterTypeMapping<IEmployeeRepository, EmployeeRepository>();   
}

//Uncomment the method below to handle the event raised before a feature is deactivated.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent(site);
IServiceLocatorConfig serviceLocatorConfig = serviceLocator.GetInstance<IServiceLocatorConfig>();
serviceLocatorConfig.Site = site;
serviceLocatorConfig.RemoveTypeMappings<IEmployeeRepository>();
}

  • 注意这个Feature 的Scope必须在在Site Level之上(建议在Farm),因为有可能用户在有权限Deactivate Feature
  • 根据依赖关系动态获取实例



private EmployeePresenter _presenter;
public VisualWebPart1()
{
IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent(SPContext.Current.Site);
IEmployeeRepository employeeRepository = serviceLocator.GetInstance<IEmployeeRepository>();
_presenter = new EmployeePresenter(this, employeeRepository);
}
总结

  至此,探索MVP(Model-View-Presenter)设计模式在SharePoint平台下的实现,已经全部结束了,在这个基础架构上还可以继续优化,如DataMapper等。相信构建高效清晰整洁的代码是每个程序员所追求的,你不得不佩服国外大神们总结的设计模式是多么的精妙,或许怀着敬畏的心才能慢慢体会其中的奥秘。点击此处下载源代码

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-118092-1-1.html 上篇帖子: SharePoint 2010 与RMS集成方案 下篇帖子: 步步为营 SharePoint 开发学习笔记系列 一、简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表