Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Difference between factory pattern and IoC [duplicate]

Possible Duplicate:
Dependency Injection vs Factory Pattern

Can someone please explain (with SIMPLE examples) of the difference between the factory pattern and Inversion of Control pattern. Preferably using .NET2.0

like image 508
Eminem Avatar asked Jan 24 '11 11:01

Eminem


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

Why is C named so?

Because a and b and c , so it's name is C. C came out of Ken Thompson's Unix project at AT&T. He originally wrote Unix in assembly language. He wrote a language in assembly called B that ran on Unix, and was a subset of an existing language called BCPL.


2 Answers

The factory pattern: the object which needs a reference to a service, should know about the factory that creates the Service:

public class BLLObject 
{
    public IDal DalInstance { get; set; }

    public BLLObject()
    {
        DalInstance = DalFactory.CreateSqlServerDal();
    }
}

The Ioc Pattern (or Dependency Injection) :

the object only needs to declare its need to the service, using any aspects of the Ioc Pattern (Constructor, setter, or interface ... etc) and the container will try to fulfill this need:

public class BLLObject 
{
    public IDal DalInstance { get; set; }

    public BLLObject(IDal _dalInstance)
    {
        DalInstance = _dalInstance;
    }
}

which means that in the factory pattern, the object decides which creation method (by choosing a specific concrete factory) to use, but the in the Ioc pattern, it is up to the container to choose.

of course this is not the only deference, but this is what is in my mind for the time being. correct me please if I'm wrong ?

like image 195
Nour Avatar answered Oct 22 '22 01:10

Nour


The factory pattern is about getting the reference to a type, so somewhere in your code you would be calling into a factory to resolve something.

The Inversion of control pattern means that you would typically use an Ioc container to resolve dependencies for you. This could be in a similar way to a factory, or more typically you would use dependency injection to resolve the dependencies into the constructor or setters.

like image 36
Kev Hunter Avatar answered Oct 22 '22 01:10

Kev Hunter