none
Dependency Injection design pattern RRS feed

  • Question

  • Hi all,

    In the following code I have implemented Dependency Injection design pattern; however, I am still confused about the correctioness of this implementation. This implementaation also includes Factory design pattern to inject dependent object. I want to know that can I implemtent Dependency Injection design pattern with out Factory design pattern.

    I find ovet the internet but I dint find any pure implementation of Dependency Injection design pattern. Can any body reffre some good example.

    public interface ILog
    {
    	void WriteLog();
    }
    
    public class LogIntoFile : ILog
    {
    	public void WriteLog()
    	{
    
    	}
    }
    
    public class LogIntoEventviewer : ILog
    {
    	public void WriteLog()
    	{
    
    	}	
    }
    
    public class LogIntoDatabase: ILog
    {
    	public void WriteLog()
    	{
    
    	}		
    }
    
    class LogFactory
    {
    	ILog objLogging;
    
    	public static ILog CreateObject()
    	{
    		//Get type of object need to be created from configuration file
    		String type = AppSettings["LoggingType"];
    
    		ILog objLogging = null;
    
    		switch(type)
    		{
    			case "LogIntoDatabase":
    				objLogging = new LogIntoDatabase();
    				break;
    			case "LogIntoFile";
    				objLogging = new LogIntoFile();
    				break;
    			case "LogIntoEventviewer";
    				objLogging = new LogIntoEventviewer();
    				break;
    			default:
    				break;		
    		}
    	
    		return objLogging;
    	}
    }
    
    
    public class DatabaseHelper
    {
    	var1, ..... varN;
    	
    	ILog objLogging;
    	
    	DatabaseHelper()
    	{
    		objLogging = LoggingFactory.CreateObject();
    	}
    
    
    	public void Insert(Object obj)
    	{	
    		/*TODO: insert code*/
    
    		
    		if (objLogging!=null)
    			objLogging.WriteLog();
    	}
    }
    
    Any help in this regards would be heighly appreciated.
    Tuesday, December 3, 2013 10:54 AM

Answers