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, e.g. 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.
The classes: Continents, Countries and Cities implement IEntities interface with property string "Name".
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. And when the IEntities instance is passed to the constructor of the BL class, I want to pass the object using Unity. Can you please advice how to do it?
As it exists, this design is not well suited to incorporating an IoC container.
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.
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. 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).
Interestingly, though, you seem to be instantiating these Entity objects for the sole purpose of passing their string
name to the CreateItemsFromDatabase
method; 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. 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.
What might be better suited is to make GetItems
a generic method. Instead of passing an IEntity
to the BL
constructor, you would pass the concrete type to the method:
var bl = new BL();
var countries = bl.GetItems<Countries>();
var cities = bl.GetItems<Cities>();
var continents = bl.GetItems<Continents>();
精彩评论