繁体   English   中英

使用 microsoft DI 创建工厂

[英]Create factory with using microsoft DI

我有 http reauest 的层次结构类和通用类,响应:

        public abstract class Publication
        {
            public string Name { get; set; }
            public string Producer { get; set; }
        }


        public class Book : Publication
        {
            public string Topic { get; set; }
            public string Autor { get; set; }
        }

        public class CopyBook : Publication
        {
            public string Topic { get; set; }
            public int CountPages { get; set; }
        }

        public class NoteBook : Publication
        {
            public int Cost { get; set; }
        }


        public class Request<T> where T : Publication
        {
            public Guid RequestId { get; }
            public T Publication { get; }

            public Request(T publication)
            {
                RequestId = Guid.NewGuid();
                Publication = publication;
            }
        }

        public class Response<T> where T : Publication
        {
            private Guid ResponseId { get; set; }
            public T Publication { get; set; }
        }

我使用带有 Refit 库的 Http API 客户端。 为此,我创建了界面:

        public interface IPublicationApiClinet<T> where T : Publication
        {
            bool Send(Request<T> publication);
            Response<T> UpdatingRecv(Request<T> publication);
        }

改装库自动为接口创建实现。 改装库不使用基类 IFormApiClinet 注入客户端,只有子类 - 例如 IFormApiClinet。

我想根据类型发布创建工厂以创建客户。 我创造了她:

        public interface IClientFactory
        {
            IPublicationApiClient<Publication> Create(Publication publication);
        }

        public class ClientFactory : IClientFactory
        {
            private readonly IServiceProvider _serviceProvider;
            public ClientFactory(IServiceProvider serviceProvider)
            {
                _serviceProvider = serviceProvider;
            }

            public IPublicationApiClient<Publication> Create(Publication publication)
            {
                switch (publication)
                {
                    case Book _:
                        return _serviceProvider.GetService<IPublicationApiClient<Book>>();
                    case NoteBook _:
                        return _serviceProvider.GetService<IPublicationApiClient<NoteBook>>();
                    case CopyBook _:
                        return _serviceProvider.GetService<IPublicationApiClient<CopyBook>>();
                    default:
                        throw new NotSupportedException();
                }
            }
        }

工厂无法编译,因为带有子类的 IPublicationApiClient 不会强制转换为基类返回。 例如IPublicationApiClient<Book>IPublicationApiClient<Publication> 怎么修?

我将使用服务中的工厂,它从 DB 获取不同类型的出版物(书籍、复印本、笔记本)。

        public interface IPublicationSerice
        {
            void Proccess(string name);
        }

         public class PublicationSerice : IPublicationSerice 
         {
            private readonly IClientFactory _clientFactory;
            public PublicationSerice(IClientFactory clientFactory)
            {
                _clientFactory = clientFactory;
            }

            public void Proccess(string name)
            {
                Publication book = new Book();
                //It is analog:
                //var specification = new PublicationByNameSpecification<Publication>(name);
                //var publication = await _publicationRepository.GetAsync(specification);

                var bookClient = _clientFactory.Create(book);
                bookClient.Send(new Request<Publication>(book));
            }
        }

您也可以通过使ClientFactory方法通用来做到这一点:

public interface IClientFactory
{
    IPublicationApiClient<T> Create<T>(T publication) where T : Publication;
}

public class ClientFactory : IClientFactory
{
    private readonly IServiceProvider _serviceProvider;
    public ClientFactory(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;
    }

    public IPublicationApiClient<T> Create<T>(T publication) where T : Publication
    {
        return _serviceProvider.GetService<IPublicationApiClient<T>>()
            ?? throw new NotSupportedException();
    }
}

这是该工作的一个简单示例:

public class PublicationApiClient<T> : IPublicationApiClient<T> where T : Publication
{
    public bool Send(Request<T> publication)
    {
        Console.WriteLine($"Hello from {typeof(T).Name} client!");
        return true;
    }

    public Response<T> UpdatingRecv(Request<T> publication)
    {
        throw new NotImplementedException();
    }
}

static void Main(string[] args)
{
    ServiceProvider provider = new ServiceCollection()
        .AddSingleton<IClientFactory, ClientFactory>()
        .AddSingleton(typeof(IPublicationApiClient<>), typeof(PublicationApiClient<>))
        .BuildServiceProvider();

    IClientFactory factory = provider.GetService<IClientFactory>();

    Book book = new Book();
    IPublicationApiClient<Book> bookClient = factory.Create(book);
    bookClient.Send(new Request<Book>(book));

    NoteBook notebook = new NoteBook();
    IPublicationApiClient<NoteBook> notebookClient = factory.Create(notebook);
    notebookClient.Send(new Request<NoteBook>(notebook));

    Console.ReadLine();
}

产生以下输出:

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM