简体   繁体   English

按类型处理对象列表

[英]Process list of object by type

I have a requirement to create a Sync method. 我需要创建一个Sync方法。 1) On input it takes SyncRequest object. 1)输入时,将使用SyncRequest对象。

class SyncRequest{
    public List<SyncObj> Objects{get;set;}
}
class SyncObj{
    public Type Type{get;set;}
    public object Object{get;set;}
}

2) Based on type of each object i need to use different service to proceed and the different repository to persist the data. 2)根据每个对象的类型,我需要使用不同的服务来进行操作,并需要使用不同的存储库来持久化数据。

Ie if i got the 3 objects with types User, User, Task. 即,如果我得到3个对象,类型分别为User,User,Task。 I will group them by type, and then Proceed with UserService and TaskService. 我将按类型对它们进行分组,然后继续执行UserService和TaskService。
I'm trying to architect my web.api with Onion Architecture. 我正在尝试使用Onion Architecture构建我的web.api。 Where DataAccess, Core, Services are separated of each other. DataAccess,Core,Service彼此分开的地方。 And i want avoid swich or if's, in my code. 我想在我的代码中避免使用swich或if。

All ideas i came up was ugly. 我提出的所有想法都很丑陋。 For example Dicitionary<Type, Service> , and manual casting. 例如Dicitionary<Type, Service>和手动转换。

I feel that Ninject might have really beatifull answer on my problem, with some factory functionality, but i can't come up with it. 我觉得Ninject可能具有某些工厂功能,对我的问题确实很满意,但是我无法提出。

Can anyone describe how this pattern should work. 谁能描述这种模式应该如何工作。 Or how this pattern calls, where to read. 或该模式如何调用,在何处读取。

Since all you've got is a Type one way or another you're going to have to use Reflection. 由于您所拥有的只是一种或另一种Type您将不得不使用Reflection。 The other question is how you're accessing the DI container (ninject kernel). 另一个问题是您如何访问DI容器(ninject内核)。

You can do something like this: 您可以执行以下操作:

public interface IHandler<T>
{
    void Handle(T obj);
}

public void UserService : IHandler<User> {...}

Bind<IHandler<User>>().To<UserService>();

which you would then use like this: 然后您将使用以下代码:

foreach(var syncObj in syncRequest.Objects) 
{
    Type handlerType = typeof(IHandler<>).MakeGeneric(syncObj.Type);
    MethodInfo handleMethod = handlerType.GetMethod("Handle");

    object handler = kernel.Get(handlerType);

    handleMethod.Invoke(handler, new object[] { syncObj.Object });
}

As you see you'll need access to the container somehow to create the specific type. 如您所见,您将需要以某种方式访问容器创建特定类型。 You can have it injected as IResolutionRoot . 您可以将其作为IResolutionRoot注入。 To adhere to the Onion Layering you'll have to move the code that uses the kernel to the composition root and give it an interface... Since you seem to know about onion layering i won't go into more detail :) 要坚持洋葱分层,您必须将使用内核的代码移至组合根并为其提供一个接口...因为您似乎对洋葱分层有所了解,所以我不再赘述:)

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

相关问题 对象列表的匿名类型 - Anonymous type to List of object 转换类型列表<Object>反对 - Convert type List<Object> to Object 对象列表到预定类型的列表 - List of object to List of predetermined type 将类型对象转换为列表类型<object> - Convert Type object to Type of List<object> 将类类型的列表分配给对象类型列表 - Assign list of a class type to object type list 转换类型为List的对象 <object> 列出 <string> - Convert object of type List<object> to List<string> 处理列表中的项目<object>通过一组线程 - 线程池<div id="text_translate"><pre>public void multithread() { List&lt;Bbject&gt; objectlist= new List&lt;Object&gt;{ //consider it has around 10 objects }; foreach (var obj in objectlist) { Process(obj); } } private void process(Object obj) { //Implementation }</pre><p> 我正在尝试实现多线程,其中每个线程采用 object 并对其进行处理...我尝试创建 5 个单独的线程,其中每个线程处理 2 个对象...但它更像是硬配置...有什么方法可以使用它来实现它线程池...</p></div></object> - Process items in list<Object> by a set of threads - threadpooling 转换以列出匿名类型的对象 - Convert to list a object with anonymous type 将对象类型转换为列表<T> - Convert Object type to List<T> 从列表中选择对象类型 - select from list by object type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM