服务层的设计与库在微软MVC [英] Design of Service Layer and Repositories in Microsoft MVC

查看:134
本文介绍了服务层的设计与库在微软MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题 - 或者说,对于提出宝贵意见的迫切需要 - 与微软的MVC。来自客户端的某个动作导致创建的:

I have the following problem - or rather, an urgent need for valuable advice - with Microsoft MVC. A certain action from the client leads to the creation of:


  • 在表格中的备注备注

  • 为HourRegistrations在表中的条目

  • 在更改日志买票的条目

我使用的业务操作和存储库的CRUD操作的服务层。问题是,我有时需要将对象从不同DataContexts连接,所以我想我用一个设计缺陷。最近,我们已经开始移除我们的控制器和存储库所有的业务逻辑,这是第一件事情我碰到之一。

I use a service layer for business actions and repositories for CRUD actions. The problem is that I at times need to connect objects from different DataContexts so I suppose I use a flawed design. Recently we have started to remove all business logic from our controllers and repositories and this is one of the first things I run into.

例如:

BLogic.AddRemarks(Ticket t, ...)
{
  Remark r = _remarksRepository.Create();
  r.Ticket = t;
  _remarksRepository.Add(r);
  _remarksRepository.Save();
}

这会触发kBOOM因为门票是使用存储库控制器牵强。于是备注r和票证T不共享相同的数据上下文。

This triggers kBOOM since the Ticket is fetched in the controller using the repository. So Remark r and Ticket t do not share the same data context.

我可以改变方法的签名,并提供一个int TicketId,但感觉不对。此外,我再进一步得到类似问题的路线。

I can alter the signature of the method and provide an int TicketId, but that doesn't feel right. Besides, I then get similar problems further down the line.

我的存储库在服务类的构造函数创建。也许我必须在方法的开始创建它们?即使这样,我必须经常转移,而不是真正的对象标识。

My repositories are created at the constructor of the service class. Perhaps I must create them at the start of a method? Even then, I must often transfer Ids instead of the true objects.

推荐答案

我的建议是使用dependeny注入(或控制反转 - 取决于你会怎么喜欢称呼它)。我用我自己的城堡windor。非常简单与mvc.net集成。 更多

My suggestion is to use dependeny injection (or inversion of control - depends how would you like to call it). I use myself castle windor. Really simple to integrate with mvc.net. read more

在国际奥委会和运行创造ContextManager。 Somethig是这样的:

When IoC is up and running create ContextManager. Somethig like this:

public class ContextManager : IContextManager
{
    private XContext context;

    public XContext GetContext()
    {
        return context ?? (context = XContext.Create());
    }
}

设置IContextManager生活方式perwebrequest和你有你自己,你可以从信息库和服务访问上下文。和它的每一个请求一样的。

Set IContextManager lifestyle as perwebrequest and you got yourself context that you can access from repositories and services. and it's same per one request.

修改

您也可以创建自己的ControllerFactory

You also have to create your own controllerFactory

然后你可以使用你的服务和存储库是这样的:

then you can use your services and repositories like this:

public class MyController : Controller
{
    public ISomeService SomeService { get; set; }
    public IContextManager ContextManager { get; set; }

...

}

您不必须对服务和资源库创建新实例,您可以管理从配置这些对象的生活方式。最合理的是单身

You dont have to create new instances for services and repositories and you can manage those objects lifestyle from configuration. Most reasonable would be singleton

这篇关于服务层的设计与库在微软MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