开发者

How to prevent shared viewmodel for usercontrols?

Let me try and explain my problem.

I am using an MVVM pattern in an ASP.NET Web Forms application.

Each usercontrol has a backing ViewModel and I am initializing them using:

public TViewModelInterface ViewModel {
    get {
        return IoC.Resolve<TViewModelInterface>();
    }
}

This code is present inside a ViewBase class declared like so:

public abstract class ViewBase<TUserCo开发者_StackOverflow社区ntrol, TViewInterface, TViewModelInterface> : UserControl, IView
    where TUserControl : UserControl
    where TViewInterface : class, IView
    where TViewModelInterface : class, IViewModel {

Now, it's all nice and fine as long as the UserControl is not used at multiple places in the same ASPX page.

When that is done, bam! Both the UserControl instances share the same TViewModelInterface instance!

Till now I was using Per Request init strategy for the View Model interfaces. I am thinking of making it Transient and caching the instance inside the ViewBase (so essentially inside a UserControl instance.)

Is that the best approach? I hope I made my question clear.

PS: IoC.Resolve is just a wrapper over Unity.


If I understand correctly, this is only a question of configuring your dependency container appropriately. You seem to resolve in sort of singleton mode which yields same instance for each Resolve<TViewModelInterface> query. There are choices to resolve in multiton or a plethora of other modi - yielding a new instance per query, for example.

Cheers,

Paul

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