限制违反架构 - asp.net MVP

时间:2022-04-07 21:12:48

If we had a defined hierarchy in an application. For ex a 3 - tier architecture, how do we restrict subsequent developers from violating the norms?

如果我们在应用程序中有定义的层次结构。对于前三层架构,我们如何限制后续开发人员违反规范?

For ex, in case of MVP (not asp.net MVC) architecture, the presenter should always bind the model and view. This helps in writing proper unit test programs. However, we had instances where people directly imported the model in view and called the functions violating the norms and hence the test cases couldn't be written properly.

例如,对于MVP(不是asp.net MVC)架构,演示者应始终绑定模型和视图。这有助于编写适当的单元测试程序。但是,我们遇到的情况是人们直接在视图中导入模型并调用违反规范的函数,因此无法正确编写测试用例。

Is there a way we can restrict which classes are allowed to inherit from a set of classes? I am looking at various possibilities, including adopting a different design pattern, however a new approach should be worth the code change involved.

有没有办法可以限制允许哪些类从一组类继承?我正在研究各种可能性,包括采用不同的设计模式,但是新方法应该值得所涉及的代码更改。

4 个解决方案

#1


2  

I'm afraid this is not possible. We tried to achieve this with the help of attributes and we didn't succeed. You may want to refer to my past post on SO.

我担心这是不可能的。我们尝试在属性的帮助下实现这一点,但我们没有成功。你可能想参考我过去发表的帖子。

The best you can do is keep checking your assemblies with NDepend. NDepend shows you dependancy diagram of assemblies in your project and you can immediately track the violations and take actions reactively.

您可以做的最好的事情是继续使用NDepend检查程序集。 NDepend显示项目中程序集的依赖关系图,您可以立即跟踪违规并采取被动操作。

alt text http://www.ndepend.com/Res/NDependBig17.png

替代文字http://www.ndepend.com/Res/NDependBig17.png

#2


1  

It's been almost 3 years since I posted this question. I must say that I have tried exploring this despite the brilliant answers here. Some of the lessons I've learnt so far -

我发布这个问题差不多3年了。尽管这里有很好的答案,我必须说我已经尝试过探索这个问题。到目前为止我学到的一些课程 -

  1. More code smell come out by looking at the consumers (Unit tests are best place to look, if you have them).

    通过观察消费者可以得到更多的代码味道(单元测试是最好看的地方,如果你有的话)。

    • Number of parameters in a constructor are a direct indication of number of dependencies. Too many dependencies => Class is doing too much.
    • 构造函数中的参数数量是依赖项数量的直接指示。依赖性太多=> Class做得太多了。
    • Number of (public) methods in a class
    • 类中的(公共)方法数
    • Setup of unit tests will almost always give this away
    • 单元测试的设置几乎总是可以解决这个问题
  2. Code deteriorates over time, unless there is a focused effort to clear technical debt, and refactoring. This is true irrespective of the language.

    除非有集中精力清理技术债务和重构,否则代码会随着时间的推移而恶化。无论语言如何,都是如此。

  3. Tools can help only to an extent. But a combination of tools and tests often give enough hints on various smells. It takes a bit of experience to catch them in a timely fashion, particularly to understand each smell's significance and impact.

    工具只能在一定程度上起作用。但是工具和测试的组合通常会给各种气味提供足够的提示。及时捕捉它们需要一些经验,特别是要了解每种气味的重要性和影响。

#3


0  

You are wanting to solve a people problem with software? Prepare for a world of pain!

您想用软件解决人员问题吗?为痛苦的世界做好准备!

The way to solve the problem is to make sure that you have ways of working with people that you don't end up with those kinds of problems.... Pair Programming / Review. Induction of people when they first come onto the project, etc.

解决问题的方法是确保你有办法与那些你没有遇到这些问题的人一起工作....结对编程/评论。当人们第一次进入项目等时引导他们

Having said that, you can write tools that analyse the software and look for common problems. But people are pretty creative and can find all sorts of bizarre ways of doing things.

话虽如此,您可以编写分析软件并查找常见问题的工具。但人们非常有创意,可以找到各种奇怪的做事方式。

#4


0  

Just as soon as everything gets locked down according to your satisfaction, new requirements will arrive and you'll have to break through the side of it.

只要一切都根据您的满意度被锁定,新的要求就会到来,您将不得不突破它的一面。

