从数据上下文获取存储过程:Linq 到 SQl
- 作者: 别停下脚步幸福
- 来源: 51数据库
- 2022-12-13
问题描述
我在 SQL Server 中有一个名为 ParseXML 的存储过程.我有一个使用 LINQ to SQL 的存储库模式.我需要从存储库层中调用存储过程.与 GetTable 方法不同,我们没有用于数据上下文的 GetStoredProcedure 方法.这种情况下如何调用存储过程?
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?
数据库代码
[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));
}
存储层
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();
}
}
}
参考:
- 多个 UnitOfWorks、ISession 和存储库
推荐答案
让存储库的任何调用者知道特定方法调用是否导致从文件、SQL 读取文本是 SOC 的一个巨大突破语句、sprocs 甚至只是花园侏儒在文本终端上输入结果.
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.
为此,将 Context 属性设为公开无济于事.使用存储库的全部意义在于让消费者免受持久性问题的困扰!
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!
由于您似乎非常需要避免使用自定义类型的 Context,您可以省去很多麻烦,只需发出一个直接的、老式的 SQL 语句即可执行你的程序.
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);
}
}
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
