Th26 - Building High Quality Solutions with Design Patterns & Application Foundations
for SharePoint 2010
Christoffer von Sabsay [email protected]
Agenda • Design Patterns – what, why and when? • Repository Pattern • Service Locator Pattern • Model-View-Presenter Pattern • Application Foundations for SharePoint 2010
SharePoint vs .NET development • SharePoint is just a .NET application, right? • So are there really any differences between
SharePoint and plain .NET development?
Design Patterns • What is it? • Why should we use it? • When should we use it?
Repository Pattern
Client Business
Logic
Data Source
Repository
Entity – Data Mapping
Business Entity
Business Entity
Persist
Retrieve
Model-View-Presenter Pattern
IView Presenter
Model
View
Service Locator Pattern
Class Service A
Service B
Service Locator
Application Foundations for SP2010
• SharePoint Service Locator • Logger • Configuration Settings Manager
SharePoint Service Locator using Microsoft.Practices.ServiceLocation; using Microsoft.Practices.SharePoint.Common.ServiceLocation;
IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
IServiceLocatorConfig typeMappings = serviceLocator.GetInstance<IServiceLocatorConfig>();
typeMappings.RegisterTypeMapping<IService1, Service1>();
IService1 service1 = serviceLocator.GetInstance<IService1>();
Other options for Service Location • Custom implementation • Unity • Spring.NET • StructureMap • Other frameworks
Logger using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.Logging;
using Microsoft.SharePoint.Administration;
IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
ILogger logger = serviceLocator.GetInstance<ILogger>();
logger.TraceToDeveloper("My message.");
logger.TraceToDeveloper("My message.", TraceSeverity.High);
logger.LogToOperations(msg, EventSeverity.Error);
Configuration Settings using Microsoft.Practices.ServiceLocation; using Microsoft.Practices.SharePoint.Common.Configuration; using Microsoft.Practices.SharePoint.Common.ServiceLocation; IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent(); IConfigManager configManager = serviceLocator.GetInstance<IConfigManager>(); IPropertyBag bag = configManager.GetPropertyBag(ConfigLevel.CurrentSPWebApplication); configManager.SetInPropertyBag("MySetting", DateTime.Now, bag); IHierarchicalConfig config = serviceLocator.GetInstance<IHierarchicalConfig>(); if(config.ContainsKey("MySetting")) lastUpdate = config.GetByKey<DateTime>("MySetting"); configManager.RemoveKeyFromPropertyBag("MySetting", bag);
Summary • Learn about the different design patterns and
when to use them • Use Application Foundations for common
tasks such as configuration and logging
Questions?
Resources Developing Applications for SharePoint 2010 http://msdn.microsoft.com/en-us/library/ff770300.aspx Developing Applications for SharePoint 2007 http://msdn.microsoft.com/en-us/library/ff800762.aspx Application Foundations for SharePoint 2010 http://msdn.microsoft.com/en-us/library/ff798371.aspx
Keep in touch! E-mail: [email protected] Blog: http://christoffervonsabsay.wordpress.com Twitter: sabsay