Click here to Skip to main content
15,442,007 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I was studying the concept of IOC and DI and tried to implement it. There are many third party tools like AutoFac,Ninject,UnitCOntroller etc. But is it possible to implement IOC and DI without using any those third party tool.
Posted

 
Share this answer
 
Comments
CHill60 19-Jan-15 6:43am    
5'd - same article that I was going to suggest :-)
TheRealSteveJudge 19-Jan-15 6:46am    
Thank you!
Mithun P 19-Jan-15 23:47pm    
Hi Thanks for your reply but I am using Repository Pattern ,now in controller I am calling this repository interface in constructor as Constructor Injection. So now how do I handle default constructor.
TheRealSteveJudge 20-Jan-15 2:14am     CRLF
Your're welcome! This looks like a new question. When you ask a new question, do not forget to give also the code you have written. Thank you.
Mithun P 20-Jan-15 3:44am     CRLF
Hi..Below is the code for controller. public class ProductController : Controller { private IProductService productService; public ProductController(IProductService productService) { this.productService = productService; } } This Product service code is as below public class ProductService:IProductService { private IRepository productRepository; public ProductService(IRepository productRepository) { this.productRepository = productRepository; } } Now you can see in controller class I am not using default constructor. For this I used two ways ,one using autofac another Unity. But can I achieve this without using any of this third party.
Please look here :
http://joelabrahamsson.com/inversion-of-control-an-introduction-with-examples-in-net/
an introduction to Inversion of Control, using the Dependency Injection and Service Locator patterns, with simple examples in C#.

This site also has some interesting articles about the IoC & DI:
Dependency Inversion Principle, IoC Container, and Dependency Injection (Part - 1)[^]
Dependency Inversion Principle, IoC Container & Dependency Injection (Part - 2)
Dependency Inversion Principle, IoC Container, and Dependency Injection (Part - 3)
Dependency Inversion Principle, IoC Container, and Dependency Injection (Part - 4)

Below is a nice video which demonstrates Inversion of control and how its is different from DI (Dependency Injection):
https://www.facebook.com/photo.php?v=690253231015623&set=vb.341019362605680&type=2&theater
And here look more examples:
https://www.facebook.com/DotNetInterviewQuestions/videos?fref=photo

I hope this will help you))
Good Luck!
 
Share this answer
 
Comments
Y.Desros 20-Jan-15 6:55am    
Nice answer Alex!
Volynsky Alex 20-Jan-15 7:07am    
Thanks Y.Desros!
I just implemented my custom IController methods. And then I used that class in Application_Start.

public class ControllerFactory : DefaultControllerFactory
{
public override IController CreateController(RequestContext requestContext, string controllerName)
{
IController controllerObject = null;

Type controllerType = GetTypeForController(controllerName);
if (controllerType != null)
{
IEmployee employee = new EmployeeService((IRepository) new Repository());
controllerObject = (IController)Activator.CreateInstance(controllerType, employee);
}
return controllerObject;
}


public override void ReleaseController(IController controller)
{
var disposable = controller as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}

private static Type GetTypeForController(string controllerName)
{
string fullTypeName;
Type type = null;

var ioCDictionary = new Dictionary();
ioCDictionary.Add("Employee", "HRApplication.Controllers.EmployeeController,HRApplication");

if (ioCDictionary.TryGetValue(controllerName, out fullTypeName))
{
type = Type.GetType(fullTypeName);
}
return type;
}
}

Then in app start we need to have this code
ControllerBuilder.Current.SetControllerFactory(typeof(ControllerFactory));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900