Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create objects using a static factory method?

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?

like image 699
burnt1ce Avatar asked Sep 27 '11 23:09

burnt1ce


People also ask

Can a static method create object?

Static Method Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.

How do we create the object in the factory method?

Factory Method Pattern. A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.

Can we create an object in Java by factory method?

Factory pattern is one of the most used design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

Should a factory method be static?

Specifically with the Factory pattern, no, there is no requirement that the factory methods be static. The essence of the pattern is that you have one object which is responsible for creating instances of another class.


1 Answers

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.

like image 105
TrueWill Avatar answered Oct 15 '22 16:10

TrueWill