Where to put Delgates in .Net Solution

I have a C # .Net solution suite that has become a proof of concept and has grown to almost 15 different projects. Currently, I am rewriting the entire family of products and trying to make the best use of the best methods with the future organization.

I did some research and itโ€™s still not clear to me what is the best way to keep delegates in a multi-project product to minimize the likelihood of future refactoring.

I would suggest that the general consensus is that you place them anywhere, which makes sense for use, but overall I think there is a better way to structure everything in the solution to minimize problems, increase positive modularity and facilitate maintenance.

In many cases, it is recommended to travel to where they are used; presumably declared in the namespace above the class file, which in my case will translate into my shared library with common interfaces that define events. I currently have delegates in my own file in the delegate namespace. Is there any oversight that I do in doing this, or am I abusing the use case for delegates - or is it related to current expectations of use?

I did a typical search, but did not find anything reliable and significant; however, this may be due to poor keyword selection.

+6
source share
1 answer

I would suggest that the general consensus is that you post them anywhere it makes sense to use, but overall I feel that there is a better way to structure everything in the solution to minimize problems, promote positive modularity and facilitate maintenance.

Well, yes, in the general case, you can just put everything anywhere. However, problems begin when you work as a team. As a team, other people need to understand the structure of the program in order to be able to efficiently create code. This implies consistency, which is the ultimate goal.

So this is not just a matter of intuition; In all the companies I worked on, I started by writing a coding standard and persistently applied it everywhere. In this coding standard, I usually put a ton of good / bad methods related to threads, static use, placing variables / classes, etc.

The link http://se.inf.ethz.ch/old/teaching/ss2007/251-0290-00/project/CSharpCodingStandards.pdf and https://msdn.microsoft.com/en-us/library/ff926074 will open . aspx will give you a start, although the first is a bit outdated and both do not answer your question. However, it can help with what you are trying to achieve in the end.

[...] I currently have delegates in their own file in the delegate namespace. Is there oversight when I am doing this, or am I abusing the use case for delegates - or is it due to current expectations of use?

A shared library with interfaces makes sense. An interface is essentially a contract between a โ€œclientโ€ and a โ€œserver,โ€ which is an important advantage for decoupling a piece of software.

At this point, you should understand that part of the interface is a functional decomposition, not a technical one. In other words: the fact that you need an interface library is a matter of contract, not technical designs. So I donโ€™t understand why you ever put classes in the classes folder and why you put delegates in the delegates folder. In short, the "delegates" folder does not make any sense to me. Place the material where it belongs.

At this point you must make a decision. (1) You can put the delegate in one file, (2) you can put the delegate in the class file in which it was used (I usually use delegates for the same purpose), (3) you can do both depending on one / multitasking and (4) you can designate single-delegates nested in a class. This basically means that you consider the delegate as a description of a function pointer and do not see it as a real "class".

Looking at functional decomposition, you can argue that a pointer to a function belongs to a class, like methods.

So, to lure. Personally, I usually choose option (2) or (3), because it is short, understandable and concise, and does not affect the amount of "typing of work", like (4). Choosing (1) will probably inflate your application with small files that have little or no purpose, which will confuse other developers.

+2
source

Source: https://habr.com/ru/post/988361/


All Articles