Udostępnij za pośrednictwem


Creating a Repository

Typical Goals

In this scenario, you want to create a repository class for a specific content or data type in a SharePoint list.

Solution

In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SPG.Common.dll.

Define a repository class and a class for the business entity that corresponds to the content type fields that are found in each of your list items. Optionally, you can define an interface that is implemented by your repository class.

Note

Even though it is optional to define an interface for the repository, it is recommended that you do so. This decouples consumers from the implementation and allows you to use the service location pattern.

Establish mappings between the public properties of the business entity and the fields of the SharePoint list items that you will access using the repository. First, build the field mappings with an instance method of the ListItemFieldMapper<T> class. This class is instantiated with the business entity class as the type parameter T. Next, call the list item field mapper's AddMapping method for each field of the content type that corresponds to a public property of the business entity.

Creating a Repository

The Partner Portal application's BusinessEventTypeConfigurationRepository class demonstrates how to create a repository. The repository uses the BusinessEventTypeConfiguration class as the business entity. Here is the definition of the BusinessEventTypeConfiguration business entity class.

public class BusinessEventTypeConfiguration
{
   public string SiteTemplate { get; set;}
   public string BusinessEventIdentifierKey { get; set; }
   public string TopLevelSiteRelativeUrl { get; set; }
}

Generally, it is recommended that you create a static class for each list or content type that contains the field GUIDs as read-only members. This improves the readability of the code and eliminates errors that can result from specifying field GUIDs in multiple places. It is also recommended to use GUIDs over internal names as internal names are much more likely to be reworded and updated than GUIDs.

The following code shows how the repository uses the list item field mapper to establish field mappings between the Business Event Type Configuration list in SharePoint and the BusinessEventTypeConfiguration business entity class.

public class BusinessEventTypeConfigurationRepository:  
                                           IBusinessEventTypeConfigurationRepository 
{
   ListItemFieldMapper<BusinessEventTypeConfiguration> listItemFieldMapper = 
                         new ListItemFieldMapper<BusinessEventTypeConfiguration>();

   public BusinessEventTypeConfigurationRepository()
   {
      listItemFieldMapper.AddMapping(
                              FieldIds.SiteTemplateFieldId, 
                              "SiteTemplate");
      listItemFieldMapper.AddMapping(
                              FieldIds.BusinessEventIdentifierKeyFieldId, 
                              "BusinessEventIdentifierKey");
      listItemFieldMapper.AddMapping(
                              FieldIds.TopLevelSiteRelativeUrlFieldId, 
                              "TopLevelSiteRelativeUrl");
   }
   // ...
}

The repository composes the ListItemFieldMapper<T> by creating a private instance field of the ListItemFieldMapper<T> class. The mapper uses the BusinessEventTypeConfiguration type. This represents the business entity that contains the data from the SharePoint list item.

The AddMapping method is invoked with the list field GUID identifier and the entity property name for each value that is mapped between the list item and the business entity. This step tells the ListItemFieldMapper object how to translate between them.

For implementation details about this example, see the BusinessEventTypeConfigurationRepository.cs and BusinessEventTypeConfiguration.cs files of the Microsoft.Practices.SPG.SubSiteCreation\BusinessEventTypeConfiguration directory in the reference implementation.

Usage Notes

For predictable results when calling the AddMapping method, make sure that each SharePoint field ID and each business entity property name is unique. You should also make sure there is type consistency between the fields of list and the properties of your business entity. The **ListItemFieldMapper'**s AddMapping method does not throw exceptions for duplicate names or IDs, or for type inconsistencies between the SharePoint list and the business entity class. If these mappings are not correct, the ListItemFieldMapper class throws a ListItemFieldMappingException when an entity instance is created.

Home page on MSDN | Community site