团结辛格尔顿code [英] Unity Singleton Code

查看:111
本文介绍了团结辛格尔顿code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来团结,然后我试图写一个初始化一些统一的逻辑和注册/解决单一实例电子邮件对象的,以便它可以在几个其他对象使用,其中一个例子在下面被OperationEntity。

所以当它注册它将填充电子邮件单从配置文件的一些值,则只要创建OperationEntity的一个实例(在我的情况下,它被反序列化),它使用相同的电子邮件单身。所以我所有的客户端逻辑需要做的是反序列化OperationEntity和调用的performAction() - 随着由Unity照顾的电子邮件实例

 公共接口IEmail
{
    字符串FROMNAME {搞定;组; }
    字符串FromEmailAddress {搞定;组; }
}公共类电子邮箱:IEmail
{
    公共字符串FROMNAME {搞定;组; }
    公共字符串FromEmailAddress {搞定;组; }    公共电子邮件(字符串FROMNAME,串fromEmailAddress)
    {
        FROMNAME = FROMNAME;
        FromEmailAddress = fromEmailAddress;
    }
}公共类OperationEntity
{
    私人只读IEmail _EMAIL;    公众诠释OperationId {搞定;组; }
    公共字符串OperationName {搞定;组; }
    公共字符串的toAddress {搞定;组; }    公共OperationEntity(IEmail电子邮件)
    {
        _EMAIL =电子邮件;
    }    公共无效的performAction()
    {
        _email.ToAddress =的toAddress;
        _email.Body =一些电子邮件正文;
        _email.Deliver();
    }
}

任何帮助将是在获得这种团结code工作pciated AP $ P $

 公共静态无效的注册(IUnityContainer容器)
    {
        容器
            .RegisterType< IEmail,电子邮件和GT;(
            新InjectionFactory(C =>新建电子邮件(
                命名,
                to@email.com)));        VAR电子邮件= container.Resolve< IEmail>();        container.RegisterType< OperationEntity>(
            电子邮件,新ContainerControlledLifetimeManager()
            新InjectionConstructor(电子邮件));
    }


解决方案

首先,你需要一个适当的终身经理

<一个href=\"http://msdn.microsoft.com/en-us/library/ff647854.aspx\">http://msdn.microsoft.com/en-us/library/ff647854.aspx

该ContainerControlledLifetimeManager是单身。

有关定制的初始化,你很可能InjectionFactory

<一个href=\"http://bartwullems.blogspot.com/2011/03/unity-injectionfactory.html\">http://bartwullems.blogspot.com/2011/03/unity-injectionfactory.html

这可以让你写任何code它初始化的实体。

EDIT1:这应该有助于

 公共静态无效的注册(IUnityContainer容器)
{
    容器
        .RegisterType&LT; IEmail,电子邮件和GT;(
        新ContainerControlledLifetimeManager(),
        新InjectionFactory(C =&gt;新建电子邮件(
            命名,
            to@email.com)));
}

然后

  VAR opEntity = container.Resolve&LT; OperationEntity&GT;();

EDIT2:为了支持序列化,你有你反序列化后重建依赖关系:

 公共类OperationEntity
{
   //使公众和标记为依赖
   [依赖]
   公共IEmail _EMAIL {搞定;组;}}

然后

  OperationEntity实体= somehowdeserializeit;//让团结重建的依赖
container.BuildUp(实体);

I'm new to Unity and am trying to write some Unity logic which initialises and register/resolves a singleton instance of the Email object so that it can be used across several other objects, one example below being OperationEntity.

So when it's registered it populates the Email singleton with some values from a config file, then whenever an instance of OperationEntity is created (in my case it's being deserialized) it uses that same Email singleton. So all my client logic needs to do is deserialize OperationEntity and call PerformAction() - with the email instance taken care of by Unity.

public interface IEmail
{
    string FromName { get; set; }
    string FromEmailAddress { get; set; }
}

public class Email : IEmail
{
    public string FromName { get; set; }
    public string FromEmailAddress { get; set; }

    public Email(string fromName, string fromEmailAddress)
    {
        FromName = fromName;
        FromEmailAddress = fromEmailAddress;
    }
}

public class OperationEntity
{
    private readonly IEmail _email;

    public int OperationId { get; set; }
    public string OperationName { get; set; }
    public string ToAddress { get; set; }

    public OperationEntity(IEmail email)
    {
        _email = email;
    }

    public void PerformAction()
    {
        _email.ToAddress = ToAddress;
        _email.Body = "Some email body";
        _email.Deliver();
    }
}

Any help would be appreciated in getting this Unity code to work

    public static void Register(IUnityContainer container)
    {
        container
            .RegisterType<IEmail, Email>(
            new InjectionFactory(c => new Email(
                "To Name", 
                "to@email.com")));

        var email = container.Resolve<IEmail>();  

        container.RegisterType<OperationEntity>(
            "email", new ContainerControlledLifetimeManager(),
            new InjectionConstructor(email));
    }

解决方案

First, you need a proper lifetime manager

http://msdn.microsoft.com/en-us/library/ff647854.aspx

The ContainerControlledLifetimeManager is for singletons.

For custom initialization, you could probably use InjectionFactory

http://bartwullems.blogspot.com/2011/03/unity-injectionfactory.html

This lets you write any code which initializes the entity.

Edit1: this should help

public static void Register(IUnityContainer container)
{
    container
        .RegisterType<IEmail, Email>(
        new ContainerControlledLifetimeManager(),
        new InjectionFactory(c => new Email(
            "To Name", 
            "to@email.com")));
}

and then

var opEntity = container.Resolve<OperationEntity>();

Edit2: To support serialization, you'd have to rebuild dependencies after you deserialize:

public class OperationEntity
{
   // make it public and mark as dependency   
   [Dependency]
   public IEmail _email { get; set;}

}

and then

OperationEntity entity = somehowdeserializeit;

// let unity rebuild your dependencies
container.BuildUp( entity );

这篇关于团结辛格尔顿code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