Tuesday, 18 December 2007

LINQ - using LINQ as the Combined Business Logic and DAL

We have taken a few different approaches to LINQ on different projects at Lend Lease. Currently on the Bovis Lend Lease Monthly Reporting Team, we are not using the Web Client Software Factory (WCSF) and the Model View Presenter Pattern (MVP). Rather, we are using partial classes of LINQ objects (separate .cs files for each class for readability & maintainablility). This is a more rapid approach to development which I enjoy coding much more. I also just found out that one of the FrameworkTeam has just extended the WCSF ObjectContainerDatasource so that it includes ALL fields (both bound and unbound) used in a control - which avoids the problem whereby non-bound fields are nulled out from the bound objects. e.g. If I use the ObjectContainerDataSource and have a field StatusId, but don't bind it - then the object in the "Updated" Event of by datasource has a statusId of null. It would be wiped out if I updated the database with the same object - which I see as a major bug in the WCSF! (Will post about this problem and solution shortly).

Anyway back to the point - here is some very basic sample Code using LINQ as the Business Logic layer with partial classes to extend the generated SQLMetal classes (aka the classes from the generated dbml file):



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LendLease.ManagementReporting.Service.DataContract.DTO;

namespace LendLease.MRP.Service.Persistence.Entity
{

public partial class Ctl_Person_Resp
{
public string UserName
{
get { return this.Dim_User.First_Name + " " + this.Dim_User.Last_Name; }
}

/// <summary>
/// Gets list of People who have responsibility in the current level of the hierarchy.
/// </summary>
/// <param name="businessHierarchyId"></param>
/// <returns></returns>
public static List<PersonResponsibleListDTO> GetPersonResponsibleList(int businessHierarchyId)
{
MRPDataContext context = new MRPDataContext();

//Join to the the member code so we can filter the person responsible list
var query = from person in context.Ctl_Person_Resps
join hierarchy in context.Dim_Business_Hierarchy_PCs on person.Business_Hierarchy_PC_Member_Code equals hierarchy.Member_Code
where hierarchy.Business_Hierarchy_PC_SKEY == businessHierarchyId
select new PersonResponsibleListDTO { User_SKEY = person.User_SKEY, UserName = person.UserName};
return query.Distinct().ToList<PersonResponsibleListDTO>();
}

}
}

No comments: