Desktop App dev guidance needed

That is it. Now we just need to wire up all the pieces using DI. The Program.cs is what is known as the Composition Root. This is where we wire up everything.

To use the mock data source we code Program.cs as follows:

Code:
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      ICoinInfoService coinInfoService = new MockCoinInfoService();

      CoinInfoViewModel viewModel = new CoinInfoViewModel(coinInfoService);

      frmCoinInfo form = new frmCoinInfo(viewModel);
      viewModel.InitialView();

      Application.Run(form);
    }
  }

When you run the app it will display the sample data.

So now we want to display the data from the live source.
We do not need to change anything apart from adding a new implementation for the ICoinInfoService that will get the data from the web:

Code:
  public class WebServiceCoinInfoService :
    ICoinInfoService
  {
    public IList<CoinInfoDto> GetTopCoins()
    {
      List<CoinInfoDto> coins = new List<CoinInfoDto>();

      HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://coindata.co.za/api.php");
      request.Method = "GET";

      using (WebResponse webResponse = request.GetResponse())
      {
        using (Stream responseStream = webResponse.GetResponseStream())
        {
          if (responseStream == null)
            throw new InvalidOperationException("No response stream");

          using (StreamReader responseStreamReader = new StreamReader(responseStream))
          {
            string jsonResult = responseStreamReader.ReadToEnd();

            return JsonConvert.DeserializeObject<List<CoinInfoDto>>(jsonResult);
          }
        }
      }


      return coins;
    }
  }


To now finally run the app with the live data:
change this line in Program.cs:

Code:
      ICoinInfoService coinInfoService = new WebServiceCoinInfoService();
Boom and that is it.

So we have completely separated the various layers and concerns and used SOLID principles:

S: Single Responsibility Principle. Each of the classes has one responsibility. The form only does presentation. The ViewMoel gets data and notifies the view that there is new data. The WebService class gets data from the web ect ect.

O: Open Closed Principle. To make changes to the app from displaying mock data to displaying live data, I did not have to change any working code expect for the Composition Root where the components are wired up. I.e. the other classes are Closed for changes. Especially the form and viewmodel. Nothing else needed to change.

L: Liskov's Substitution Principle. I could exchange the mock service and live web service without an issue as they adhered to the same interface contract, the ICoinInfoService .

I: Interface Segregation. Not used here

D: Dependency Injection. I used DI to ctor-inject services into the classes that depended on them. This of course allowed me to inject any compatible service interchangably without changing any code except the wiring up.

This looks like a lot of work for a little app like this but it illustrates the principles. And it is actually not that much work. Took me 10 mins. You just have to plan and get into that mindset. When you have an enterprise level app, these techniques become very important to keep your app extensible. It also allows easy mocking for testing.

In the case where I develop WM6 apps, this works like a charm as all code is shared between WM6 and Windows desktop. Hence I can write and debug all code on desktop without having the painfully slow Code/Deploy/Run/Debug on the device. The only diff between the 2 code bases is the actual form that is laid out for the device instead of the desktop.

Now when you start using DI and dynamically loaded assemblies, that is where it starts to get awesome. Support for different hardware device protocols, easy support for integration with e.g. financial systems, etc etc...

The biggest take-away from this is that SOLID is not specific to WinForms or C#. These techniques can be applied to all stacks and languages.

In the case of MVC with typical WebApi and web page controllers, the real work is deferred to services and domain classes. It is not the controllers' responsibility to do the work. The MVC framework and controllers' only responsibility is to marshall the web request and the routing into a code entry point. Typically the controller should contain minimal code and use DI to get access to a service to do the real work.

Project files VS2015 at https://www.dropbox.com/s/zbfk9ia3vq4e6bo/MyBBCoin.zip?dl=0
Hey [MENTION=375958]Spacerat[/MENTION] thank you so much for this, as soon as I have my coffee in I'm going to open this up in visual studio and see if I can grasp it.

