简体   繁体   中英

Get Stored Procedure from Data Context : Linq to SQl

I have a stored procedure named ParseXML in SQL Server. I have a repository pattern using LINQ to SQL. I need to call the stored procedure from within the repository layer. Unlike GetTable method, we don't have a GetStoredProcedure method for data context. How can we call the stored procedure in such a scenario?

Dbml Code

[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.ParseXML")]

    public ISingleResult<ParseXMLResult> ParseXML([global::System.Data.Linq.Mapping.ParameterAttribute(Name="InputXML", DbType="Xml")] System.Xml.Linq.XElement inputXML)
    {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), inputXML);
        return ((ISingleResult<ParseXMLResult>)(result.ReturnValue));
    }

Repository Layer

namespace RepositoryLayer
{
public interface ILijosBankRepository
{
    System.Data.Linq.DataContext Context { get; set; }
    List<DBML_Project.BankAccount> GetAllAccountsForUser(int userID);
    void UpdateBankAccountUsingStoredProcedure();

}

public class LijosSimpleBankRepository : ILijosBankRepository
{
    public System.Data.Linq.DataContext Context
    {
        get;
        set;
    }


    public List<DBML_Project.BankAccount> GetAllAccountsForUser(int userID)
    {
        IQueryable<DBML_Project.BankAccount> queryResultEntities = Context.GetTable<DBML_Project.BankAccount>().Where(p => p.AccountOwnerID == userID);
        return queryResultEntities.ToList();
    }


    public virtual void UpdateBankAccountUsingStoredProcedure()
    {
        //Context.GetStroedProcedures();
    }

}

}

REFERENCE:

  1. Multiple UnitOfWorks, ISession and repositories

You can do something like this, calling the method using reflection:

var inputXML = GetXML(); 

var method = Context.GetType().GetMethod("ParseXML");

if(method == null) throw new InvalidOperationException("Defined DataContext does not have method ParseXML");

var result = method.Invoke(Context, new object[]{ inputXML });

If you are using c# 4.0, you can do:

var inputXML = GetXML(); 

dynamic dynamicContext = Context;

var result = (ISingleResult<ParseXMLResult>)dynamicContext.ParseXML(inputXML);

It's a pretty huge break of SOC to have any callers of your repository be aware of whether or not a particular method call results in reading text from a file, SQL statement, sprocs or even just garden gnomes typing results out on a text terminal.

To that end, it doesn't help matters to have your Context property be public. The whole point of using a repository is so that consumers are shielded from persistence concerns!

Since you seem to have a strong need to avoid using a custom-typed Context , you'd save yourself much trouble and just issue a straight-up, old-school SQL statement that will execute your sproc.

Consider refactoring your interface and logic to look more like this:

public interface ILijosBankRepository
{
    List<DBML_Project.BankAccount> GetAllAccountsForUser(int userID);
    void UpdateBankAccount(/* params go here */);
    /* ...other query methods, etc... */

}
public class LijosBankRepository : ILijosBankRepository
{
     private readonly DataContext context { get; set;}
     public LijosBankRepository(DataContext ctx) { ... }

     public void UpdateBankAccount(string inputXml)
     {
          context.ExecuteCommand("ParseXML", inputXml);
     }

}

The C# wrapper is part of your custom DataCcontext derived class. You would call like this:

public virtual void UpdateBankAccountUsingStoredProcedure()
{
    var results = Context.ParseXML(...);
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM