Restrict violation of architecture - asp.net MVP

浪子不回头ぞ 提交于 2020-01-14 09:52:05

问题


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.

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.


回答1:


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.


(source: ndepend.com)




回答2:


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 -

  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.
    • 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:


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:


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.

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.



来源:https://stackoverflow.com/questions/2718864/restrict-violation-of-architecture-asp-net-mvp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!