Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.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# 统一应用程序块,如何向注塑厂传递参数?_C#_.net_Ioc Container_Unity2.0 - Fatal编程技术网

C# 统一应用程序块,如何向注塑厂传递参数?

C# 统一应用程序块,如何向注塑厂传递参数?,c#,.net,ioc-container,unity2.0,C#,.net,Ioc Container,Unity2.0,这就是我现在拥有的 Container.RegisterType<IUserManager, UserManagerMock>(); Container.RegisterType<IUser, UserMock>( new InjectionFactory( (c) => c.Resolve<IUserManager>().GetUser("John"))); Contai

这就是我现在拥有的

  Container.RegisterType<IUserManager, UserManagerMock>();
  Container.RegisterType<IUser, UserMock>(
                new InjectionFactory(
                    (c) => c.Resolve<IUserManager>().GetUser("John")));
Container.RegisterType();
Container.RegisterType(
新注射厂(
(c) =>c.Resolve().GetUser(“John”);
明白了吗

Container.Resolve<IProfile>();
Container.Resolve();
我想把一个名称作为参数传递给工厂,这样我就能够解析名为的用户对象; 大概是这样的:

 Container.Resolve<IProfile>("Jonh");
Container.Resolve(“john”);

如何更改此案例的类型注册

Resolve方法允许传递ResolverRide的参数。ResolverOverride的子类型是ParameterOverride,可用于将参数传递给已解析的构造函数

您可以这样做(参数是Name,传递的值是John):

Container.Resolve(新参数覆盖(“Name”、“John”);

虽然大多数DI框架都有高级功能来完成这些类型的注册,但我个人宁愿改变应用程序的设计来解决这个问题。这使得我的DI配置简单,代码更容易理解。特别是对于依赖于某些上下文(线程、请求等)或具有必须显式管理的生存期的对象的创建,我喜欢定义工厂。工厂使这些事情更加明确

在您的情况下,您希望获取某个用户的配置文件。这通常是你想要有一个工厂的东西。下面是一个例子:

// Definition
public interface IProfileFactory
{
    IProfile CreateProfileForUser(string username);
}

// Usage
var profile = Container.Resolve<IProfileFactory>()
    .CreateProfileForUser("John");

// Registration
Container.RegisterType<IProfileFactory, ProfileFactory>();

// Mock implementation
public class ProfileFactory : IProfileFactory
{
    public IProfile CreateProfileForUser(string username)
    {
        IUser user = Container.Resolve<IUserManager>()
            .GetUser(username);

        return new UserProfile(user);
    }
}
//定义
公共接口IProfileFactory
{
IProfile CreateProfileForUser(字符串用户名);
}
//用法
var profile=Container.Resolve()
.CreateProfileForUser(“约翰”);
//登记
Container.RegisterType();
//模拟实现
公共类ProfileFactory:IProfileFactory
{
公共IProfile CreateProfileForUser(字符串用户名)
{
IUser user=Container.Resolve()
.GetUser(用户名);
返回新的UserProfile(用户);
}
}

我希望这能有所帮助。

就我个人而言,我根本不会尝试通过服务位置检索IUser/IProfile。只需通过服务位置检索IUserManager,然后在代码中调用GetUser(“John”)就可以了。我同意John的观点。如果可能,尝试使用依赖项注入,而不是服务位置。这允许您保持代码不受容器调用的影响,并且在您的应用程序中有一个(或最多几个)地方调用容器。我认为这样的示例(我们调用容器)让我感到困惑的是您直接调用容器。我不会引用这句狡猾的谚语,但你知道我的意思……史蒂文。。非常感谢,简单,好,有效。。它对meTrue很有吸引力,但您无法将这些参数传递到工厂方法中
// Definition
public interface IProfileFactory
{
    IProfile CreateProfileForUser(string username);
}

// Usage
var profile = Container.Resolve<IProfileFactory>()
    .CreateProfileForUser("John");

// Registration
Container.RegisterType<IProfileFactory, ProfileFactory>();

// Mock implementation
public class ProfileFactory : IProfileFactory
{
    public IProfile CreateProfileForUser(string username)
    {
        IUser user = Container.Resolve<IUserManager>()
            .GetUser(username);

        return new UserProfile(user);
    }
}