A LINQ to the CRUD
|
A question many people have run into is: How does CRUD fit into LINQ-to-SQL? While LINQ-to-SQL (I'll abbreviate as DLINQ) provides a very fast and easy way for us to start querying our data, it doesn't handle updates as beautifully. It is particularly noticeable when you are doing multi-tier and hence cannot share a DataContext. Let's consider an example database called "MembersDatabase" which has a table called "Accounts". Accounts has an int primary key, and a varchar Email field. We use the DLINQ designer and create the dbml that generates a class called MembersDataContext. How does our app-tier code look? I'll give you a hint, it starts with "ug" and rhymes with "nasty": // Select It's not horrible; it's certainly better than anything before it. But, we can do better. I created some helper classes to do so (see attached file). First, DatabaseBase<TContext>. This holds our tables, and provides DataContext helper functions Use and Query. Second, TableBase<TItem, TKey>. This actually provides our CRUD methods. I don't think anyone is overly interested in the implementation details (comment if I'm wrong), so here's how you declare your CRUD types: class MembersDatabase : DatabaseBase<MembersDataContext> That's it. You create a new class to serve as your "database" class. All that's required here is to inherit from DatabaseBase. Next, for each table, simply create a new field via CreateTable. The first parameter is a lambda function that selects the right table off the DataContext. The second parameter is a lambda expression that selects the primary key. Not much too it. So, how does our previous chunk of code look with this small helper library? // Select That's about 40% less code. It’s far more straightforward, being a single block. To start using this code, just drop DatabaseBase.cs into your project. It adds DatabaseBase to System.Data.Linq. Then subclass as shown above, and you're on your way to LINQ updating bliss. What do you think? DatabaseBase.cs (5.65 KB) Update: I forgot to mention, you'll want to turn UpdateCheck to Never on your columns in the LINQ-to-SQL designer. |
| Code |
나는 오늘도 기적을 만든다 .
posted on 2008-06-18 20:18 NullReferenceException 阅读(239) 评论(0) 收藏 举报
浙公网安备 33010602011771号