It's like you you say, it looks like a metric ton of code vs the equivalent in php, but I understand what you did.

I am between C# and Java for my next language that I want to learn.
 
This makes sense. I have Java and I have NetBeans.

When I I searched for JavaFX the oracle website said its replaced with open something (just did a quick search few days ago).

Is JavaFX still current?

Can you guys maybe give me what you believe to be a good tutorial for Java (I've used it 7 years ago, so forgotten most of it.)

Maybe I should buy a Lynda tutorial for Java first.
Yes re JavaFX is still relevant -- not only that it's actively being extended as of Java 9 e.g. Support for GTK3+ for Linux, ...

As for the confusion that relates to scenebuilder which Oracle deprecated in own release of that -- but even that isn't an issue because Gluon took over the development / upkeep of scenebuilder for JavaFX, you can download the latest release from here: https://gluonhq.com/products/scene-builder/
 
[)roi(];21890533 said:
...and for [MENTION=304573]Thor[/MENTION] JavaFX employs a derivative of CSS that would be easy to pick up, and the XML in the FXML files is no more challenging to edit than an HTML file, and as you mentioned one can always fall back on Scenebuilder.

Not that I'm punting this over .Net alternatives or any other. Simply use what makes sense in the particular context and / or what you're comfortable most productive with.

Yeah I forgot to mention this. Good catch [MENTION=8711][)roi(][/MENTION].
 
[)roi(];21890909 said:
Yes re JavaFX is still relevant -- not only that it's actively being extended as of Java 9 e.g. Support for GTK3+ for Linux, ...

As for the confusion that relates to scenebuilder which Oracle deprecated in own release of that -- but even that isn't an issue because Gluon took over the development / upkeep of scenebuilder for JavaFX, you can download the latest release from here: https://gluonhq.com/products/scene-builder/
SceneBuilder is such a blessing in the javafx world .
 
Hey [MENTION=375958]Spacerat[/MENTION] thank you so much for this, as soon as I have my coffee in I'm going to open this up in visual studio and see if I can grasp it.

It's like you you say, it looks like a metric ton of code vs the equivalent in php, but I understand what you did.

I am between C# and Java for my next language that I want to learn.
Keep in mind that Interfaces and Constructors methods are not the only any to do dependency injection and mocks.

Personally I prefer not to design it this way because that style of DI is very boilerplate heavy, and new dependencies not only require more interfaces but also changes to the constructor methods, among others. There are far lighter (less code) and easier to maintain alternatives.
 
Last edited:
SceneBuilder is such a blessing in the javafx world .
Agreed it's an easy to learn and use UI toolkit; but practically many things are just quicker to mod directly in the FXMLs
 
Hey [MENTION=375958]Spacerat[/MENTION]
It's like you you say, it looks like a metric ton of code vs the equivalent in php, but I understand what you did.

Well the actual code that does stuff is very little. There is more 'declarative' code than 'functional' code because of the small nature of the app. Also because we did separation of concerns. The balance will tip once you add 'functional meat'.

I could do an alternative to show you with how little code you can get away with. But that would not use any good practices

[)roi(];21890961 said:
Keep in mind that Interfaces and Constructors methods are not the only any to do dependency injection and mocks.

Personally I prefer not to design it this way because that style of DI is very boilerplate heavy, and new dependencies not only require more interfaces but also changes to the constructor methods, among others. There are far lighter (less code) and easier to maintain alternatives.
Can you expand?

I am aware that you can DI using property or method injection. Property injection is my last resort. It causes temporal coupling which is bad. It means that I have to set a property before the class is fully satisfied in terms of its dependencies. If I forget to set the property, then the class may fail in the ctor or somewhere else down the line. Just looking from the class from the outside does not tell me the full story about what dependencies are required inside.

Constructor injection does not mean you can only inject interfaces. You can obviously inject concrete implementations using (abstract or not) base class types instead of interfaces. I just chose an interface because I wanted to inject a mock. But it is your choice based on what you want to achieve and the specific scenario. Constructor injection is actually good because it advertises the class' dependencies through the ctor parameters. By simply looking at the ctor you know what dependencies it needs and you then inject them. It also forces you to inject those dependencies in order to instantiate the class. That is actually a good thing. Injecting a ServiceLocator is way less code but actually an anti-pattern (bad). There is lots written about why it is bad.

New dependencies do not require new interfaces. Inject concrete implementations.

The fact that a class ctor signature changes when it needs a new dependency is actually a good thing. When you change a class ctor, the compiler will tell you where to add the new dependency in the ctor call chain. If you added property injection you run a real risk of missing setting the property somewhere in your code where you used the class. And this will cause your code to likely fail at some point. The dreaded object reference exception...
 
Last edited:
[)roi(];21890961 said:
Keep in mind that Interfaces and Constructors methods are not the only any to do dependency injection and mocks.

Personally I prefer not to design it this way because that style of DI is very boilerplate heavy, and new dependencies not only require more interfaces but also changes to the constructor methods, among others. There are far lighter (less code) and easier to maintain alternatives.

Would you care to elaborate? :)
 
