0

I am writing an app using WPF. I want to new up an instance of my service class every time I make a call to my database.
How do I do that using Simple Injector? Should lower layers in my app have knowlege of the container?

In my viewmodel I have an instance of DBClient. DBClient is responsible for creating/disposing an instance of the Service class. The service class is passed the dbcontext and from there it queries the database (I could also new up the dbcontext in the service, either way my question is the same).

Here is the object hierarchy:

ViewModel(DBClient) //  DBClient.GetCustomers()

DBClient(Services???)  // using(Services) { Services.GetCustomers() }  // Problem here...I need to create/dispose Services     

Services(db)    // return db.Customers 

DbContext(connectionString)

connectionString

My question might be related to this one: DbContext Lifestyle per Screen/ViewModel (WPF + Simple Injector) but in my case I want to create/dispose the context on a per use basis, as might be done with a wcf/rest service wrapper.

Community
  • 1
  • 1
  • I think the referenced so answer is all you need. Please read the links given in that answer about the commandhandler pattern – Ric .Net Apr 07 '16 at 19:50
  • @Ric.Net thank you Ric, I think I will look around at other DI frameworks before going to far with Simple Injector. –  Apr 07 '16 at 22:58
  • I don't think this is a container problem. Think how you would solve this question using [Pure DI](http://blog.ploeh.dk/2014/06/10/pure-di/). I think [answer](http://stackoverflow.com/a/29808487/3294832) can also help you – Ric .Net Apr 08 '16 at 08:59

0 Answers0