开发者

Scoping question

I'm working on an app using Caliburn and Autofac. I'm having trouble understanding scoping and best practices.

Lets say I have my main customer window that displays a list of customers (CustomerViewModel). This VM spawns child VM's to edit individual customers (CustomerEditViewModel)

I would like CustomerViewModel to have an instance of CustomerRepository that is accessible to all VM's, but each VM will have it's own local CustomerRepository.

What is the best way to achieve this? I have been using constructor injection and have been avoiding injecting the container everywhere. I could add a static property to my repository, that lets it access the 'global' repository. I believe with Autofac that there is a better way to implement this us开发者_如何学JAVAing lifetime scopes (Main, Child, etc) but I am not sure how to go about this.

Essentially what I'm trying to achieve is, when a child VM saves it's record back to the database through it's local repository, this updated entity will then be imported back into the main repository, avoiding a database call to retrieve the updated object.

Edit: I've come up with a solution of sorts.

I now have a IMainRepository interface, which is created with InstancePerLifetimeScope(). CustomRepository takes an instance of IMainRepository. The CustomerRepository can now communicate with the master repository, without the VM knowing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