LINQ to SQL
There are 6 entries for the tag LINQ to SQL
Thursday, July 22, 2010 10:35 PM | Comments

A common requirement for many applications is filtering rows of data based on the current user. A simple example is an e-commerce site which would want to make sure a Customer who is logged in is only able to view his own Orders. Another example would be a payroll site that would want to make sure a user could only view their own Employees. These are of course trivial examples, and could be solved in a number of ways. My concern is that often times the responsibility of security is placed upon each individual developer to make sure they are calling the right methods on your repositories. This may seem fine to many readers, and we could debate whether or not this should be the case, but my current take on security is to let the application infrastructure handle those details for you. What if I could easily...

Tuesday, September 8, 2009 10:59 PM | Comments

Today I began integration testing my SqlRepository while I experiment with an MVC prototype. I tried googling for a simple base class for integration testing a LINQ to SQL DataContext that would provide automatic rollback between each test, but after (minimal) searching, decided to simply write my own. Perhaps this will help someone. This base class was written for MS Test, but simply renaming the attributes accordingly would yield the same results on nUnit, xUnit, etc. The Code Simply inherit your test class from this base class and any changes done via the internal Context property will be automatically rolled back after each test runs. /// <summary> /// The base class will allow LINQ to SQL integration testing with automatic rollbacks after each test /// </summary> /// <typeparam name="TContext">The type of data context to use</typeparam> public abstract class RolledBackDataContextTests<TContext> where TContext : DataContext { private TContext _context; ...

Friday, September 4, 2009 2:14 PM | Comments

LINQ and Expression Trees have enabled a ton of possibilities for us as developers, particularly in establishing some code-reducing convention based querying. I have found myself playing with Expressions in C# since their inception and will hopefully be publishing a few other posts I have queued up. For now, this post will simply demonstrate how to take advantage of a popular convention whereby each database table always has a single integer primary key named “Id” By taking advantage of this primary key convention I can save myself a ton of boilerplate code to query a table by ID. The Extension Method public static T GetById<T>(this IQueryable<T> query, int id) where T : class { // Create lambda parameter x of type T var param = Expression.Parameter(typeof(T), "x"); // Given x param, extract the Id property var...

Thursday, February 19, 2009 2:49 PM | Comments

A project I am now working on is using the Web Client Software Factory (WCSF) from the patterns & practices team. It is basically an elaborate MVP-based framework that currently sits on top of ASP.NET Webforms. One of the components it ships with is the ObjectContainerDataSource which is a lot like the ObjectDataSource that ASP.NET provides. I admittedly have little experience with either of these controls, since I am not a huge fan of declarative data binding in larger applications. It does however offer better integration with the MVP pattern, simplifying the “bubbling” of DataSource events up to the Presenter to handle them. The Presenter is of course where all of the logic for the view and its services belongs. At first everything was working great: selects, server-side paging, server-side sorting, even inserting. But when I tried some Updates and Deletes with the GridView and the ObjectContainerDataSource...

Monday, January 12, 2009 6:50 AM | Comments

A couple months ago I wrote a basic set of extension methods to handle automatic auditing in LINQ to SQL. Well I have received a large number of emails regarding this particular project so I have decided to focus on cleaning up my v2 API and releasing it on CodePlex. There was a lot of room for improvement from version 1 and today I am going to post the all new LINQ Audit Trail code. This new version is significantly enhanced in the previous version. Objective Automatic auditing of all inserts/updates/deletes for any table in your database with a single line of code, including: What table was modified? What fields changed? Who made the change? When did it...

Thursday, May 8, 2008 6:10 AM | Comments

This post is out of date This post is outdated and has significantly enhanced with a new version. Please see LINQ Audit Trail V2 In a project I am currently working on, we had a fairly common requirement of recording an Audit Trail of any data changes. The requirements were typical, provide a running log of any changes in the database, including: What table was modified? What fields changed? Who made the change? When did it occur? Naturally, there are many ways to tackle this problem. In the past I have either relied on writing database Triggers for the tables, or wrote the Auditing logic right into the stored procedure that was doing the modification. But this time, since the project is utilizing...