I will demonstrate the entire concept with clear examples that shows the difference between traditional implementation and new implementation.
Download Source Code.
Traditionally,
- publicinterfaceINewemp
- {
- stringEmpID { get; set; }
- stringEmpName { get; set; }
- stringEmpAddress { get; set; }
- DateTimeDateofJoining{ get; set; }
- }
In the above interface we have properties for New Employees and the below interface has properties for Old Employee.
- publicinterfaceIOldemp
- {
- stringEmpID
- {
- get;
- set;
- }
- stringEmpName
- {
- get;
- set;
- }
- stringEmpAddress
- {
- get;
- set;
- }
- DateTimeDateofJoining
- {
- get;
- set;
- }
- DateTimeDateofReleaving
- {
- get;
- set;
- }
- }
See the difference carefully,
Just try to give implementation for it.
When you are trying to implement both interfaces, your class may get conflicts. Find Conflict section below
So what we do?
Solution1: Take two different classes and give implementation accordingly.
Finally your problem is clear for this time only. In future your project may have a chance to come with the same number of interfaces with duplicate properties. So find below solution2 for constructor injection for this problem.
Solution 2: Best solution (Constructor Injection),
Change your design pattern and come like this.
Now you can give simple implementation without need of any second classes.
IOldEmp successfully implements INewemp properties also. Let’s check with practically.
- namespaceConstructorInjection.ConstructorTechnique.Injection
- {
- publicclassConstructorInject
- {
- IOldempOldEmpData = newEmployee();
- publicINewempNewEmpData;
- publicConstructorInject()
- {
- NewEmpData = OldEmpData;
- }
- }
- }
And,
- classProgram
- {
- staticvoid Main(string[] args)
- {
- var request = newConstructorInject();
- varActualResult = request.NewEmpData;
- Console.WriteLine("Actual values from Formal Object refrence");
- Console.WriteLine(ActualResult.EmpID);
- Console.WriteLine(ActualResult.EmpName);
- Console.WriteLine(ActualResult.EmpAddress);
- Console.Read();
- }
- }
Here I never Implemented INewemp interface. It is receiving implementation from IOldemp with the help of constructor injection.
For more information: TechNet (TN).