Archive

Archive for the ‘.NET’ Category

Entity Framework with Stored Procedures?

November 18, 2013 Leave a comment

I found this study/quote interesting:

“We’ve carefully evaluated whether object-relational mapping (ORM) tools such as the Entity Framework could help us achieve simpler and more testable code without moving away from stored procedures. Our conclusion was that the Entity Framework is an extremely appealing solution, but it relies heavily on being able to compose and execute complex SQL statements at run time. It can map stored procedures, but when limited to mapping only stored procedures, it loses most of its benefits. For that reason, we decided to implement our own database-access framework as an adaptive database access layer.”

From the article:

Adaptive Access Layers + Dependency Injection = Productivity

Advertisements

Great video session on ASP.NET MVC 3

June 29, 2011 Leave a comment

Well actually this video covers a lot more than just ASP.NET MVC 3.  Here are the topics I’ve picked up from it:

There’s a lot in this video!  I don’t recommend it as your introduction into MVC 3.  I’d suggest you do some introductory training for that and even get your feet wet with some hands-on.  Then watch this.  It is fast-moving – I found myself pausing the video at various points to pick up on some of the finer points in the code.

Well done Steven Sanderson.

-Krip

Binary trees in C#

June 24, 2011 Leave a comment

Great C# implementation of a binary tree by Roni Schuetz.

As simple to use as this:

    var x = new BinaryTree<int>();
    x.Add(5);
    x.Add(7);
    Console.WriteLine(x.Count);

Or this:

    var y = new BinaryTree<Taxicab>();
    y.Add(new Taxicab() { number = 3 });
    y.Add(new Taxicab() { number = 1729 });
    Console.WriteLine(y.Count);

    class Taxicab:IComparable<Taxicab>
    {
        public int number { get; set; }
        public int frequency { get; set; }
        public int CompareTo(Taxicab other)
        {
            if (this.number < other.number)
                return -1;
            else if (this.number == other.number)
                return 0;
            else
                return 1;
        }
    }

-Krip

REST in practice

June 6, 2011 Leave a comment

I’ve heard good things about the book Rest in Practice.

Glenn Block has a post briefly touching on the book and the port of the sample app RestBucks to the WCF Web Api.

-Krip

 

Categories: WCF, Web Services Tags: ,

How to use jQuery autocomplete with MVC

June 2, 2011 2 comments

Gone are the days of hand coding AJAX to get auto complete textboxes (a.k.a. incremental search).  jQuery’s Autocomplete widget does all the heavy lifting for you.

Want to know how to use it with an ASP.NET MVC app?  Microsoft has documented this step-by-step on their asp.net site.  The article shows how to use the widget two ways: 1) with items hard-coded on the page, and 2) with items retrieved from a call to the server (controller action).

To give you a leg up I’ll share my experience.

1. When setting the Source to point to the URL of your action, I used the razor markup instead of the angle bracket / percent side syntax.

So, the updated code looks like:

$("#Title").autocomplete({ 
   source: '@Url.Action("List")', 
   select: function (event, ui) { 
      $("#movieID").val(ui.item.id); 
      } 
   });

2. The name of the parameter in the controller action must be “term” – any other name results in a null value being passed in.

3. Be sure that the property of the names you want to display is called “label”.

public ActionResult List(string term) { 
   var results = from m in _repository.Movies 
                 where m.Title.StartsWith(term) 
                 select new { label = m.Title, id = m.Id }; 
   return Json(results.ToArray(), JsonRequestBehavior.AllowGet); 
}

4. To get the right visual look you need to include some stylesheets:

   <link href="@Url.Content("~/Content/themes/base/jquery.ui.base.css")"
      rel="stylesheet" type="text/css" />
   <link href="@Url.Content("~/Content/themes/base/jquery.ui.core.css")"
      rel="stylesheet" type="text/css" />
   <link href="@Url.Content("~/Content/themes/base/jquery.ui.theme.css")"
      rel="stylesheet" type="text/css" />

-Krip

How to update model data in receiving action

May 30, 2011 Leave a comment

When using all the standard out-of-the-box fuctionality of ASP.NET MVC, you may notice that you simply can’t just update a model’s properties in an action method.  Oh the code will execute just fine, but the data you see on the page will not reflect updates from code.  Here’s an example of what I mean:

[HttpPost]
public ActionResult Index(myModel m)
{
   if (ModelState.IsValid)
   {
      // do some stuff
      m.Message = "data submitted";
   }
   return View(m);
 }

The new value of m.Message will not be reflected on the page.  Instead you will see the previous value.  For the updates to be reflected you must remove the old data from Model State.  You do this in one of two ways:

  • Either clear out the affected properties, one by one using ModelState.Remove(propertyName)
  • Or just clear out the entire model state with ModelState.Clear()

So the above code becomes:

[HttpPost]
public ActionResult Index(myModel m)
{
   if (ModelState.IsValid)
   {
      // do some stuff
      ModelState.Clear()
      m.Message = "data submitted";
   }
   return View(m);
}

-Krip

P.S. credit

Categories: ASP.NET, MVC Tags: , , ,

Performance of in-line SQL vs. stored procedures

May 27, 2011 Leave a comment

Just been doing some load tests with .NET code against SQL Server 2008 R2 Developer edition via Data Access Application Block in Enterprise Library.

I insert 5,000 records into a table with a primary key on an Identity column and a clustered index on the same.  I’m setting 3 nvarchar fields and one smallint one, and then returning the Identity value of the newly added record.  The in-line SQL method calls SELECT SCOPE_IDENTITY() as the second statement in the batch and utilizes the ExecuteScalar() method while the stored proc path merely returns the same value and is called via the ExecuteNonQuery method.  Using caching as detailed below, I almost achieve 1,000 records per second on my developer machine.

I’m afraid I can’t help the stored procedure vs. in-line SQL debate as sometimes the in-line SQL is quicker and other times the stored proc is quicker!

However to get the greatest throughput (on both in-line and stored procs) be sure to cache (or reuse) the following:

  • Database object (from DatabaseFactory.CreateDatabase) – although caching this didn’t make a huge difference
  • DbCommand (needed for next item!)
  • ALL DbParameters – merely overwrite the values on subsequent calls instead of calling Database.AddInParameter

-Krip