Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 962123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:27:17+00:00 2026-05-16T01:27:17+00:00

Background: I have a custom user control based around a WPF TreeView. I’ve been

  • 0

Background:
I have a custom user control based around a WPF TreeView. I’ve been using the MVVM pattern and have a base view model TreeNode class and several derived view models such as LocationNode, LocationFolder, PersonNode, PersonFolder, CollectionNode, CollectionFolder, etc..

A example of how the tree might be laid out is:


- CollectionFolder
--> CollectionNode
    --> LocationFolder
        -->LocationNode
        -->LocationNode
    --> PersonFolder
        -->PersonNode
--> CollectionNode
--> CollectionNode
+ CollectionFolder
+ CollectionFolder   

When I perform drag and drop operations, each class handles the business logic, i.e. if I drop on a PersonNode on a CollectionNode, the CollectionNode view model contains the logic how on to add the PersonNode it its child PersonFolder.

Problem:
Everything works great, I can drag and drop all over the place, and the code is nicely contained in the dervied classes. If I need to add an extra drop rule, I add it to the appropriate drop target view model.

The problem is when a PersonNode is added to a PersonFolder I need to create a new database entry to reflect that the underlying Person model is now in also in a new Collection.

Currently, each view model in the tree has access to the current database session/transaction and can perform the insert/save. But this makes catching exceptions and errors extremely repeative, my exception handling code is being duplicated all over my view models. Is there a better way in MVVM to handle my database interactions?

An code snippet from my Drop event in my PersonFolder


// Create a new view model for the Person and add it to my children
_children.Add( new PersonNode( droppedPerson ) );

// Create a new entry in the collection for this person
CollectionEntry entry = new CollectionEntry();
entry.Entity = droppedPerson;
entry.Collection = _collection;

// Save the new entry
using( var transaction = _uow.BeginTransaction( IsolationLevel.Serializable ) )
{
    // Add the entry to the session
    _uow.Add( entry );

    // Save the session
    _uow.SaveChanges(); // [1]

    // Commit transaction
    transaction.Commit(); // [2]
}

[1] and [2] have the potential for throwing exceptions and should be handled in try/catch statements. However, I don’t want to duplicate all my exception handling in all my view models, any suggestions?

I guess I could always implement a singleton to contain the session and exception handling and pass my new entities into that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:27:18+00:00Added an answer on May 16, 2026 at 1:27 am

    I’m assuming that the variable part of your last code block is:

    _uow.Add( entry );
    

    …so in some cases you might actually want significantly more or less actions to happen in that spot.

    I think this is a good candidate for the “Hole in the Middle Pattern“.

    Basically just pass an

    Action<T>
    

    to some other place (Singleton, whatever) that opens a transaction, passes the context (_uow) to your action, and then commits the transaction, and handles all your exception logic. Your code would look like:

    
    // Create a new view model for the Person and add it to my children
    _children.Add( new PersonNode( droppedPerson ) );
    
    // Create a new entry in the collection for this person
    CollectionEntry entry = new CollectionEntry();
    entry.Entity = droppedPerson;
    entry.Collection = _collection;
    
    someSingleton.Execute(o => o.Add(entry));
    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 500k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is not pretty but it works: rm -R $(ls… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer Yes. Override the base1 and base2 methods in Derived to… May 16, 2026 at 12:45 pm
  • Editorial Team
    Editorial Team added an answer No, you can't. Unfortunately, UIEvent doesn't expose any public way… May 16, 2026 at 12:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a custom base user control in silverlight. <UserControl x:Class=Problemo.MyBaseControl xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:d=http://schemas.microsoft.com/expression/blend/2008
I have a WPF user control defined in an application assembly. I'm trying to
The background is I have a custom control that is a asp:Menu that is
I have a custom control containing a path that has a templated tooltip. I
I'm working on a custom user control that essentially displays a name value pair
I am rather confused I have got a createuser wizard control that I use
I have an UITableView showing custom view cells that I've designed in interface builder.
I have an XSLT transform I am using to process an XML file, inserting
The Scenario Bit: On one of the controls within my Silverlight application I have
I am planning on writing a hierarchical organizational control, similar to an org chart.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.