English 中文(简体)
避免与服务和DI发生循环联系
原标题:Avoid circular references with services and DI

我有多门服务班。 每个班组都有一套相关的服务方法。 这些服务是利用IoC和建筑注射器进行的。

If I have two service classes that may, at times, need to call methods in the other, what is the best way to handle these in order to avoid circular references?

例如,从两个不同的服务类别(不一定是最实际的例子,但为了简单起见):

public class UserService
{
    public void RegisterUser(User user)
    {
        // Do a bunch of stuff needed to register a user

        // Now call out to permission service to set up basic permissions
        _permissionService.SetUpBasicPermissions(user);
    }
}

public class PermissionService
{
    public void GrantPermission(User user, int PermissionId)
    {
        // Add user permission to database

        // Now call out to user service to do some other stuff on the user
        _userService.TakeActionsOnUserAccount(user);
    }
}

以下是我可以看到的备选办法:

  1. 考虑到这两个部门之间需要共享功能,这是将服务类别合并为一个类别的一个标志。 如果情况是这样,我是否最终会有一个庞大的服务类别? 如何区分?

  2. 捐助将两个班子结合起来,但将具有逻辑性的方法移到自己的服务班级(在他们各自的班级中为用户和许可提供具体逻辑)。 如果是这样的话,我最后可能要提供这些共享服务,我认为这不是一件好事。

  3. 任何需要从一个部门到另一个部门的行动都应当移至呼吁守则。 这对我来说似乎有问题,因为我将消除可能依赖的逻辑,并使它成为另一个层次的关切。 换言之,我一直称之为“用户服务”。 注册用户(注册用户)——现在我总是必须称职。 SetUpBasicPermissions () Immediate following each calls.

  4. Allow one service to call the other, but not both. If this is the case, which one do I choose? What do I do w/ the logic for the one that can t call the other?

  5. 利用代表将依赖性服务方法引入所谓的服务方法。 Bleh,这种声音是很荒谬的。

这些是我可以想象的,但我确信,有一些我looking。

Thoughts?

提前感谢。

最佳回答

如果你想把这两种服务相互参照,那么你就需要使用财产注入(或方法),而不是建筑,以便在将财产注入到其他类别之前能够充分创造。

在设计一个服务时,在某些情况下,如果业务的合理流动是明智的,那么这种服务就毫无意义。 尽管试图将它保持在最低水平是更好的设计,因为越来越多的依赖者会增加政变,必须在单位测试中更加仔细地规划行为。

问题回答
  1. The SRP principles states that every object should have a single responsibility so it s not ok to merge the classes.
  2. That s way inheritance way invented, use an abstract classes for common code and specialize after (don t use references)
  3. Dependency logic is bad, RegisterUser should do only that Register, not give Permissions. It s ok to move the dependency to the calling layer.
  4. Do this only if you must, it should be a bit clear after you make the abstrat class. Try object composition though.
  5. It s called a container, and you should understand it better after you read: http://www.potstuck.com/2009/01/08/php-dependency-injection

关于通知提及,OOP原则只表示避免抽象和具体类别(Hollywood 原则)之间的循环提及,但情况并非如此。 就像你一样,这 s。

然而,你正在使用非常可靠的注射方法,以便你能够提取《基本药物清单》,将其排除在这一类别之外。

objUser = new User();
objUserService.RegisterUser(objUser);
objPermissionService.SetUpBasicPermissions(objUser);




相关问题
DDD - Returning entity in response to a service operation

I am new to domain driven development & have a simple question. If a service needs to generate some entity as a response to an operation then how should it be done? One of the ways is to inject ...

Domain Driven Design efforts in dynamic languages? [closed]

Are you aware of any DDD efforts in a dynamic language ? Practical resources on DDD tend to decrease quite dramatically when straying from enterprise-oriented solutions (a google search exluding C#, ....

Accessing domain objects in the view

If I don t want to expose the internal state of my Domain Objects, but I need to display them, I can think of three approaches. Which of these is the most "correct" (if any?). The "DTO/ViewModel ...

DDD screen cast question?

I wathced a screen cast on DDD by Greg Young the other day which spoke about persisting all state transitions of an object, instead of it s state when saved, then to load it "replay" all these ...

How to fluent-map this (using fluent nhibernate)?

I have two tables in my database "Styles" and "BannedStyles". They have a reference via the ItemNo. Now styles can be banned per store. So if style x is banned at store Y then its very possible that ...

热门标签