Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MS unity容器_C#_Unity Container - Fatal编程技术网

C# MS unity容器

C# MS unity容器,c#,unity-container,C#,Unity Container,我有一个界面,它看起来像下面这样- public interface IInterface { void SomeMethod1(); void SomeMethod2(); void SomeMethod3(); . . . } 其中一个实现类似于- public class

我有一个界面,它看起来像下面这样-

public interface IInterface             
{      
  void SomeMethod1();      
  void SomeMethod2();  
  void SomeMethod3();   
  .                    
  .                    
  .                     
}       
其中一个实现类似于-

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   
     }      
  } 
  .                            
  .                            
  .                                    
}     

如何在通过unity配置向类型IIInterface注册实现类时传递类型Object的静态只读实例?

我首选的方法可能是创建一个工厂来创建
IIInterface
s

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);
        }
    }
}
您的unity配置和客户端代码将如下所示

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();
}
void Main()
{
IUnityContainer容器=新的UnityContainer();
//这种映射可以在XML配置中轻松完成。
//留给读者作为练习:)
container.RegisterType();
IInterfaceFactory=container.Resolve();
IInterface myInterface=factory.CreateInterface();
myInterface.SomeMethod1();
}

我的首选方法可能是创建一个工厂来创建
接口

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);
        }
    }
}
您的unity配置和客户端代码将如下所示

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();
}
void Main()
{
IUnityContainer容器=新的UnityContainer();
//这种映射可以在XML配置中轻松完成。
//留给读者作为练习:)
container.RegisterType();
IInterfaceFactory=container.Resolve();
IInterface myInterface=factory.CreateInterface();
myInterface.SomeMethod1();
}

“静态只读实例”-您指的是单例?在这种情况下,请看一看。我想通过配置文件(而不是代码)来实现,而且我不想拥有实现类的单例实例,而是在向unity注册该类时,我想向它传递一个Object类型的静态只读实例(即类似于“static readonly obj=new Object()”的内容)然后将obj作为参数值传递给实现类的构造函数)您将如何定义“静态只读”?@PeterPorfy-我不确定是否有任何方法可以通过unity config做到这一点。@sandipray当然,这就是为什么我问您的目标是什么。因为“传递Object类型的静态只读实例”毫无意义。“静态只读实例”-你指的是单例?在这种情况下,请看一看。我想通过配置文件(而不是代码)来实现,而且我不想拥有实现类的单例实例,而是在向unity注册该类时,我想向它传递一个Object类型的静态只读实例(即类似于“static readonly obj=new Object()”的内容)然后将obj作为参数值传递给实现类的构造函数)您将如何定义“静态只读”?@PeterPorfy-我不确定是否有任何方法可以通过unity config做到这一点。@sandipray当然,这就是为什么我问您的目标是什么。因为“传递对象类型的静态只读实例”毫无意义。