Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I generated a entity classes + data contexts by LINQ to Sharepoint and created a repository classes. When I use normal database and mapping like Entity framework I've got usually some base repository class with some common generic methods like Find(Expression> where). Theese methods are generic and placed in base class, example:

class BaseRepository<TEntity>
{
    public TEntity Find(Expression<Func<TEntity, bool>> where)
    {
         _database.CreateObjectSet<TEntity>().AsQueryable().Where(where).SingleOrDefault(); // Implementation with Entity FW
    }
}

Concrete implementations of repository classes are created this way:

class ProductsRepository : BaseRepository<Product> // etc ...

So my question - is possible to create something similar with LINQ to Sharepoint or I have to repeat implementation of these common methods in every repository class?

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted
Expression<Func<Task, bool>> get = t => t.Id == 2;

string url = "http://localhost:12345";
string listName = "Tasks";

MySharePointSite1DataContext context = new MySharePointSite1DataContext(url);
var list = context.GetList<Task>(listName);
var task = list.Where(get).SingleOrDefault();

using GetList method of DataContext you can create the same repository pattern as it was done in Entity Framework, but you repository needs 2 additional params - current list name you are working with (cause one site can have several lists of the same content type, that differs by name and url) and url of site you are working with to create a data context.

In very simple version you repository will look like:

    class RepBase<T>
            where T : Item
        {
            private MySharePointSite1DataContext context;
            string url;
            string listName;

        public RepBase(string listName, string url)
        {
            this.url = url;
            this.listName = listName;
             context = new MySharePointSite1DataContext(url);
        }

        public T Get(int id)
        {
            return context.GetList<T>(listName).Where(i => i.Id == id).SingleOrDefault();
        }
    }

and Item is base content type that provides you id parameter and all other implement you.

Also my strong consideration not to use Find method that receives expression that is used to filter data - that is very powerful, but decrease Repository pattern strength cause you split data access logic in repository and in custom code that use the repository. Thus you may have several places that a new change should be applied instead of one in the repository.

share|improve this answer
add comment

Chris O'Brien has a good blog post on the subject: Simple data access patterns for SharePoint Lists.

You should also take a look at the guidance from the Microsoft patterns and practices group on the Repository Pattern.

share|improve this answer
    
+1 for the Patterns and Practice Group direct link. No reason to re-invent the wheel when Microsoft has already paid someone to do the hard work. –  Tom Resing Nov 10 '11 at 22:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.