如何注册通用服务 [英] How to register generic service

查看:185
本文介绍了如何注册通用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算将我的系统移至通用服务层。

  public interface IAbcService< TEntity>其中TEntity:class 

public class AbcService< TEntity> :IAbcService< TEntity>其中TEntity:class

我尝试了一些功能,但无效。

  services.AddTransient< typeof(IAbcService< MyEntity>),AbcService(); 
....
....
....





如何在asp.net中注册一个通用服务?

我如何在asp.net中注册一个通用服务核心?

解决方案

之前我想尝试完全相同的事情,并且我已经像这样工作:

  services.AddTransient(typeof(IAbcService<>),typeof(AbcService<>)); 
services.AddTransient(typeof(IAbcService< Person>),typeof(AbcService< Person>));

请注意不同的结构,这是通过参数在一个方法中注册的。



详细说明,正如您在上面的示例中所看到的那样,使用新的DNX框架包,我们现在可以注册开放式和封闭式泛型。
试试看,两条线都可以使用。


I am planning to move my system to generic service layer.

public interface IAbcService<TEntity> where TEntity : class

public class AbcService<TEntity> : IAbcService<TEntity> where TEntity : class

I tried something but it does not work.

services.AddTransient<typeof(IAbcService<MyEntity>), AbcService();
....
....
....

I'm using some of my entities and along with that I am trying to add it to properties.

How can I register a generic service in asp.net core?

解决方案

I wanted to try the exact same thing before and I've got it to work like this:

services.AddTransient(typeof(IAbcService<>), typeof(AbcService<>));
services.AddTransient(typeof(IAbcService<Person>), typeof(AbcService<Person>));

Do mind the different structure, this is registering by parameters in a method.

To elaborate, as you can see in the example above, with the new DNX framework package we now can register both open and closed generics. Try it out, both lines will work.

这篇关于如何注册通用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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