首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StructureMap问题

StructureMap问题
EN

Stack Overflow用户
提问于 2010-02-02 03:42:28
回答 2查看 276关注 0票数 0

--这相当于我试图用StructureMap:创建的东西

代码语言:javascript
复制
new ChangePasswordWithNotificationAndLoggingService(
new ChangePasswordService(
      new ActiveDirectoryRepository(new ActiveDirectoryCredentials()),
      new TokenRepository("")),
new EmailNotificationService(new PasswordChangedNotification(new UserAccount())),
new LoggingService());

,这就是我现在要做的:

代码语言:javascript
复制
ForRequestedType<IChangePasswordService>()
  .TheDefault.Is.ConstructedBy(() => 
     new ChangePasswordService(DependencyRegistrar.Resolve<IActiveDirectoryRepository>(),
                               DependencyRegistrar.Resolve<ITokenRepository>()))
  .EnrichWith<IChangePasswordService>(x => 
     new ChangePasswordWithNotificationAndLoggingService(x,
                   DependencyRegistrar.Resolve<INotificationService>(),
                   DependencyRegistrar.Resolve<ILoggingService>()));

我需要将UserAccount传递给INotificationService.无法解决。

我尝试过:DependencyRegistrar.With(新的UserAccount { Username = "test“});

没有luck...UserAccount总是结果为null。我不需要用StructureMap来做所有的事情,我愿意接受任何建议。

这就是我目前所做的工作:

代码语言:javascript
复制
public static IChangePasswordService ChangePasswordService(UserAccount userAccount)
{
    return new ChangePasswordWithNotificationService(
        new ChangePasswordService(ActiveDirectoryRepository(), TokenRepository()),
        new EmailNotificationService(new PasswordChangedNotification(userAccount)));
}
EN

回答 2

Stack Overflow用户

发布于 2010-02-02 14:03:57

你试过使用AutoWiring吗?这些都是具有简单构造的具体类,因此StructureMap可以确定您需要什么。

代码语言:javascript
复制
For<IChangePasswordService>().Use<ChangePasswordService>();

看一下您的结构,我认为这个简单的配置可能只是起作用了。

编辑关于评论的

您应该使用用(T实例)方法让容器使用给定的userAccount构造您的IChangePasswordService。

代码语言:javascript
复制
var userAccount = new UserAccount("derans"); 
var changePasswordService = container.With(userAccount).GetInstance<IChangePasswordService>();
票数 0
EN

Stack Overflow用户

发布于 2010-02-05 12:23:47

为什么不将更改密码服务的创建封装到工厂中--然后工厂被实现为StructureMap工厂,它使用传入的UserAccount和“ObjectFactory”根据需要创建IIChangePasswordService的实例?

我在下面演示了一下:

代码语言:javascript
复制
namespace SMTest
{
class Program
{
    static void Main(string[] args)
    {
        // bootstrapper...
        ObjectFactory.Configure(x => x.AddRegistry(new TestRegistry()));

        // create factory for use later (IoC manages this)...
        var changePasswordServiceFactory = ObjectFactory.GetInstance<IChangePasswordServiceFactory>();

        var daveAccount = new UserAccount("Dave Cox");
        var steveAccount = new UserAccount("Steve Jones");

        var passwordService1 = changePasswordServiceFactory.CreateForUserAccount(daveAccount);
        var passwordService2 = changePasswordServiceFactory.CreateForUserAccount(steveAccount);


    }
}

public class TestRegistry : Registry
{
    public TestRegistry()
    {
        Scan(x =>
                 {
                     x.TheCallingAssembly();
                     x.AssemblyContainingType(typeof(IChangePasswordService));
                     x.AssemblyContainingType(typeof(IActiveDirectoryRepository));
                     x.AssemblyContainingType(typeof(IActiveDirectoryCredentials));
                     x.AssemblyContainingType(typeof(ITokenRepository));
                     x.AssemblyContainingType(typeof(INotification));
                     x.AssemblyContainingType(typeof(INotificationService));
                     x.AssemblyContainingType(typeof(ILoggingService));

                     ForRequestedType<ILoggingService>().TheDefault.Is.OfConcreteType<MyLogger>();

                     ForRequestedType<IActiveDirectoryRepository>().TheDefault.Is.OfConcreteType<MyAdRepository>();
                     ForRequestedType<IActiveDirectoryCredentials>().TheDefault.Is.OfConcreteType<MyAdCredentials>();

                     ForRequestedType<ITokenRepository>().TheDefault.Is.OfConcreteType<MyTokenRepository>();

                     ForRequestedType<IChangePasswordService>().TheDefault.Is.OfConcreteType<ChangePasswordService>();
                     ForRequestedType<IChangePasswordServiceFactory>().CacheBy(InstanceScope.Singleton).TheDefault.Is.OfConcreteType<StructureMapChangePasswordServiceFactory>();

                     ForRequestedType<INotification>().TheDefault.Is.OfConcreteType<MyPasswordChangedNotification>();
                     ForRequestedType<INotificationService>().TheDefault.Is.OfConcreteType<MyEmailNotificationService>();
                 });
    }
}

public interface ILoggingService
{
}

public class MyLogger : ILoggingService
{
}

public class UserAccount
{
    public string Name { get; private set; }

    public UserAccount(string name)
    {
        Name = name;
    }
}

public interface INotification
{
}

public class MyPasswordChangedNotification : INotification
{
    private readonly UserAccount _account;
    private readonly ILoggingService _logger;

    public MyPasswordChangedNotification(UserAccount account, ILoggingService logger)
    {
        _account = account;
        _logger = logger;
    }
}

public interface INotificationService
{
}

public class MyEmailNotificationService : INotificationService
{
    private readonly INotification _notification;
    private readonly ILoggingService _logger;

    public MyEmailNotificationService(INotification notification, ILoggingService logger)
    {
        _notification = notification;
        _logger = logger;
    }
}

public interface ITokenRepository
{
}

public class MyTokenRepository : ITokenRepository
{
}

public interface IActiveDirectoryRepository
{
}

public interface IActiveDirectoryCredentials
{
}

public class MyAdCredentials : IActiveDirectoryCredentials
{
}

public class MyAdRepository : IActiveDirectoryRepository
{
    private readonly IActiveDirectoryCredentials _credentials;

    public MyAdRepository(IActiveDirectoryCredentials credentials)
    {
        _credentials = credentials;
    }
}

public interface IChangePasswordService
{
}

public class ChangePasswordService : IChangePasswordService
{
    private readonly IActiveDirectoryRepository _adRepository;
    private readonly ITokenRepository _tokenRepository;
    private readonly INotificationService _notificationService;

    public ChangePasswordService(IActiveDirectoryRepository adRepository, ITokenRepository tokenRepository, INotificationService notificationService)
    {
        _adRepository = adRepository;
        _tokenRepository = tokenRepository;
        _notificationService = notificationService;
    }
}

public interface IChangePasswordServiceFactory
{
    IChangePasswordService CreateForUserAccount(UserAccount account);
}

public class StructureMapChangePasswordServiceFactory : IChangePasswordServiceFactory
{
    public IChangePasswordService CreateForUserAccount(UserAccount account)
    {
        return ObjectFactory.With(account).GetInstance < IChangePasswordService>();
    }
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2181750

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档