Get Stored Procedure from Data Context : Linq to SQl

前端 未结 3 1155
旧时难觅i
旧时难觅i 2020-12-21 06:34

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.

3条回答
  •  春和景丽
    2020-12-21 06:51

    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 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);
         }
    
    }
    

提交回复
热议问题