views:

184

answers:

1

I'm following Steve Sanderson's example from this ASP.NET MVC book on creating a model by hand instead of using diagramming tools to do it for me. So in my model namespace I place a class called MySystemModel with something like the following in it

[Table(Name="tblCC_Business")]
public class Business
{
  [Column(IsPrimaryKey=true, IsDbGenerated=false)]
  public string BusinessID { get; set; }

   // this is done because Business column and Business have interfering names
  [Column(Name="Business")] public string BusinessCol { get; set; }
}

This part of it is all fine. The problem however is returning multiple result sets from a stored procedure, but mixing and matching SQL with LINQ modelling. We do this because the LINQ to SQL translation is too slow for some of our queries (there's really no point arguing this point here, it's a business requirement). So basically I use actual SQL statements along with my LINQ models in my "repository" like so:

public IEnumerable<MyType> ListData(int? arg)
{
    string query = "SELECT * FROM MyTable WHERE argument = {0}";

    return _dc.ExecuteQuery<MyType>(query, arg);
    //c.GetTable<MyType>(); <-- this is another way of getting all data out quickly
}

Now the problem I'm having is how to return multiple result sets as I'm not extending DataContext, like so:

public ContractsControlRepository()
{
  _dc = new DataContext(ConfigurationManager.ConnectionStrings["MyConnectionString"].ToString());
}

This link describes how multiple result sets are returned from stored procedures.

[Function(Name="dbo.VariableResultShapes")]
[ResultType(typeof(VariableResultShapesResult1))]
[ResultType(typeof(VariableResultShapesResult2))]
public IMultipleResults VariableResultShapes([Parameter(DbType="Int")] System.Nullable<int> shape)
{
    IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), shape);
    return ((IMultipleResults)(result.ReturnValue));
}

So how do I turn this into something that can be used by my repository? I just need to be able to return multiple result sets from a repository which contains DataContext, and doesn't extend it. If you copied and pasted the previous extract into a repository like I've got it will just state how ExecuteMethodCall isn't available, but that's only available if you extend DataContext.

Resources

Guy Berstein's Blog

A: 

Every time I ask a question that has been hindering me for days on end I end up finding the answer within minutes. Anyway, the answer to this issue is that you have to extend DataContext in your repository. If like me you're worried about having to specify the connection string in every single controller then you can change the constructor in the repository class to something like this:

public ContractsControlRepository()
      : base(ConfigurationManager.ConnectionStrings["AccountsConnectionString"].ToString()) { }

This way when you instantiate your repository the connection is set up for you already, which gives you less to worry about, and actually centralizes specifying the connection string. Extending DataContext also means you have access to all of the protected methods such as ExecuteMethodCall used for calling stored procedures and bringing back, if you will, multiple result sets.

Kezzer