Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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# 在dotnetcore中解析通用服务_C#_.net - Fatal编程技术网

C# 在dotnetcore中解析通用服务

C# 在dotnetcore中解析通用服务,c#,.net,C#,.net,我使用的API在数据可访问时触发以下方法 public void Process(IData data) IData由几种数据类型(即EventData和RegionData)实现,只要有新数据可用,它们都将触发此方法 此传入数据应通过IDataMapper映射到我的实体: IDataMapper<T,K> where T : IData where K : IEntity 所有数据映射程序的注册方式如下: services.AddTransient<IDataMapper

我使用的API在数据可访问时触发以下方法

public void Process(IData data)
IData
由几种数据类型(即
EventData
RegionData
)实现,只要有新数据可用,它们都将触发此方法

此传入数据应通过
IDataMapper
映射到我的实体:

IDataMapper<T,K> where T : IData where K : IEntity
所有数据映射程序的注册方式如下:

services.AddTransient<IDataMapper<EventData, EventEntity>, EventMapper>();
services.AddTransient<IDataMapper<RegionData, RegionEntity>, RegionMapper>();
services.AddTransient();
services.AddTransient();

有没有可能使它更通用?

您也可以使映射器更通用,并将类型检查问题转移到该类中。这样,您就可以“只”注册一个映射器类型。这也是微软ILogger的工作原理

示例代码:

using Microsoft.Extensions.DependencyInjection;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddTransient(typeof(IDataMapper<,>), typeof(DataMapper<,>));
            serviceCollection.AddTransient<EventService>();
            serviceCollection.AddTransient<RegionService>();
            var provider = serviceCollection.BuildServiceProvider();
            var eventService = provider.GetRequiredService<EventService>();
            var regionService = provider.GetRequiredService<RegionService>();
        }
    }

    class EventService
    {
        public EventService(IDataMapper<EventData, EventEntity> mapper)
        {
            mapper.Map(new EventData(), new EventEntity());
        }
    }

    class RegionService
    {
        public RegionService(IDataMapper<RegionData, RegionEntity> mapper)
        {
            mapper.Map(new RegionData(), new RegionEntity());
        }
    }

    public interface IData { }

    public interface IEntity { }

    public interface IDataMapper<T, K> where T : IData where K : IEntity
    {
        void Map(T data, K entity);
    }

    public class EventData : IData { }

    public class RegionData : IData { }

    public class EventEntity : IEntity { }

    public class RegionEntity : IEntity { }

    public class DataMapper<T, K> : IDataMapper<T, K> where T : IData where K : IEntity
    {
        public void Map(T data, K entity)
        {
            if (data is EventData eData && entity is EventEntity eEntity)
            {
                // map event
            }
            else if (data is RegionData rData && entity is RegionEntity rEntity)
            {
                // map region
            }
        }
    }
}
使用Microsoft.Extensions.DependencyInjection;
名称空间控制台EAPP1
{
班级计划
{
静态void Main(字符串[]参数)
{
var servicecolection=新servicecolection();
AddTransient(typeof(IDataMapper)、typeof(DataMapper));
serviceCollection.AddTransient();
serviceCollection.AddTransient();
var provider=serviceCollection.BuildServiceProvider();
var eventService=provider.GetRequiredService();
var regionService=provider.GetRequiredService();
}
}
类事件服务
{
公共事件服务(IDataMapper mapper)
{
Map(new EventData(),new EventEntity());
}
}
类区域服务
{
公共区域服务(IDataMapper mapper)
{
Map(newregiondata(),newregionEntity());
}
}
公共接口IData{}
公共接口属性{}
公共接口IDataMapper,其中T:IData,其中K:Entity
{
空洞图(T数据,K实体);
}
公共类EventData:IData{}
公共类区域数据:IData{}
公共类EventEntity:IEntity{}
公共类RegionEntity:Entity{}
公共类数据映射器:IDataMapper其中T:IData其中K:Entity
{
公共空图(T数据,K实体)
{
if(数据为EventData数据&实体为EventEntity)
{
//地图事件
}
else if(数据为RegionData rData&&entity为RegionEntity)
{
//地图区域
}
}
}
}

似乎是一个不错的解决方案。感谢您的快速回复:)
using Microsoft.Extensions.DependencyInjection;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var serviceCollection = new ServiceCollection();
            serviceCollection.AddTransient(typeof(IDataMapper<,>), typeof(DataMapper<,>));
            serviceCollection.AddTransient<EventService>();
            serviceCollection.AddTransient<RegionService>();
            var provider = serviceCollection.BuildServiceProvider();
            var eventService = provider.GetRequiredService<EventService>();
            var regionService = provider.GetRequiredService<RegionService>();
        }
    }

    class EventService
    {
        public EventService(IDataMapper<EventData, EventEntity> mapper)
        {
            mapper.Map(new EventData(), new EventEntity());
        }
    }

    class RegionService
    {
        public RegionService(IDataMapper<RegionData, RegionEntity> mapper)
        {
            mapper.Map(new RegionData(), new RegionEntity());
        }
    }

    public interface IData { }

    public interface IEntity { }

    public interface IDataMapper<T, K> where T : IData where K : IEntity
    {
        void Map(T data, K entity);
    }

    public class EventData : IData { }

    public class RegionData : IData { }

    public class EventEntity : IEntity { }

    public class RegionEntity : IEntity { }

    public class DataMapper<T, K> : IDataMapper<T, K> where T : IData where K : IEntity
    {
        public void Map(T data, K entity)
        {
            if (data is EventData eData && entity is EventEntity eEntity)
            {
                // map event
            }
            else if (data is RegionData rData && entity is RegionEntity rEntity)
            {
                // map region
            }
        }
    }
}