[)roi(];21890909 said:
Yes re JavaFX is still relevant -- not only that it's actively being extended as of Java 9 e.g. Support for GTK3+ for Linux, ...

As for the confusion that relates to scenebuilder which Oracle deprecated in own release of that -- but even that isn't an issue because Gluon took over the development / upkeep of scenebuilder for JavaFX, you can download the latest release from here: https://gluonhq.com/products/scene-builder/

Thanks for that, I will check that link out and read up on scenebuilder.

This is what I got the last time I googled for JavaFX that confused me: https://www.oracle.com/technetwork/java/javase/overview/javafx-overview-2158620.html
 
Would you care to elaborate? :)
I probably need to do a separate thread on this, because it's best explained by working through an example that explores the code required to build interface oriented DI, including what is involved with adding features -- e.g. How many changes are required to accommodate this.

A different take on the testing paradigm is based on separation of concerns by way of pure functions where possible; in practice this covers a large portion of a typical codebase.

For the remainder of the mutable state, there's two solutions I actively employ either:
  • Partial application of functions I.e. The dependencies are passed in by way of partially applied arguments of a function, linking to a type structure that contains either mutable application state or a State / Action store.
  • Alternative and considerably easier solution is a singleton, but designed specifically to enable mutable mocking.
 
Last edited:
[)roi(];21891035 said:
Agreed it's an easy to learn and use UI toolkit; but practically many things are just quicker to mod directly in the FXMLs

Wait, is scenebuilder a paid for thing?
 
Wait, is scenebuilder a paid for thing?
Nope there's two options, 1 is free and covers the full extent of JavaFX. The paid Gluon stuff relates to their custom JavaFX controls, ...
 
My next question.

Is JAVA fast?
My personal experience have been that Java is sluggish and very slow.
How did I test this?
I implement ERP systems and with Adaxa and ADempiere I alwasy found Java to be very slow and unable to handle large workloads quickly, especially reports for billing.

However with SugarCRM (php 7.2), I found it to be really fast and not sluggish.

So my question, is it just bad programming on Adaxa's side or is Java really a slow langauge?
 
My next question.

Is JAVA fast?
My personal experience have been that Java is sluggish and very slow.
How did I test this?
I implement ERP systems and with Adaxa and ADempiere I alwasy found Java to be very slow and unable to handle large workloads quickly, especially reports for billing.

However with SugarCRM (php 7.2), I found it to be really fast and not sluggish.

So my question, is it just bad programming on Adaxa's side or is Java really a slow langauge?
The only negative performance press that is really relevant to Java is related to garbage collection, but that's no different for many other mainstream languages that similarly employ GC. If your comparison is PHP then Java on its own will stand up to the challenge, but I can't talk for your experience with Adaxa and Adempiere as I use neither of these -- quite possible their implementations is the problem.
 
