Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Dependency injection and abstract factory

I have this wcf method

Profile GetProfileInfo(string profileType, string profileName)

and a business rule:

if profileType is "A" read from database.

if profileType is "B" read from xml file.

The question is: how to implement it using a dependency injection container?

like image 400
tartafe Avatar asked Jan 30 '10 17:01

tartafe


People also ask

When should we use abstract factory pattern?

When to Use Abstract Factory Pattern: The client is independent of how we create and compose the objects in the system. The system consists of multiple families of objects, and these families are designed to be used together. We need a run-time value to construct a particular dependency.

Is factory pattern a dependency injection?

Factory and Dependency injection both are the design pattern which can be used to enhance loose coupling abilities between the software components. Factory design pattern is used to create objects. But, injection and life cycle management of the object should be handled by programmer within the application.

What is the purpose of Abstract Factory?

The purpose of the Abstract Factory is to provide an interface for creating families of related objects, without specifying concrete classes. This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles.

Is Abstract Factory a design pattern?

Abstract Factory is a creational design pattern that lets you produce families of related objects without specifying their concrete classes.


1 Answers

Let's first assume that you have an IProfileRepository something like this:

public interface IProfileRepository
{
     Profile GetProfile(string profileName);
}

as well as two implementations: DatabaseProfileRepository and XmlProfileRepository. The issue is that you would like to pick the correct one based on the value of profileType.

You can do this by introducing this Abstract Factory:

public interface IProfileRepositoryFactory
{
    IProfileRepository Create(string profileType);
}

Assuming that the IProfileRepositoryFactory has been injected into the service implementation, you can now implement the GetProfileInfo method like this:

public Profile GetProfileInfo(string profileType, string profileName)
{
    return this.factory.Create(profileType).GetProfile(profileName);
}

A concrete implementation of IProfileRepositoryFactory might look like this:

public class ProfileRepositoryFactory : IProfileRepositoryFactory
{
    private readonly IProfileRepository aRepository;
    private readonly IProfileRepository bRepository;

    public ProfileRepositoryFactory(IProfileRepository aRepository,
        IProfileRepository bRepository)
    {
        if(aRepository == null)
        {
            throw new ArgumentNullException("aRepository");
        }
        if(bRepository == null)
        {
            throw new ArgumentNullException("bRepository");
        }

        this.aRepository = aRepository;
        this.bRepository = bRepository;
    }

    public IProfileRepository Create(string profileType)
    {
        if(profileType == "A")
        {
            return this.aRepository;
        }
        if(profileType == "B")
        {
            return this.bRepository;
        }

        // and so on...
    }
}

Now you just need to get your DI Container of choice to wire it all up for you...

like image 112
Mark Seemann Avatar answered Sep 22 '22 01:09

Mark Seemann