Pages

Monday, January 27, 2014

Generic Repositories Including Includes!

The real credit for this solution goes to Steve Moss. He posted this article last year:

http://www.appetere.com/Blogs/SteveM/May-2012/Passing-Include-statements-into-a-Repository

All that I've done is modified his work to fit in with the generic repository pattern that I often use.

http://www.viamacchina.com/2013/07/a-generic-repository-for-entity.html

One of the difficulties in using a repository to manage your data is that the repository itself can end up obscuring functionality from the underlying data provider. The pattern that I previously wrote about did just this. If you needed to Include relationships that were not originally established in your entity model, you had to create additional logic to retrieve that missing data.

As the Entity Framework has matured, the support for Including data from related entities has improved. Specifically, since EF 4.1, you can use strongly typed lambda's to specify relationships to eagerly load data. This, in combination with the existing string "Include", make working with entity relationships much more convenient.

To extend the original repository pattern to support dynamic includes, all that has to be done is to add an additional Select overload to the repository interface, and then make a quick change to the base class and the child classes that implement it.

1. Update the interface definition. The overloaded Select method that accepts an array of Expressions is the key.
public interface IRepository : IDisposable
{
    void Insert<E>(E entity) where E : class;

    void Update<E>(E entity) where E : class;

    void Delete<E>(E entity) where E : class;

    IQueryable<E> Select<E>() where E : class;

    IQueryable<E> Select<E>(params Expression<Func<E, object>>[] includeExpressions) where E : class;

    E Select<E>(object key) where E : class;
}

Thursday, October 24, 2013

Lights, Camera, Action Delegates!

I recently worked on a project that a number of other developers collaborated on. All of the contributors to the project are very smart people, and I was impressed by much of the work that they had done. But one thing caught my eye, and I want to write a post about it since there are folks out there that are not familiar with this technique.

The Func and Action delegates were introduced in .Net 3.5 and they have a variety of uses. One of the best uses that I have found for them is to separate procedural code from actual business logic. Specifically, I like to create methods that accept an action delegate to control database, Entity Framework, or Linq to Sql contexts, and WCF service operations.

In the project I was working on I repeatedly found classes in the data layer with code like this (in this example a connection object has already been created and passed into the class) :

IDbCommand dbCommand = connection.CreateCommand();
dbCommand.CommandText = "Command_Text";

try
{
    if (dbCommand.Connection.State == ConnectionState.Closed)
        dbCommand.Connection.Open();

    dbCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
    Log.DefaultLog.WriteVerbose(ex.Message + Environment.NewLine + ex.StackTrace);
    throw ex;
}
finally
{
    if (dbCommand != null && dbCommand.Connection != null && dbCommand.Connection.State == ConnectionState.Open)
        dbCommand.Connection.Close();
}

Friday, August 16, 2013

SSIS and SharePoint List Sources with Lookup Fields

If you are not familiar with the Microsoft SQL Server Community Samples: Integration Services site, then I recommend that you check it out:

https://sqlsrvintegrationsrv.codeplex.com/

The site contains excellent SSIS samples and documentation on how to build solutions for everyday problems using SSIS. One of the most useful samples covers using a SharePoint list as a data source. A special SSIS Data Adapter project has been created and packaged so that you can run an installer and then use SharePoint lists in your SSIS package. The project can be found here:image

https://sqlsrvintegrationsrv.codeplex.com/releases/view/17652

After following the installation procedure you end up with a SharePoint List Source and SharePoint List Destination in your toolbox.
You can then use these sources and destinations in any Data Flow Task.
image

While using the SharePoint List sources today I ran into an issue using a lookup field. Essentially, my package was copying data from a SharePoint list into a local database table. The data exposed by the List Source included the internal SharePoint lookup value.image Essentially, it was including the field type and the separator (in this case ‘string;#’), which I did not want in my table.

There is an easy fix to remedy this situation though.

Thursday, August 8, 2013

SharePoint RenderingTemplate Gotcha!

Recently when working on a SharePoint 2010 project I let a minor mistake cause a great deal of frustration. I was creating a custom field that used a custom rendering template. I have done this before, but I guess on this day my mind was in WebForms land and I ended up with a solution that would deploy, but when I went to access my custom field, it would not render.

Here is a picture of my template… can you see the problem?

image

I had included the button OnClick call in the control markup. If you do this, your template will not render because it does not know how to wire up that event to it’s respective event handler. Take note of the Control declaration. Unlike normal ASP.Net User Controls, there is no reference to a Code Behind or Inherited Type… therefore… any events that need to be wired up will have to be done in your RenderingTemplateControl class file, and not in the markup!

I hope this helps!