Home > C#, Magiq > It’s a kind of magiq

It’s a kind of magiq

In a previous post, I talked about a problem in the project I’m working on regarding a mass update managed by a model inheritance. Well, when the problem cames, I thought about how it would be implemented using ActiveRecord. For the API, it should be very easy, but what about the implementation? I know I need to use pure sql to make the mass updates and that should be wrong and,  as when I need a query I use the query language of ActiveRecord, I suppose it should be a query language for updates. Then I think “oh, it would be nice if the ActiveRecord queries were solved with linq”. And then, “oh, it would be nice to have linq like mass updates”.

I googled a little and I found some interesting posts about it, but none of the given solutions meet my expectations. What I wanted to do is something like:

products.Where( x=> x.ExpirationDate >= DateTime.Now )
        .Update( x=> x.State = ProductState.Expired );

Well… I talked with my friend Sebastián Piu and we realize that we should make it work, so Magiq was born. Now it is in a kind of POC version but it’s working for Linq-to-sql. We are planning to add support for nhibernate and entity framework soon. And a lot of code improvements! (if you see the code you will understand the need of code improvements :P).

Basically, what you can actually do with Magiq is:

Mass updates:

products.Where( x=> x.ExpirationDate >= DateTime.Now )
        .Set( x=> x.State, ProductState.Expired )
        .Update();

Mass deletes:

products.Where( x=> x.ExpirationDate >= DateTime.Now )
        .Delete();

Mass inserts:

products.Where( x=> x.ExpirationDate >= DateTime.Now )
        .Insert( new ExpirationRecord { Description = x.Name + " expired" } );

You can see the code and follow the project in the Magiq codeplex site
In future posts I will talk a little about the issues with the implementation. Basically, about how hard is to work with linq-to-sql and how interesting is working with lambda expressions.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: