2

We're creating a desktop MVVM application using Prism and Ninject. This application will feature sessions during which data should be recorded under a specific session. Our Views and ViewModels are created by an IoC container by Prism. I would like to start and stop sessions, but I have no idea how to handle scoping for parts such as the DAL of a desktop application.

Am I thinking in the right direction or should I be thinking about it differently? I guess I could pass through a session ID through navigation parameters in Prism or broadcast session events using the PubSubEvents messagebus, but that just seems wrong.

Wouter
  • 2,170
  • 1
  • 28
  • 58
  • 1
    Is there only one user using the application instance at a time? If so you *can* use a custom Scope (`.InScope(...)`). But maybe it would be better if you just had some singleton `SessionManager` which manages the current session state (`.Start()`, `.Stop()`, `.CurrentSessionId`... ). That way you would not need to re-create part of the object-graph but instead just manage state. Any component that needs to know the current session just asks the `ISessionManager`. – BatteryBackupUnit Sep 03 '14 at 05:30
  • This seems like a nice solution. I could let the SessionManager be injected with the necessary factories to generate session dependent data services for anyone interested. – Wouter Sep 04 '14 at 08:13
  • I'm still interested in how to handle IoC scopes in a desktop application. Web developers seems to have it easy with their per request scope. – Wouter Sep 04 '14 at 08:14
  • I don't quite understand what you're trying to achieve. With a desktop application, there can only be one user concurrently, right? So what kind of scopes do you need? Ninject provides several ways to devise your own scopes, for example see https://github.com/ninject/ninject.extensions.namedscope/wiki. – BatteryBackupUnit Sep 05 '14 at 06:15
  • As I said session scopes. Maybe I just don't understand the use cases of IoC scopes and try to apply them to solve the wrong thing. – Wouter Sep 07 '14 at 12:14
  • yeah that might be the case. You should describe - technically (and in detail) - what you want to achieve. – BatteryBackupUnit Sep 07 '14 at 17:54

2 Answers2

1

When you use an IoC container in a desktop app you don't have Scopes that you have in a Web App. Scopes per WebRequest or per Session are not available.

So you have to use another kind of scopes like pero Thread Scope. Or if you a have a special requirement you can create a Custom scopes so you decide when it starts and when it finish. https://github.com/ninject/ninject/wiki/Object-Scopes

Ricardo Polo Jaramillo
  • 12,110
  • 13
  • 58
  • 83
0

Ordinarily a 'session' in web terms can be considered equivalent to a 'unit of work'. I'd expect you're trying to achieve the same level of scope in your desktop application.

If so, you need to define what you consider to be an accurate measure of a single unit of work. In the web world, it can be considered everything that happens between a user request coming in (i.e. clicking a submit button) to the results being sent back (i.e. updating the screen).

As far as I am aware, Ninject supports per request sessions in web by caching the objects for a given user for the duration of their request being handled via some hooked in programmatic magic.

If you decide to apply the same measure of session to your desktop application, then you will need to introduce a layer that triggers a session start on any user page interaction and ends a session on the results being fed back to them.

Unfortunately I have no catch-all solution for you, but hopefully that gets you on the right path.

Steve Lillis
  • 3,263
  • 5
  • 22
  • 41