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 1074715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:10:06+00:00 2026-05-16T21:10:06+00:00

we mvvm lovers all know Josh Smith mvvm sample and how he has saved

  • 0

we mvvm lovers all know Josh Smith mvvm sample and how he has saved the customer in the detail customer view by injecting the repository object into the customerViewModel`s constructor.

But a viewmodel should not know about repositories. Its just a model of a view nothing must being aware of persistence etc…

How can I register my Action delegate SaveDocumentDelegate on the DocumentViewModel if its set in the code-behind? Actually I should subscribe the delegate in my DocumentController but how can I instantiate the DocumentView in my DocumentController and set it as Datacontext not doing that in code-behind. Only thing that came to my mind is using a contentcontrol in the window and bind it to the type of the viewModel and datatemplate it with the Document UserControl like this:

<UserControl.Resources>

        <DataTemplate DataType="{x:Type ViewModel:DocumentViewModel}">
            <View:DocumentDetailView/>
        </DataTemplate>

    </UserControl.Resources>

<ContentControl Content="{Binding MyDocumentViewModel}" />

But I do not want to use a control to solve my architectural problems…

xaml:(view first approach)

public partial class DocumentDetailView : UserControl
    {
        public DocumentDetailView()
        {
            InitializeComponent();

            this.DataContext = new DocumentViewModel(new Document());
        }
    }

DocumentViewModel:

 public class DocumentViewModel : ViewModelBase
    {
        private Document _document;
        private RelayCommand _saveDocumentCommand;
        private Action<Document> SaveDocumentDelegate;

        public DocumentViewModel(Document document)
        {
            _document = document;
        }

        public RelayCommand SaveDocumentCommand
        {
            get { return _saveDocumentCommand ?? (_saveDocumentCommand = new RelayCommand(() => SaveDocument())); }
        }

        private void SaveDocument()
        {
            SaveDocumentDelegate(_document);
        }        

        public int Id
        {
            get { return _document.Id; }
            set
            {
                if (_document.Id == value)
                    return;

                _document.Id = value;
                this.RaisePropertyChanged("Id");
            }
        }

        public string Name
        {
            get { return _document.Name; }
            set
            {
                if (_document.Name == value)
                    return;

                _document.Name = value;
                this.RaisePropertyChanged("Name");
            }
        }

        public string Tags
        {
            get { return _document.Tags; }
            set
            {
                if (_document.Tags == value)
                    return;

                _document.Tags = value;
                this.RaisePropertyChanged("Tags");
            }
        }
    }

UPDATE:

public class DocumentController
    {
        public DocumentController()
        {    
            var win2 = new Window2();
            var doc =  new DocumentViewModel(new DocumentPage());
            doc.AddDocumentDelegate += new Action<Document>(OnAddDocument);
            win2.DataContext = doc;
            wind2.ShowDialog();
        }

        private void OnAddDocument(Document doc)
        {
            _repository.AddDocument(doc);
        }
    }

What do you think about that idea?

  • 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-16T21:10:06+00:00Added an answer on May 16, 2026 at 9:10 pm

    But a viewmodel should not know about
    repositories. Its just a model of a
    view nothing must being aware of
    persistence etc…

    The viewmodel connects the model and view together; it is exactly what controls persistence, though it does not handle persistence.

    We decouple this from other concern by using services.

    One way to avoid adding persistence concerns to the viewmodel is by abstracting those concerns into repository interfaces, so that we can inject it as a dependency. In this way we can delegate persistence work in the viewmodel, usually in response to the user’s interaction with the view.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.