Click here to Skip to main content
15,892,927 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: A proper DI implementation? Pin
James Simpson28-Feb-11 3:57
James Simpson28-Feb-11 3:57 
GeneralRe: A proper DI implementation? Pin
Member 391904928-Feb-11 19:21
Member 391904928-Feb-11 19:21 
GeneralRe: A proper DI implementation? Pin
Member 391904928-Feb-11 20:01
Member 391904928-Feb-11 20:01 
GeneralRe: A proper DI implementation? Pin
James Simpson28-Feb-11 22:55
James Simpson28-Feb-11 22:55 
GeneralRe: A proper DI implementation? Pin
Member 39190491-Mar-11 8:44
Member 39190491-Mar-11 8:44 
GeneralRe: A proper DI implementation? Pin
Member 39190491-Mar-11 9:07
Member 39190491-Mar-11 9:07 
GeneralRe: A proper DI implementation? Pin
James Simpson1-Mar-11 10:02
James Simpson1-Mar-11 10:02 
AnswerRe: A proper DI implementation? Pin
Spectre_0013-Mar-11 2:03
Spectre_0013-Mar-11 2:03 
While not incorrect by any means, IoC/DI can be as simple as:

/*
 * Simple Inversion of Control/Dependency Injection
 */

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Configuration;
using USCG_SSO.Interfaces;

namespace USCG_SSO
{
    public class ObjectFactory<T> : IObjectFactory<T>
    {
        public ObjectFactory() { }

        // Create an instance of an object
        public T getInstance()
        {
            return getInstance(null);
        }

        // Overload create instance of an object using creation parameters
        public T getInstance(object[] initializationParameters)
        {
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            Type type = Activator.CreateInstance(typeof(T), initializationParameters).GetType();
            // Any special initialization for an object should be placed in a case statement
            // using that object's fully qualified type name
	    if (initializationParameters == null)
	    {
		switch (type.ToString())
		{
		    case "USCG_SSO.ServiceUser":
			NameValueCollection appSettings = ConfigurationManager.AppSettings;
			initializationParameters = new object[]
			{
			    appSettings["ADUserID"],
			    appSettings["ADUserPassword"],
			    appSettings["ADFieldName"]
			}
	                break;
		    case "USCG_SSO.ActiveDirectory"
			initializationParameters = new object[]
			{
			    new ObjectFactory<ServiceUser>().getInstance(),
			    new ObjectFactory<CurrentUser>().getInstance()
			}
			break;
		    default:
			break;
		}
	    }
            return (T)Activator.CreateInstance(typeof(T), initializationParameters);
        }
    }
}


Assuming: USCG_SSO.ActiveDirectory object requires a ServiceUser object and a CurrentUser object to be passed in it's constructor, and USCG_SSO.ServiceUser object requires values retreived from the .config file in it's constructor. If you call

IActiveDirectory ADObject = new ObjectFactory<ActiveDirectory>().getInstance();


You would obtain an ActiveDirectory object complete with all of it's dependencies. Now, granted, this isn't as "fancy" or extensible as an actual IoC framework (there's no registration of dependencies, auto-resolution, etc.), it is, however, a complete, very simple IoC/DI implementation.
Kevin Rucker, Application Programmer
QSS Group, Inc.
United States Coast Guard OSC
Kevin.D.Rucker@uscg.mil

"Programming is an art form that fights back." -- Chad Hower

Questionpayment processor - your experience Pin
Jassim Rahma26-Feb-11 20:55
Jassim Rahma26-Feb-11 20:55 
AnswerRe: payment processor - your experience Pin
Abhijit Jana26-Feb-11 22:18
professionalAbhijit Jana26-Feb-11 22:18 
GeneralRe: payment processor - your experience Pin
Jassim Rahma3-Mar-11 1:12
Jassim Rahma3-Mar-11 1:12 
AnswerRe: payment processor - your experience Pin
N a v a n e e t h27-Feb-11 2:56
N a v a n e e t h27-Feb-11 2:56 
GeneralRe: payment processor - your experience Pin
Vimalsoft(Pty) Ltd28-Feb-11 22:38
professionalVimalsoft(Pty) Ltd28-Feb-11 22:38 
QuestionHow to save Session Data using Oracle? Pin
Member 297299226-Feb-11 8:55
Member 297299226-Feb-11 8:55 
AnswerRe: How to save Session Data using Oracle? Pin
Abhijit Jana26-Feb-11 22:22
professionalAbhijit Jana26-Feb-11 22:22 
QuestionWindows service failing to get the data from Database Pin
indian14325-Feb-11 12:21
indian14325-Feb-11 12:21 
AnswerRe: Windows service failing to get the data from Database Pin
N a v a n e e t h26-Feb-11 14:35
N a v a n e e t h26-Feb-11 14:35 
AnswerRe: Windows service failing to get the data from Database Pin
coolestCoder28-Feb-11 2:08
coolestCoder28-Feb-11 2:08 
QuestionFinal Year IEEE Projects in Cegonsoft malleshwaram Pin
hema govindaraju25-Feb-11 3:06
hema govindaraju25-Feb-11 3:06 
AnswerRe: Final Year IEEE Projects in Cegonsoft malleshwaram Pin
Richard MacCutchan25-Feb-11 3:13
mveRichard MacCutchan25-Feb-11 3:13 
AnswerRe: Final Year IEEE Projects in Cegonsoft malleshwaram Pin
fjdiewornncalwe25-Feb-11 4:54
professionalfjdiewornncalwe25-Feb-11 4:54 
Questionasp.net security > access rules apply to whole folder rather than single form Pin
Asif Rehman25-Feb-11 0:27
Asif Rehman25-Feb-11 0:27 
AnswerRe: asp.net security > access rules apply to whole folder rather than single form Pin
Shahriar Iqbal Chowdhury/Galib25-Feb-11 1:17
professionalShahriar Iqbal Chowdhury/Galib25-Feb-11 1:17 
GeneralRe: asp.net security > access rules apply to whole folder rather than single form Pin
Asif Rehman25-Feb-11 2:03
Asif Rehman25-Feb-11 2:03 
Questiongridview dropdownlist bind onclick Pin
C#Coudou24-Feb-11 20:24
C#Coudou24-Feb-11 20:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.