1

I have an interface IInterface and it looks something like below -

public interface IInterface             
{      
  void SomeMethod1();      
  void SomeMethod2();  
  void SomeMethod3();   
  .                    
  .                    
  .                     
}       

One of the implementations is something like -

public class Implementation : IInterface         
{     
  private Object obj;
  public Implementation(Object obj)      
  {        
       this.obj = obj;
      // Do Something           
  }        

  public void SomeMethod1()    
  {    
     lock(obj)
     {
        // Do Something   
     }     
  }    

  public void SomeMethod2()    
  {    
     // Do Something   
  } 

  public void SomeMethod3()    
  {    
     lock(obj)
     {
        // Do Something   
     }      
  } 
  .                            
  .                            
  .                                    
}     

How to pass a static readonly instance of type Object while registering Implementation class with type IInterface via unity configuration?

sandip ray
  • 57
  • 1
  • 8
  • *"static readonly instance"* - You mean a singleton? In that case, have a look at [this possiby duplicate question](http://stackoverflow.com/questions/16835728/unity-singleton-code). – GolezTrol Jan 11 '16 at 16:07
  • I want to do it via configuration file (& not code) and also I don't want to have a singleton instance of Implementation class but rather while registering the class with unity I want to pass it a static readonly instance of type Object (i.e, something similar to "static readonly obj = new Object()" and then passing obj as a parameter value to the constructor of Implementation class) – sandip ray Jan 11 '16 at 16:17
  • How would you define 'static readonly'? – Peter Porfy Jan 11 '16 at 16:26
  • @PeterPorfy - I am not sure if there is any way to do that via unity config. – sandip ray Jan 11 '16 at 16:44
  • @sandipray Sure, that is why I am asking you about what is your goal. Because 'pass a static readonly instance of type Object' makes no sense. – Peter Porfy Jan 11 '16 at 16:54
  • @sandipray If the object that the constructor takes is static (and public?), why not just change your constructor to take zero paramters? `public Implementation() { this.localObject = SomeClass.MyStaticObject; }` – RB. Jan 11 '16 at 17:12
  • @PeterPorfy Actually there are few methods like SomeMethod in the class and few of these methods uses a lock and that lock needs to use the Object instance and so I want to inject it from outside of the class so that the consumers of this class can pass on some static readonly instance of Object type. – sandip ray Jan 11 '16 at 17:12
  • @RB Please check my last comment. – sandip ray Jan 11 '16 at 17:14
  • @sandipray, you need to provide more information. Please edit the question and include the current unity XML configuration (the relevant parts) and include information about the object that you want to inject. – Yacoub Massad Jan 11 '16 at 21:14
  • @YacoubMassad I don't have any xml configuration as of now. I am just looking for some sample suggestion. – sandip ray Jan 12 '16 at 10:05

1 Answers1

0

My preferred approach is probably to create a factory for creating IInterfaces

public interface IInterface
{
  void SomeMethod1(); 
}

public interface IInterfaceFactory
{
    IInterface CreateInterface();
}

public class StandardInterfaceFactory : IInterfaceFactory
{
    // Define your static lock object here. Other customers 
    // can define their own IInterfaceFactory to use a 
    // different lock object.
    private static readonly object lockObject = new object();

    public IInterface CreateInterface()
    {
        return new StandardInterface(lockObject);
    }
}

public class StandardInterface : IInterface
{
    private readonly object lockObject;

    public StandardInterface(object lockObject)
    {
        this.lockObject = lockObject;
    }

    public void SomeMethod1()
    {
        lock (this.lockObject)
        {
            Console.WriteLine("I've locked on " + lockObject);
        }
    }
}

Your unity configuration and client code will then look like this.

void Main()
{
    IUnityContainer container = new UnityContainer();

    // This mapping can be done trivially in XML configuration.
    // Left as an exercise for the reader :)
    container.RegisterType<IInterfaceFactory, StandardInterfaceFactory>();

    IInterfaceFactory factory = container.Resolve<IInterfaceFactory>();

    IInterface myInterface = factory.CreateInterface();

    myInterface.SomeMethod1();
}
RB.
  • 36,301
  • 12
  • 91
  • 131