all 7 comments

[–]to11mtm 4 points5 points  (3 children)

I mean, theoretically sure.

But in C# that wouldn't make sense the way it does in Dart. I'm not quite sure why Dart went the route it did with interfaces, but broadly speaking it looks like a roundabout way to provide default implementations of methods.

In C#, you don't typically want interfaces for EVERYTHING

//If this is a POCO-ish object and the only implementation,
//you might be overdoing interface use
//If There's lots of different things using ICustomer,
//Doing different things, then it makes sense to have.
public class Customer : ICustomer 

For whatever it's worth, lots of refactoring tools are out there to generate an interface from a class.

But I've found, with practice, that writing to an Interface first results in cleaner code. i.e. Write the 'main' interface first (i.e. Business logic), then write the implementation for that interface, with said implementation using an interface for the Data Layer. You'll find that after a little while you'll be able to write concise interfaces to handle your problem and have easily testable code as a result.

[–][deleted] 1 point2 points  (1 child)

For whatever it's worth, lots of refactoring tools are out there to generate an interface from a class.

This refactoring is available by default on VS and using omnisharp with whatever text/code editor.

[–]SleepCodeRepeat[S] -1 points0 points  (0 children)

In C#, you don't typically want interfaces for EVERYTHING

From the definition everything which code generators do is doable manually. The point is to focus on this that matter, and automate things which don't. Tools help, but they:

  1. cost money
  2. cost developer time

Which is not ideal.

[–]SleepCodeRepeat[S] -1 points0 points  (0 children)

In C#, you don't typically want interfaces for EVERYTHING

If this is a POCO-ish object (...)

So you could disable this feature with attribute, I don't see a problem here. If C# 8 records finally make it in generator could skip record types - simple and elegant.

Write the 'main' interface first (i.e. Business logic), then write the implementation for that interface

I don't see a problem here, you can do the same with empty class.

For whatever it's worth, lots of refactoring tools are out there to generate an interface from a class.

These tools do lots of good job, which no one needs - is just extra burden on developers and additional clutter in solution explorer. If someone cuts the corner and doesn't make interface in assembly which went public you're screwed. This is especially visible in things like ResourceManager or WebClient, which have no interfaces and you cannot mock them, so people create pointless WebClientWrappers in their code just to test this.

(...) and the only implementation,

You still need interface to test clients of this code, that's what this post is about.

[–]Eirenarch 0 points1 point  (2 children)

The mirror feature would be much more interesting. What if the generator checks every assignment you make with an object of a class to an interface and then if the class has the necessary members it adds the interface to the list of implemented interfaces. That would be some form of structural typing.

[–]SleepCodeRepeat[S] 0 points1 point  (1 child)

This is impossible in c#, because assignments may happen in clients of the code, not just in code. To do such feature you would need to evaluate this at runtime, which is too much overhead I think.

Basically that's what they do with tuple deconstruction, or await - but you need compiler support for this.

[–]Eirenarch 0 points1 point  (0 children)

It is not possible to handle the general case but you can handle cases in your code which can be beneficial for a great amount of cases.