如何实现控制反转C#-wpf

时间:2019-03-20 18:43:13

标签: c# wpf inversion-of-control

我正在通过下面的链接学习如何在wpf应用程序中实现控制反转

https://www.codeguru.com/columns/experts/implementing-the-inversion-of-control-pattern-in-c.htm

应用程序的目的是将数据从表加载到组合框。该代码运行正常,没有任何问题。因此,作为学习基础设计原理的一部分,我认为以下是高级模块(Viewmodel)依赖于较低级模块(populatetab2combobox)的完美案例。我相信在这里我们可以应用依赖倒置。在在线阅读时,大多数都是从控制反转开始的,这是实现依赖反转的第一步。

我只是想重申一下,我没有经验原则。我的假设可能是错误的。如果我错了,请纠正我。

Window.xaml:

  <ComboBox ItemsSource="{Binding populatecombobox.modeltogetusername}" Width="155" Margin="0,-20,-180,137">
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding username}"/>
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>

查看模型:

代码而不实现控制反转(此方法可以正常工作,没有任何问题):

//Class to populate the combobox

public class populatetab2combobox
{
    public ObservableCollection<comboboxdata> modeltogetusername { get; set; }

    public void getdatausinglinq()
    {
        using (Operations_Productivity_ToolEntities context = new Operations_Productivity_ToolEntities())
        {
            var a1 = from t1 in context.Test_ImportedAuditdata
                     select t1;

            if (modeltogetusername == null)
                modeltogetusername = new ObservableCollection<comboboxdata>();

            foreach (var a in a1.GroupBy(x => x.username).Select(x => x.FirstOrDefault()))
            {
                modeltogetusername.Add(new comboboxdata
                {
                 username = a.username.ToString()
                });

            }
        }

    }

}


 public class ViewModel: INotifyPropertyChanged {

/** You can see that I'm calling viewModel class is depended with 
   populatetab2combobox. I believe this is the perfect case for implementing 
   Inversion of control **/

populatetab2combobox_populatecombobox = new populatetab2combobox();
private PopulateDatagrid _populatedatagridwithobservablecollection = new PopulateDatagrid();
private Loadfileintodatabase loaddata = new Loadfileintodatabase();

public PopulateDatagrid Populatedatagridwithobservablecollection {
    get {
        return _populatedatagridwithobservablecollection;
    }
    set {
        if (value != _populatedatagridwithobservablecollection) {
            _populatedatagridwithobservablecollection = value;
            OnPropertyChanged("Populatedatagridwithobservablecollection");
        }
    }
}


DataModel dm = new DataModel();

public ViewModel() {
    _populatecombobox.getdatausinglinq();
    DoSomeThingCmd = new RelayCommand(o = >search());
    _populatedatagridwithobservablecollection.getdatausinglinq();
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName) {
    if (PropertyChanged != null) {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

#endregion
}
}

具有控制反转的实现的代码(问题是在实现IOC时运行时,组合框为空):

public interface INofificationAction
{
     void getdatausinglinq();
}



public class populatetab2combobox : INofificationAction
{
    public ObservableCollection<comboboxdata> modeltogetusername { get; set; }

    public void getdatausinglinq()
    {
        using (Operations_Productivity_ToolEntities context = new Operations_Productivity_ToolEntities())
        {
            var a1 = from t1 in context.Test_ImportedAuditdata
                     select t1;

            if (modeltogetusername == null)
                modeltogetusername = new ObservableCollection<comboboxdata>();

            foreach (var a in a1.GroupBy(x => x.username).Select(x => x.FirstOrDefault()))
            {
                modeltogetusername.Add(new comboboxdata
                {
                 username = a.username.ToString()
                });

            }
        }

    }

}

 public class implementingabstraction
{
    INofificationAction _an;
    public implementingabstraction(INofificationAction action)
    {
        this._an = action;
    }

    public void getdatausinglinq()
    {
        _an.getdatausinglinq();
    }

}


public class ViewModel: INotifyPropertyChanged
{
INofificationAction getdata123 = new populatetab2combobox();

populatetab2combobox _populatecombobox = new populatetab2combobox();

public populatetab2combobox populatecombobox {
    get {
        return _populatecombobox;
    }
    set {
        if (value != _populatecombobox) {
            _populatecombobox = value;
            OnPropertyChanged("populatetab2combobox");
        }
    }
}

public RelayCommand DoSomeThingCmd {
    get;
    set;
}
DataModel dm = new DataModel();

public ViewModel() {

    implementingabstraction abs = new implementingabstraction(getdata123);
    abs.getdatausinglinq();
    DoSomeThingCmd = new RelayCommand(o = >search());
    _populatedatagridwithobservablecollection.getdatausinglinq();
}

}

问题:

1)因为我是基于oops的方法的新手,以上用例是否正确。

2)上面的代码无法正常工作。您能指出我的方法中的错误吗?

0 个答案:

没有答案