Enforcing such stringency at the programming level with .NET is almost impossible considering a programmer can access all private members through reflection.

考虑到程序员可以通过反射访问所有私有成员,使用.NET在编程级别执行这样的严格性几乎是不可能的。

Do yourself and favour and schedule regular code reviews, provide education and implement proper training. And, as you said, it will become quickly evident when you can't write unit tests against it.

做好自己,支持并安排定期的代码审查,提供教育并实施适当的培训。并且,正如您所说,当您无法针对它编写单元测试时,它将很快变得明显。

#1


2  

I'm afraid this is not possible. We tried to achieve this with the help of attributes and we didn't succeed. You may want to refer to my past post on SO.

我担心这是不可能的。我们尝试在属性的帮助下实现这一点,但我们没有成功。你可能想参考我过去发表的帖子。

The best you can do is keep checking your assemblies with NDepend. NDepend shows you dependancy diagram of assemblies in your project and you can immediately track the violations and take actions reactively.

您可以做的最好的事情是继续使用NDepend检查程序集。 NDepend显示项目中程序集的依赖关系图,您可以立即跟踪违规并采取被动操作。

alt text http://www.ndepend.com/Res/NDependBig17.png

替代文字http://www.ndepend.com/Res/NDependBig17.png

#2


1  

It's been almost 3 years since I posted this question. I must say that I have tried exploring this despite the brilliant answers here. Some of the lessons I've learnt so far -

我发布这个问题差不多3年了。尽管这里有很好的答案,我必须说我已经尝试过探索这个问题。到目前为止我学到的一些课程 -

  1. More code smell come out by looking at the consumers (Unit tests are best place to look, if you have them).

    通过观察消费者可以得到更多的代码味道(单元测试是最好看的地方,如果你有的话)。

    • Number of parameters in a constructor are a direct indication of number of dependencies. Too many dependencies => Class is doing too much.
    • 构造函数中的参数数量是依赖项数量的直接指示。依赖性太多=> Class做得太多了。
    • Number of (public) methods in a class
    • 类中的(公共)方法数
    • Setup of unit tests will almost always give this away
    • 单元测试的设置几乎总是可以解决这个问题
  2. Code deteriorates over time, unless there is a focused effort to clear technical debt, and refactoring. This is true irrespective of the language.

    除非有集中精力清理技术债务和重构,否则代码会随着时间的推移而恶化。无论语言如何,都是如此。

  3. Tools can help only to an extent. But a combination of tools and tests often give enough hints on various smells. It takes a bit of experience to catch them in a timely fashion, particularly to understand each smell's significance and impact.

    工具只能在一定程度上起作用。但是工具和测试的组合通常会给各种气味提供足够的提示。及时捕捉它们需要一些经验,特别是要了解每种气味的重要性和影响。

#3


0  

You are wanting to solve a people problem with software? Prepare for a world of pain!

您想用软件解决人员问题吗?为痛苦的世界做好准备!

The way to solve the problem is to make sure that you have ways of working with people that you don't end up with those kinds of problems.... Pair Programming / Review. Induction of people when they first come onto the project, etc.

解决问题的方法是确保你有办法与那些你没有遇到这些问题的人一起工作....结对编程/评论。当人们第一次进入项目等时引导他们

Having said that, you can write tools that analyse the software and look for common problems. But people are pretty creative and can find all sorts of bizarre ways of doing things.

话虽如此,您可以编写分析软件并查找常见问题的工具。但人们非常有创意,可以找到各种奇怪的做事方式。

#4


0  

Just as soon as everything gets locked down according to your satisfaction, new requirements will arrive and you'll have to break through the side of it.

只要一切都根据您的满意度被锁定,新的要求就会到来,您将不得不突破它的一面。

Enforcing such stringency at the programming level with .NET is almost impossible considering a programmer can access all private members through reflection.

考虑到程序员可以通过反射访问所有私有成员,使用.NET在编程级别执行这样的严格性几乎是不可能的。

Do yourself and favour and schedule regular code reviews, provide education and implement proper training. And, as you said, it will become quickly evident when you can't write unit tests against it.

做好自己,支持并安排定期的代码审查,提供教育并实施适当的培训。并且,正如您所说,当您无法针对它编写单元测试时,它将很快变得明显。