zyzyx
Topic Author
Posts: 19
Joined: 13 Aug 2014, 13:19

[Unity] Dependency Injection?

16 Sep 2014, 18:19

Hey,
I could not find anything on the forum regarding IoC/DI. Is there a way to use DI in NoesisGUI or are there plans for the future?
 
User avatar
jsantos
Site Admin
Posts: 2900
Joined: 20 Jan 2012, 17:18
Contact:

Re: [Unity] Dependency Injection?

16 Sep 2014, 20:13

Could you elaborate about it? How do you think Dependency Injection could be included in NoesisGUI?
 
zyzyx
Topic Author
Posts: 19
Joined: 13 Aug 2014, 13:19

Re: [Unity] Dependency Injection?

17 Sep 2014, 00:08

I asked just out of curiosity since this gets often mentioned in MVVM and WPF articles. I vaguely remember using DI a long time ago working on a WPF project so I wondered if noesisGUI has an IoC of some sort.

I think this can explain it better than me:
http://wpftutorial.net/ReferenceArchitecture.html
 
User avatar
jsantos
Site Admin
Posts: 2900
Joined: 20 Jan 2012, 17:18
Contact:

Re: [Unity] Dependency Injection?

17 Sep 2014, 18:27

At first, I would say that it can be done on top of NoesisGUI without having to change anything... But I would like to hear the opinion of somebody that has already experience with this pattern.
 
User avatar
Scherub
Posts: 141
Joined: 06 May 2014, 20:53
Contact:

Re: [Unity] Dependency Injection?

18 Sep 2014, 07:23

Well, DI has not that much to do with Noesis in general, it's just a pattern to inject dependencies. So the first step would be to decide what DI framework you want to use. There is for instance StrangeIoC which was primarily written to be used within Unity.

The next step would be to declare all types that should be known and also be injected. You also have to decide what type of injection you want.

The problem with DI is that it won't work when you just call the constructor of a class, such as "new ViewModel()". Instead, depending on the DI framework, there are other ways to create object instances. In the linked WPF article they use a unity container and call Resolve() or TryResolve() to create a new instance of a class. This will also inject the 'marked' dependencies.

That's where you might stumble when you use Noesis and just declare views or viewmodels within XAML, as you have no control over the construction process. Also the 'ViewModels' are relatively closely bound to the 'View' in Noesis.

Hmm, now that I think about it there might be a way to achieve a better separation. I have to check whether I can convince Noesis to work that way. On the other hand I'm happy that I finally got it finally working more or less the way I wanted. There might also be a problem with "OnPostInit()" as there are certain things that shouldn't be put in the constructor.

Anyway, I solved it by just creating all views in code instead. But the goal of my GUI is to be pretty flexible. For a more simple UI that might not be the best approach.
 
zyzyx
Topic Author
Posts: 19
Joined: 13 Aug 2014, 13:19

Re: [Unity] Dependency Injection?

18 Sep 2014, 09:26

Great answer, @Scherub!

I work mostly on GUI heavy apps and somethimes one ViewModel depends on another ViewModel or service. Can you perhaps elaborate on your workflow a little bit?
 
User avatar
jsantos
Site Admin
Posts: 2900
Joined: 20 Jan 2012, 17:18
Contact:

Re: [Unity] Dependency Injection?

18 Sep 2014, 11:54

That's where you might stumble when you use Noesis and just declare views or viewmodels within XAML, as you have no control over the construction process. Also the 'ViewModels' are relatively closely bound to the 'View' in Noesis.

Hmm, now that I think about it there might be a way to achieve a better separation. I have to check whether I can convince Noesis to work that way. On the other hand I'm happy that I finally got it finally working more or less the way I wanted. There might also be a problem with "OnPostInit()" as there are certain things that shouldn't be put in the constructor.
About the "convince Noesis". Yes, we are open to suggestions, improvements... As soon as v1.2 is released I think we will be able to dedicate the energy to new things. :)

By the way, I am really curious about what you are developing using noesisGUI.
 
User avatar
Scherub
Posts: 141
Joined: 06 May 2014, 20:53
Contact:

Re: [Unity] Dependency Injection?

25 Sep 2014, 22:56

@zyzyx: Can you be a bit more precise, please? Otherwise I don't know where to start. :)

@jsantos: It's a bit early to show anything. Not only is everything coders art and all of the graphics are place holders, there are also a lot of missing gui elements that I would like to integrate/implement first. :)

Who is online

Users browsing this forum: Bing [Bot] and 2 guests