WCF依赖注入和抽象工厂 [英] WCF Dependency injection and abstract factory

查看:129
本文介绍了WCF依赖注入和抽象工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个wcf方法

  Profile GetProfileInfo(string profileType,string profileName)

和业务规则:



如果profileType是从数据库读取的A。



如果profileType是从xml文件读取的B。



问题是:如何使用一个依赖注入容器?

解决方案

我们首先假设你有一个这样的IProfileRepository:

  public interface IProfileRepository 
{
Profile GetProfile(string profileName);
}

以及两个实现: DatabaseProfileRepository XmlProfileRepository 。您可以根据profileType的值选择正确的问题。



您可以通过引入此抽象工厂

  public interface IProfileRepositoryFactory 
{
IProfileRepository Create(string profileType);
}

假设IProfileRepositoryFactory已注入到服务实现中,您现在可以实现GetProfileInfo方法如下:

  public Profile GetProfileInfo(string profileType,string profileName)
{
return this.factory.Create(profileType).GetProfile(PROFILENAME);
}

IProfileRepositoryFactory的具体实现可能如下所示:

  public class ProfileRepositoryFactory:IProfileRepositoryFactory 
{
private readonly IProfileRepository aRepository;
私有只读IProfileRepository bRepository;

public ProfileRepositoryFactory(IProfileRepository aRepository,
IProfileRepository bRepository)
{
if(aRepository == null)
{
throw new ArgumentNullException aRepository);
}
if(bRepository == null)
{
throw new ArgumentNullException(bRepository);
}

this.aRepository = aRepository;
this.bRepository = bRepository;


public IProfileRepository Create(string profileType)
{
if(profileType ==A)
{
return this。 aRepository;
}
if(profileType ==B)
{
return this.bRepository;
}

//等等...
}
}

现在,您只需要选择您所选择的DI容器即可连线...


I have this wcf method

Profile GetProfileInfo(string profileType, string profileName)

and a business rule:

if profileType is "A" read from database.

if profileType is "B" read from xml file.

The question is: how to implement it using a dependency injection container?

解决方案

Let's first assume that you have an IProfileRepository something like this:

public interface IProfileRepository
{
     Profile GetProfile(string profileName);
}

as well as two implementations: DatabaseProfileRepository and XmlProfileRepository. The issue is that you would like to pick the correct one based on the value of profileType.

You can do this by introducing this Abstract Factory:

public interface IProfileRepositoryFactory
{
    IProfileRepository Create(string profileType);
}

Assuming that the IProfileRepositoryFactory has been injected into the service implementation, you can now implement the GetProfileInfo method like this:

public Profile GetProfileInfo(string profileType, string profileName)
{
    return this.factory.Create(profileType).GetProfile(profileName);
}

A concrete implementation of IProfileRepositoryFactory might look like this:

public class ProfileRepositoryFactory : IProfileRepositoryFactory
{
    private readonly IProfileRepository aRepository;
    private readonly IProfileRepository bRepository;

    public ProfileRepositoryFactory(IProfileRepository aRepository,
        IProfileRepository bRepository)
    {
        if(aRepository == null)
        {
            throw new ArgumentNullException("aRepository");
        }
        if(bRepository == null)
        {
            throw new ArgumentNullException("bRepository");
        }

        this.aRepository = aRepository;
        this.bRepository = bRepository;
    }

    public IProfileRepository Create(string profileType)
    {
        if(profileType == "A")
        {
            return this.aRepository;
        }
        if(profileType == "B")
        {
            return this.bRepository;
        }

        // and so on...
    }
}

Now you just need to get your DI Container of choice to wire it all up for you...

这篇关于WCF依赖注入和抽象工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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