Scott Watermasysk

Still Learning to Code

ActiveLinq

I have been evaluating Linq to SQL and Castle ActiveRecord for a small project I am working on. In end I decided to go with ActiveRecord. As part of my experiment, I did a quick prototype of an ActiveRecord like pattern using Linq to SQL.

I have zero plans to continue to working on and I cannot stress ActiveRecord like enough. In all honesty, this is an insult to ActiveRecord, but life is too short and there is no reason for this code to die on my hard drive if someone else might find it useful.

What does it do?

What it does today is enable very simple querying without the need write more than a line of code. There are some parts like using lambas for the where filter that I really like, but there are others like the order by which feel hacky.

How to use it?

  1. Add the code files in the download to your project
  2. Take a Linq to SQL class and derive from from ActiveLinq
  3. Register the connectionstring for each type (preferably in App_Start)

Example:


[Table(Name = "wc_Users")]
public class User : ActiveLinq<User>
{
   //Columns
}

Then you can use it like this:


[TestFixture]
public class LinqRunner
{
    [TestFixtureSetUp]
    public void Init()
    {
        ActiveLinqStarter.RegisterTypesByConnectionstring("...", typeof(User));
    }
 
    [Test]
    public void FindFirstUserMatchingCondition()
    {
        User u = User.FindFirst(ux => ux.IsEnabled && !ux.IsAdmin,  User.OrderByAsc(x => x.Email));
        Assert.IsNotNull(u,"user could not be found");
    }
 
    [Test]
    public void FindASingleUserRecord()
    {
        User u = User.FindOne(ux => ux.Username == "scottw"); 
        Assert.IsNotNull(u,"user could not be found");
 
        u = User.FindOne(ux => ux.Username == "DOES NOT EXIST");
        Assert.IsNull(u, "A user was found when it should not have been");
    }
 
    [Test]
    [ExpectedException(typeof(ArgumentException))]
    public void FindASingleUserRecordException()
    {
       User.FindOne(ux => ux.IsEnabled); //Should return 16 enabled users
    }
 
    [Test]
    public void FindAllUserRecords()
    {
        var users = User.FindAll(null, null); 
        Assert.IsTrue(users.Count == 17, "Could not find all 17 users");
    }
 
    [Test]
    public void FindAllUserRecordsWithOffset()
    {
        var users = User.FindAll(5,20, null, null);
        Assert.IsTrue(users.Count == 12, "Offset failed, should be 12 uses");
 
        users = User.FindAll(0, 20, ux => ux.IsEnabled);
        Assert.IsTrue(users.Count == 16, "Could not find all 16 enabled users");
    }
}

Download ActiveLinq.