Owais Wani
Owais Wani

Reputation: 119

How to Access members of WPF App & ClassLibrary and vice versa

I am into this wpf from last 2 weeks. I am currently developing a wpf application based on MVVM pattern. I have 2 projects inside my Solution in Visual C# 2010. One is a WPF application(lets say MSPBoardControl) and other is a Class Library(lets say ConnectViewComponent). Thus both the MSPBoardControl and ConnectViewComponent have the view, viewmodel, model classes respectively.

I have added the reference of ConnectViewComponent in my MSPBoardControl and I am able to access the member variables of ConnectViewComponent in my MSPBoardControl's View,Viewmodel and model class. My concern is how to access the member variables of MSPBoardControl from my ConnectViewComponent.

ViewModel of MSPBoardControl:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using ConnectViewComponent.Model;
using System.Windows.Input;
using ConnectViewComponent.Commands;
[[[using MSPBoardControl.ViewModel;]]]

namespace ConnectViewComponent.ViewModel
{
public class ConnectViewModel : INotifyPropertyChanged
{
    public List<ConnectModel> m_BoardNames;
    [[[BoardControlViewModel mBoardVM;]]]

    public ConnectViewModel()
    {
        m_BoardNames = new List<ConnectModel>()
        {                
            new ConnectModel() {Name = "Bavaria", Connection_Status = "Disconnected"},
            new ConnectModel() {Name = "Redhook", Connection_Status = "Disconnected"},                
        };
    }

    public List<ConnectModel> BoardNames
    {
        //get set
    }

    private ConnectModel m_SelectedBoardItem;
    public ConnectModel SelectedBoard
    {
        //get set
    }

    private ICommand mUpdater;
    public ICommand ConnectCommand
    {
        get
        {
            if (mUpdater == null)
                mUpdater = new DelegateCommand(new Action(SaveExecuted), new Func<bool>(SaveCanExecute));

            return mUpdater;
        }
        set
        {
            mUpdater = value;
        }
    }

    public bool SaveCanExecute()
    {
        return true;
    }

    public void SaveExecuted()
    {
        if (SelectedBoard.Connection_Status == "Disconnected" && SelectedBoard.Name == "Bavaria")
        {
            SelectedBoard.Connection_Status = "Connected";                
        }

        else if (SelectedBoard.Connection_Status == "Disconnected" && SelectedBoard.Name == "Redhook")
        {
            SelectedBoard.Connection_Status = "Connected";               
        }
    }
}
}

[[[ -- ]]] in my code denotes I am not able to access the members of BoardControlViewModel as well as USING Namespace.ViewModel too.

I cannot add the reference of BoardControl in my ConnectComponent project since it will lead to circular dependency. How can I access it? Please Help!!

Upvotes: 0

Views: 215

Answers (2)

Martin Liversage
Martin Liversage

Reputation: 106856

Having circular dependencies in your project can be a "code smell". There are various ways to remove this "smell". For simplicity lets say that you have project A and project B that has a circular dependency.

  1. Factor out the common types used by both projects and move them into a new project C. Let A and B reference C. This should remove either the dependency from A to B or the opposite dependency or even both dependencies.

  2. If A and B has types that need to interact you need to decouple this interaction into a common set of abstractions (e.g. interfaces or abstract base classes). You should then move these types without any implementation into project C that both A and B references. This will allow the types in A and B to interact but only using the definitions in C. An example could be that A is the main application. It calls in interface IService defined in C but implemented in B and registers a callback IServiceCallback defined in C via IService. B can then call back into A using IServiceCallback without knowing that the implementation is in A.

  3. If the types in A and B are strongly coupled you should merge A and B into a single project.

Upvotes: 2

DanyloStackylo
DanyloStackylo

Reputation: 420

You can add a some type of "Common" library (project) that will contain those general classes. So both BoardControl and ConnectComponent can reference it.

Also you can check similar question.

Upvotes: 1

Related Questions