Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
In our project we are using a CQS pattern. Because some of our queries call other webservices, which in our case can be slow, we want to cache those results. Now I've found several solutions how to do this:
Create an CachedQuery which inherits from a Query object.
Add a 'CacheResult' attribute to the queryhandler, have a decorator check for the presence of this attribute
Add a 'CacheResult' attribute to the query, have a decorator check for the presence of this attribute
Implement a 'ICacheableQuery' interface, have a decorator check for the implementation of this interface.
option 1 is discarded because of "composition over inheritance'
option 2 is not really flexible, e.g. it is not possible to not cache
But how to choose between 3 and 4? Adding an attribute is not inheritance (or is it?) so that is equal. Both are flexible enough, at least for now.
Do I miss some convincing argument? Or is it a matter of personal preference, if so what would you choose to do?
Adding an attribute is not inheritance (or is it?)
Whether you want your custom attribute to be inherited is up to you to implement. When you write code that checks the presence of an attribute, you can write it so that it checks the base classes/interfaces for the attribute as well.
As a commenter already noted, attributes allow you to add metadata that could be useful depending on what you're trying to implement.
Generally, I dislike having interfaces without methods just for the sake of differentiating types. But in the end...
Is it a matter of personal preference
Yes.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
We all know that interfaces and abstract classes are needed for many design principles, and were told to be "the best practice" of programming, both for maintenance and expansion purposes.
But I also heard people saying, "Do NOT overuse interfaces or abstract classes"!
So then, how do I "draw the line"?
I know the difference between interface and abstract class (kind of).
So I'm not interested in "when to use which?", I'm interested in "when is too much"?
For example, the example given by r/PiggyChu620 from this post is clearly "overengineering" the interfaces, as r/AlarmedSlide1 down below puts it.
So to avoid the same mistake r/PiggyChu620 did, is there any "borderline guideline" as to "when should I use interface/abstract class and when should I not"?
Thank you very much for your help!
It's actually pretty easy.
If you can delete an interface from your code (maybe replace it with a specific class when it's used as a parameter) and your code still works, it wasn't needed.
As with every tool, the answer to the question "when should I use it" is "when you need too". If you use it "just because", then you probably make your program simpler (and therefor more maintainable) when you don't use it at all.
Interfaces, Base classes and OOP are important because you are solving problems with it. If you find yourself doing it without solving an actual problem with it, I would consider it "overused".
Interfaces are contracts that are signed between classes, and all classes that use it are bound by the rules of the interface.
This type of design makes it easier to reuse and increases maintenance.
Suppose you create a vehicle interface. All vehicles must implement this interface, including bicycles, motorcycles, cars, etc.
This style of design creates blood and creates a beautiful and principled architecture for your program
So if you have classes that behave similarly, you can use interfaces or abstract classes.
You can refer to this link to compare abstract classes and interfaces
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am new to unit testing so my questions might seem basic, apologies for that.
I am trying to figure out if there is such API in MOQ that can help me to modify a method logic.
I mean,
When calling . I want to increase lets say my "count" variable by 1 and then call the method or do something else, doesn't really matter.
There is no code yet, this is a principle question. Could not find it in the MOQ Documentation on GitHub.
Hope I was clear and you can help me with that.
Thank you!
You can't 'modify' the method logic per se, but you can replace it entirely.
When you create a mock of an interface, you aren't instantiating a specific concrete type of that interface. Instead, you are allowing your mocking framework to create a 'mock' version of that interface; an object which, by default, has no functionality but requires no work to instantiate.
The easiest way to do this is to use Moq's Setup functionality, which is explained well here:
https://github.com/Moq/moq4/wiki/Quickstart
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I know that C# does not currently support this, so this is more of a meta-C# question. I have been thinking of what changes would be required in the language to allow us to create our own commands like while or using. I could create a method accepting an Action, like Task.Factory.StartNew(), but then I would need parentheses around the curly braces and the syntax would not look the same.
Would this essentially amount to allowing us to apply parameters outside of the parentheses?
i.e.
public void MyUsing (IDisposable disposable) Action body
{
try
{
body();
}
finally
{
disposable.Dispose();
}
}
Commands like for, switch and if would be even more complicated to create because of their syntax.
I recommend you to take a look at the Roslyn project (which is essentially an opensource C# compiler written in C#!)
They also have demos of how easy it is to add language features.
https://roslyn.codeplex.com/
However, take note that it is unlikely for your changes to be accepted unless you are working in the C# team. If you want to have code generated for you, you could take a look at PostSharp, it isn't the same thing as you were suggesting, but it might suit you situation anyway.
http://www.postsharp.net/
PostSharp allows you to mark methods, properties, classes etc. with Attributes which will then trigger user defined code generation functions which will run after the initial compile process. It helps in situations where you don't want to copy paste the same code pattern over and over again.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Assuming I was writing my own version of .Net :)
What would be the downside of such a setup?
Yes, I am talking about a new anti-pattern here to avoid creating endless tuples and EventArgs. I think such a setup would have made coding a lot cleaner.
No. The Tag property has history, it was important in VB6 and Winforms was meant to replace it. It needed to be added to make porting code relatively simple.
It is entirely unnecessary in .NET. It supports implementation inheritance, a feature that VB6 didn't have. So if you want to add extra properties then you just derive a class and add them. And you'll be able to give them a good name and a type so you don't have to cast every time you read the property. This works just as well with Winforms controls.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
due to unit-testing we create for every class an Interface. The .Net Framework coding standards say that every class, interface, enum, etc. should be located in a different file.
As these interfaces are so closely related with the class we were thinking of creating an internal coding-standards rule to put together the class and the interface.
Have you seen this approach before? What do you think about it?
PD: Always talking about interfaces used only to mock the classes, not 'real' interfaces that can have more than one implementation.
You should follow .NET coding standards and separate the interfaces into their own files. You could create a folder Interfaces within your project. I usually have Concrete, Abstract and Interfaces folders within my projects.
Developers who may be unfamiliar with your solution will have a hard time finding interfaces if they are in class files.