What is so bad about DataContexts?

You know, there comes a time when one comes full circle, and realizes that a lot of what he’s done since he started something hasn’t really added anything of value.

That’s kind of where I am at the moment. After learning mvc, then moving on to persistence abstraction, and eventually applying the same principles to Linq2Sql, I began to wonder if any of it was really to my benefit.

Just as a test, I created a simple Library mvc solution. In it, I dropped a Linq2sql designer, dragged my tables on to it, and saved it. I then added a few custom attributes based on ValidationAttribute, some entity metadata classes, a resource file with required strings, and a few extension methods. Last, I added a few methods to the generated data context, using partials, that overcome concurrency issues with updates. The resulting files (minus the views) were:

Author.cs
AuthorController.cs
Book.cs
BookController.cs
HtmlExtensions.cs
IsNullOrEmpty.cs
LibraryContext.cs
Locale.resx
QueryExtensions.cs

Controller code was simple:

private LibraryContext context = new LibraryContext();
 
[HttpGet]
public ActionResult Index()
{
    return View(context.Authors);
}
[HttpGet]
public ActionResult Add()
{
return View(new Author());
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Add([Bind(Exclude = "Id")] Author transient)
{
if (ModelState.IsValid)
if (context.Insert(transient))
return RedirectToAction("Index");
return View(transient);
}
[HttpGet]
public ActionResult Edit(int id)
{
return View(context.Authors.WithId(id));
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Edit(Author transient)
{
if (ModelState.IsValid)
if (context.Update(transient))
return RedirectToAction("Index");
return View(transient);
}
[HttpGet]
public ActionResult Delete(int id)
{
return View(context.Authors.WithId(id));
}
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Delete(int id, bool confirmed)
{
Author current = context.Authors.WithId(id);
if (!confirmed)
ModelState.AddModelError("Confirmed", Locale.action_not_confirmed);
if (ModelState.IsValid)
if (context.Delete(current))
return RedirectToAction("Index");
return View(current);
}

This is all rudimentary to you all, I am sure, and the only reason I bring it up, is to pose the following questions:

Should I be satisfied with this?

It all works. Things display, validate and persist correctly.

Do I really need a persistence abstraction layer?

Considering I work only with SQL Server, I don’t see where I do.

Do I really need a repository layer?

I’ve extended the generated context, taking advantage of it being a partial, and added any required methods there. It made sense as any repositories would require access to the context to start with. Why not just build it in to the context instead, and use that?

Keep a few things in mind before answering or commenting: I only use SQL Server, I prefer to keep everything in a single project, and I want to be able to go from start to finish using nothing more than VisualStudio, the MVC framework, and Sql Server. I do not unit test, at least not in the same way most people do. I test, it’s just not test-driven-development. As such, I have no real need for dependancy injection. I test against an actual db. There is very little logic involved. Most of it is handled by the validators. For the rest, I use pipelines to affect an object. Those are just extension methods. So nothing really end up in a service class.

Here’s the thing, part of me wants to do things the way others are doing them. But the majority of me is telling me that it’s WAY overkill for my little apps. Intuition is great, but is sometimes opinionated, so I can’t be sure.


if(project.MeetsYourRequirements) {
    HappyFunTime();
} else {
    throw new OhNoesException();
}

Sounds like OCD, I know, because I have it. You just have to realize nothing will ever be ‘perfect’. If it gets the job done, then accept it. Perfectionism sucks, and will only lead to second guessing. It’s impossible to keep up with Web Development/Design trends, so I stopped trying. For example, I just now understand CSS floats and positioning, and now CSS3 is coming out, now I have to start the learning process over again.