需要调用实体内部的存储过程

发布于 2024-10-20 06:17:14 字数 1331 浏览 5 评论 0原文

我的服务中有这个公共操作:

    void IApplicationService.DeleteApplication(int id)
    {
        var repository = UnitOfWork.CreateRepository<Application>();

        var application = repository.GetByKey(id);
        OnCommit.Run(() => DeleteInvitations(id)); // ouch!!
        repository.Delete(application);
    }

OnCommit.Run() 安排一个操作在我调用 SaveChanges() 时运行,该操作在 WCF 检查器中实现。 DeleteInvitations 可以访问对象上下文并调用存储过程来删除集合 application.Invitations 中的每个项目。我这样做是为了避免将所有邀请加载到内存中,然后在下一步中删除它们。我的 Repository.Delete() 依次调用实体中的 Delete() 。

但我不太喜欢由服务代码处理部分删除。我需要访问我的实体内的存储过程。

我想我可以创建一个 IStoredProcedures 接口并将其提供给每个实体。然后我可以这样说:

    public void Delete()
    {
        Contract.Requires(EntityState == System.Data.EntityState.Unchanged);

        if (Responses.Any())
            throw new ValidationException("There are responses.");

        // *..* relationship, just break that
        for (int i = Forms.Count - 1; i >= 0; i--)
        {
            var form = Forms.ElementAt(i);
            Forms.Remove(form);
        }

        // I could have something like this, perhaps?
        StoredProcedures.Call("DeleteInvitations", this.Id);
    }

你对此有何感想?有什么建议吗?

我提出的一些要求:

  • 服务代码应仅调用存储库和实体方法
  • 存储库应是通用的
  • 实体不应访问对象上下文

I have this public operation in my service:

    void IApplicationService.DeleteApplication(int id)
    {
        var repository = UnitOfWork.CreateRepository<Application>();

        var application = repository.GetByKey(id);
        OnCommit.Run(() => DeleteInvitations(id)); // ouch!!
        repository.Delete(application);
    }

OnCommit.Run() schedules an action to run when I called SaveChanges(), implemented in a WCF inspector. DeleteInvitations has access to an object context and invokes a stored procedure to delete every item in collection application.Invitations. I made this way to avoid loading all invitations in memory only to delete them in the next step. My Repository.Delete() in turn calls Delete() in the entity.

But I don't quite like having part of the delete being handled by service code. I need to have access to stored procedures inside my entities.

I think I could make an IStoredProcedures interface and provide it to each entity. Then I could have something like this:

    public void Delete()
    {
        Contract.Requires(EntityState == System.Data.EntityState.Unchanged);

        if (Responses.Any())
            throw new ValidationException("There are responses.");

        // *..* relationship, just break that
        for (int i = Forms.Count - 1; i >= 0; i--)
        {
            var form = Forms.ElementAt(i);
            Forms.Remove(form);
        }

        // I could have something like this, perhaps?
        StoredProcedures.Call("DeleteInvitations", this.Id);
    }

What do you feel about it? Any suggestions?

Some requirements I've put:

  • Service code shall call only repository and entity methods
  • Repository shall be generic
  • Entities shall not have access to object context

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唔猫 2024-10-27 06:17:14

我没有看到问题,你能解释一下你想做什么吗?

您可以将存储过程添加到实体模型中,并像任何其他方法一样调用它或/并且您可以像这样调用过程:

string sp = "Entities.StoredProcedureName";
var conn = (EntityConnection)dbContext.Connection;
conn.Open();
EntityCommand cmd = new EntityCommand(sp, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("Parameter1", [type]).Value = paramvalue;
cmd.ExecuteNonQuery();

如果您愿意,可以使此执行通用并动态传递存储过程名称和参数。

我希望我明白你想要什么。

I don't see problem, can you please explain what you want to do.

You can add stored procedures to your entity model, and call it like any other method or/and you can call procedure like this:

string sp = "Entities.StoredProcedureName";
var conn = (EntityConnection)dbContext.Connection;
conn.Open();
EntityCommand cmd = new EntityCommand(sp, conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("Parameter1", [type]).Value = paramvalue;
cmd.ExecuteNonQuery();

You can make this execution generic if you want and dynamically pass stored procedure name and parameters.

I hope i understand what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文