[)roi(];21891481 said:
The only negative performance press that is really relevant to Java is related to garbage collection, but that's no different for many other mainstream languages that similarly employ GC. If your comparison is PHP then Java on its own will stand up to the challenge, but I can't talk for your experience with Adaxa and Adempiere as I use neither of these -- quite possible their implementations is the problem.

alright, that makes sense. Okay no then I am determined to invest time in Java.

Yay!! (fml OOP....)
 
[)roi(];21891421 said:
Nope there's two options, 1 is free and covers the full extent of JavaFX. The paid Gluon stuff relates to their custom JavaFX controls, ...

Oh, awesome, awesome!

Thank you for the help Droid!
 
Can you expand?

I am aware that you can DI using property or method injection. Property injection is my last resort. It causes temporal coupling which is bad. It means that I have to set a property before the class is fully satisfied in terms of its dependencies. If I forget to set the property, then the class may fail in the ctor or somewhere else down the line. Just looking from the class from the outside does not tell me the full story about what dependencies are required inside.

Constructor injection does not mean you can only inject interfaces. You can obviously inject concrete implementations using (abstract or not) base class types instead of interfaces. I just chose an interface because I wanted to inject a mock. But it is your choice based on what you want to achieve and the specific scenario. Constructor injection is actually good because it advertises the class' dependencies through the ctor parameters. By simply looking at the ctor you know what dependencies it needs and you then inject them. It also forces you to inject those dependencies in order to instantiate the class. That is actually a good thing. Injecting a ServiceLocator is way less code but actually an anti-pattern (bad). There is lots written about why it is bad.

New dependencies do not require new interfaces. Inject concrete implementations.

The fact that a class ctor signature changes when it needs a new dependency is actually a good thing. When you change a class ctor, the compiler will tell you where to add the new dependency in the ctor call chain. If you added property injection you run a real risk of missing setting the property somewhere in your code where you used the class. And this will cause your code to likely fail at some point. The dreaded object reference exception...
As I replied to [MENTION=32397]mercurial[/MENTION] I probably need to set aside time for a separate thread on dependency injection as a whole. That way we can share code on different approaches to this, including weighing up the pros/cons of each.

My choices have changed over time. As a previous OOP programmer I similarly used to build DI as you have described. My current approaches have however shifted primarily because of the change in the way I construct apps now; In particular the avoidance of traditional reference types, inheritance and abstract classes, with a heavy focus on pure functions.

That's not to say that I don't use Interfaces, I definetly do re it certainly has its place, except I don't tend to use it for DI or mocking that much.
 
Last edited:
[)roi(];21891571 said:
I probably need to set aside time for a separate thread on dependency injection as a whole. That way we can share code on different approaches to this, including weighing up the pros/cons of each.
Agreed

[)roi(];21891571 said:
My choices have changed over time. As a previous OOP programmer I similarly used to build DI as you have described. My current approaches have however shifted primarily because of the change in the way I construct apps now; In particular the avoidance of traditional reference types, inheritance and abstract classes, with a heavy focus on pure functions.

That's not to say that I don't use Interfaces, I definetly do re it certainly has its place, except I don't tend to use it for DI or mocking that much.
Would be interested to see this.
 
My next question.

Is JAVA fast?
My personal experience have been that Java is sluggish and very slow.
How did I test this?
I implement ERP systems and with Adaxa and ADempiere I alwasy found Java to be very slow and unable to handle large workloads quickly, especially reports for billing.

However with SugarCRM (php 7.2), I found it to be really fast and not sluggish.

So my question, is it just bad programming on Adaxa's side or is Java really a slow langauge?
Syntax is more or less similar to C#
 
Top
Sign up to the MyBroadband newsletter
X