简体   繁体   English

使用Unity作为IoC的构造函数依赖项注入

[英]Constructor dependency injection using Unity as IoC

I have a form, which shows three items in a combo box. 我有一个窗体,它在组合框中显示三个项目。 Continents, Countries and Cities 大洲,国家和城市

If I select an item, eg if I select Cities and then if I click on the "Get Results" button, I send a select command to the database via business and data layer which then retrieves a list of type Cities. 如果选择一个项目,例如,如果选择“城市”,然后单击“获取结果”按钮,则我将通过业务和数据层将选择命令发送到数据库,然后检索“城市”类型的列表。

The List are then bound to the grid on the UI form. 然后将列表绑定到UI表单上的网格。

The classes: Continents, Countries and Cities implement IEntities interface with property string "Name". 这些类:洲,国家和城市使用属性字符串“名称”实现IEntities接口。

The button click event calls Business layer using: 按钮单击事件使用以下方法调用业务层:

click(object sender, EventArgs e)
{
    string selectedItem = comboBox.SelectedItem;
    IEntities entity = null;
    List<IEntities> list = null;

    if (selectedItem == "Cities")
    {
        entity  = new Cities("City");   
    }

    if (selectedItem == "Continents")
    {
        entity  = new Continents("Continents");   
    }

    if (selectedItem == "Countries")
    {
        entity  = new Countries("Countries");   
    }

    //Then I call a method in Business Layer to return list
    BL bl = new BL(entity);
    list = bl.GetItems();
    myDataGrid.DataContext = list;//to bind grid to the list
}

Business Layer looks like this: 业务层如下所示:

public class BL
{

    public IEntities _entity;

    //constructor sets the variable
    public BL(IEntity entity)
    {
        _entity = entity;
    }

    public IList<Entities> GetItems()
    {
        //call a method in data layer that communicates to the database
        DL dl = new DL();
        return dl.CreateItemsFromDatabase(_entity.Name);//name decides which method to call
    }
}

I want to use Unity as the IOC so instead of using factory (sort of) pattern in the button click event with if then elses and using hardcoded class names, I want to use the container's configration that creates the relevant class instance. 我想将Unity用作IOC,所以我不想在按钮click事件中使用工厂(某种)模式(如果使用else则使用else并使用硬编码的类名),而是要使用容器的配置来创建相关的类实例。 And when the IEntities instance is passed to the constructor of the BL class, I want to pass the object using Unity. 当IEntities实例传递给BL类的构造函数时,我想使用Unity传递对象。 Can you please advice how to do it? 您能建议如何做吗?

As it exists, this design is not well suited to incorporating an IoC container. 确实存在,这种设计不太适合合并IoC容器。

As long as your ComboBox still contains strings, you're going to have to compare that against hardcoded values in a switch statement or set of if blocks somewhere . 只要你的ComboBox仍然包含字符串,你将不得不进行比较,对在硬编码值switch语句或设置的if地方

Furthermore, the BL class takes a constructor parameter of type IEntity , but that can be an object of any among many different types at runtime. 此外, BL类采用IEntity类型的构造函数参数,但在运行时可以是许多不同类型中的任何一个对象。 There is no way to configure Unity at startup to instantiate BL without also telling it what to use as that parameter (and nothing to gain by it, really). 无法在启动时将Unity配置为实例化BL而又不告诉它用作该参数的内容(实际上没有任何收获)。

Interestingly, though, you seem to be instantiating these Entity objects for the sole purpose of passing their string name to the CreateItemsFromDatabase method; 不过,有趣的是,您似乎是为了实例化这些Entity对象,其唯一目的是将其string名称传递给CreateItemsFromDatabase方法。 you're not using its type for anything. 您没有将其类型用于任何东西。 It seems that you can skip the constructor parameter altogether and simply pass the selected string from the ComboBox directly to the GetItems method and achieve the same result. 看来,您可以完全跳过构造函数参数,只需将所选的stringComboBox直接传递到GetItems方法即可获得相同的结果。 If you have some other reason for doing this, you should at least not supply the name in the constructor; 如果您还有其他原因,则至少不要在构造函数中提供名称; make it a const within each class declaration. 在每个类声明constconst

What might be better suited is to make GetItems a generic method. 可能更适合的是使GetItems为通用方法。 Instead of passing an IEntity to the BL constructor, you would pass the concrete type to the method: 您可以将具体类型传递给方法,而不是将IEntity传递给BL构造函数:

var bl = new BL();
 var countries = bl.GetItems<Countries>();
 var cities = bl.GetItems<Cities>();
 var continents = bl.GetItems<Continents>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Unity IoC:创建没有构造函数依赖注入的接口实例 - Unity IoC : Create instance of an interface without constructor dependency injection IoC、依赖注入和构造函数参数 - IoC, Dependency injection and constructor arguments 使用Unity的构造函数中使用的属性依赖注入 - Property Dependency Injection used in Constructor using Unity 依赖注入:使用构造函数注入 - Dependency Injection : Using Constructor Injection 使用Unity设置依赖注入以将类的实例传递给构造函数 - Setting up Dependency Injection using Unity to pass instance of class into a constructor 使用Unity依赖注入时如何将类型作为参数传递给构造函数 - How to pass a type as parameter to a constructor when using Unity dependency injection 我应该避免使用依赖注入和IoC吗? - Should I avoid using Dependency Injection and IoC? 使用IOC(控制反转)进行依赖注入 - Dependency injection using IOC (Inversion of Control) IoC Unity:为依赖注入注册实体框架ObjectContext - IoC Unity: registering an Entity Framework ObjectContext for Dependency Injection 在 Xamarin Forms 中找不到 UnityServiceLocator 命名空间(Unity 依赖注入和 IoC 容器) - In Xamarin Forms UnityServiceLocator namespace not found (Unity Dependency Injection & IoC containers)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM