How to create objects using a static factory method?
Asked Answered
Z

2

33

I know Unity can be configured to use a class' constructor to create an instance of a class (like below) but that's not what I want.

container.RegisterType<IAuthoringRepository, AuthoringRepository>();

I would like to configure Unity to use a factory method with the windows identity passed as a parameter (ie: RepositoryFactory.CreateAuthoringRepository(WindowsIdentity.GetCurrent())) when resolving a type of IAuthoringRepository. How do i do this?

Zion answered 27/9, 2011 at 23:26 Comment(0)
E
52

One way is to have RepositoryFactory implement IRepositoryFactory, then register that. Resolved types can get a factory, then call its CreateAuthoringRepository method. You could create an overload called CreateAuthoringRepositoryForCurrentIdentity if desired, or register an IIdentity dependency of the factory with Unity.

I'd probably just inject a factory and leave the CreateAuthoringRepository method as you have it, then have the clients pass WindowsIdentity.GetCurrent(). That way the identity is always fresh, and you can mock the factory for testing.

Alternately, you can specify a delegate with InjectionFactory:

void Main()
{
    using (var container = new UnityContainer())
    {
        container.RegisterType<IAuthoringRepository>(
            new InjectionFactory(c => CreateAuthoringRepository()));

        Console.WriteLine("debug - resolving model");
        var model = container.Resolve<Model>();
    }
}

public IAuthoringRepository CreateAuthoringRepository()
{
    Console.WriteLine("debug - calling factory");
    return new AuthoringRepository
        { Identity = WindowsIdentity.GetCurrent() };
}

public class Model
{
    public Model(IAuthoringRepository repository)
    {
        Console.WriteLine(
            "Constructing model with repository identity of "
            + repository.Identity);
    }
}

public interface IAuthoringRepository
{
    IIdentity Identity { get; }
}

public class AuthoringRepository : IAuthoringRepository
{
    public IIdentity Identity { get; set; }
}

This prints:

debug - resolving model
debug - calling factory
Constructing model with repository identity of System.Security.Principal.WindowsIdentity

That's for Unity 2.0. With earlier versions, see StaticFactoryExtension.

Elery answered 27/9, 2011 at 23:44 Comment(1)
How to integrate this to asp.net mvc application?#50044286Urethra
T
3

Now method InjectionFactory is obsolete. That's why it'd be better to use method RegisterFactory. Below I am showing how the previous code changed. How you see I changed the method CreateAuthoringRepository. Now it is the static method with one param IUnityContainer container

void Main()
{
    using (var container = new UnityContainer())
    {
        container.RegisterFactory<IAuthoringRepository>(CreateAuthoringRepository);

        Console.WriteLine("debug - resolving model");
        var model = container.Resolve<Model>();
    }
}

public static IAuthoringRepository CreateAuthoringRepository(IUnityContainer container)
{
    Console.WriteLine("debug - calling factory");
    return new AuthoringRepository
        { Identity = WindowsIdentity.GetCurrent() };
}

public class Model
{
    public Model(IAuthoringRepository repository)
    {
        Console.WriteLine(
            "Constructing model with repository identity of "
            + repository.Identity);
    }
}

public interface IAuthoringRepository
{
    IIdentity Identity { get; }
}

public class AuthoringRepository : IAuthoringRepository
{
    public IIdentity Identity { get; set; }
}
Tessatessellate answered 29/3, 2019 at 14:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.