Unity Container, allow single objects

I'm starting to learn about Unity Container and Injection Dependency Injection. It’s hard for me to understand what my object model should look like.

In my example, I created a really simple Employee class (I omitted the constructor because this is what I got confused about):

public class Employee
{
    private int _id;
    private string _name;
    private DateTime _birthDate;

    public int Id
    {
        get { return _id; }
    }

    public string Name
    {
        get { return _name; }
    }

    public DateTime BirthDate
    {
        get { return _birthDate; }
    }
}

This Employee object must get its information from the database. Here is the strip database adapter to develop the dependency:

public class DataAdapter
{
    public DbParameter NewParameter(string name, object value)
    {
        return new OracleParameter(name, value);
    }

    public DataRow ExecuteDataRow(string command, CommandType commandType, List<DbParameter> parameters)
    {
        DataTable dt = new DataTable();

        dt.Columns.Add(new DataColumn("ID"));
        dt.Columns.Add(new DataColumn("NAME"));
        dt.Columns.Add(new DataColumn("BIRTH_DATE"));

        DataRow dr = dt.NewRow();

        dr["ID"] = new Random().Next();
        dr["NAME"] = "John Smith";
        dr["BIRTH_DATE"] = DateTime.Now;

        return dr;
    }
}

Ideally, the employee object should take the id parameter to find out which Employee should get from the database. Let's say that we use the Employee constructor, which looks like this:

public Employee(int id, DataAdapter dataAdapter)
{
    List<DbParameter> parameters = new List<DbParameter>();

    parameters.Add(dataAdapter.NewParameter("ID", id));

    DataRow dr = dataAdapter.ExecuteDataRow("GetEmployee", CommandType.StoredProcedure, parameters);

    if (dr == null)
        throw new EmployeeNotFoundException();

    _id = id;
    _name = Convert.ToString(dr["NAME"]);
    _birthDate = Convert.ToDateTime(dr["BIRTH_DATE"]);

    _id = employeeData.Id;
    _name = employeeData.Name;
    _birthDate = employeeData.BirthDate;
}

I'm not sure how to specify an Employee identifier using a Unity resolver, except for using the ParameterOverride parameter:

class Program
{
    static void Main(string[] args)
    {
        UnityContainer container = new UnityContainer();

        container.RegisterType(typeof(EmployeeData));

        Employee emp = container.Resolve<Employee>(new ParameterOverride("id", 45));

        Console.WriteLine(emp.Id);
        Console.WriteLine(emp.Name);
        Console.WriteLine(emp.BirthDate);
        Console.ReadKey();
    }
}

, , . , . - , ?

!

+5
2

/-. , , -.

, .

:

  • DI
  • factory .

-

public class PersonRepository
{
    public PersonRepository(IDbConnection iGetInjected)
    {
    }

    public Person Get(int id)
    {
        // I create and return a person
        // using the connection to generate and execute a command
    }
}
+6

. DI - , .

:

public interface IEmployee
{
    int Id { get; set;}
    string name { get; set;}
    DateTime BirthDate { get; set; }
}

public class Employee : IEmployee
{
    public int Id { get; set;}
    public string name { get; set;}
    public DateTime BirthDate { get; set; }
}

:

class Program
{
    static void Main(string[] args)
    {
        UnityContainer container = new UnityContainer();

        container.RegisterType(typeof(IEmployee), typeof(Employee));

        IEmployee emp = container.Resolve<IEmployee>();

        Console.ReadKey();
    }
}

IEmployee .

0

All Articles