小编Vij*_*jay的帖子

如何添加通用依赖注入

使用只读api服务并利用泛型将操作打包到基于约定的流程中.

存储库界面:

public interface IRepository<TIdType,TEntityType> where TEntityType:class {
   Task<EntityMetadata<TIdType>> GetMetaAsync();
}
Run Code Online (Sandbox Code Playgroud)

存储库实现:

public class Repository<TIdType,TEntityType> : IRepository<TIdType,TEntityType> where TEntityType:class {
   public Repository(string connectionString) { // initialization }
   public async Tas<EntityMetadata<TIdType>> GetMetaAsync() { // implementation   }
}
Run Code Online (Sandbox Code Playgroud)

Startup.cs -> ConfigureServices:

services.AddSingleton<IRepository<int, Employee>> ( p=> new Repository<int, Employee>(connectionString));
services.AddSingleton<IRepository<int, Department>> ( p=> new Repository<int, Department>(connectionString));
// and so on
Run Code Online (Sandbox Code Playgroud)

控制器:

public class EmployeeController : Controller {
   public EmployeeController(IRepository<int,Employee> repo) {//stuff}
}
Run Code Online (Sandbox Code Playgroud)

我目前正在重复所有类型的实体类型的存储库实现ConfigureServices.有没有办法让这个通用呢?

services.AddSingleton<IRepository<TIdType, TEntityType>> ( p=> new Repository<TIdType, …
Run Code Online (Sandbox Code Playgroud)

c# generics dependency-injection .net-core asp.net-core

9
推荐指数
1
解决办法
6556
查看次数

如何结合go范围内的第一位和第一位

我是Go和Hugo网站生成器的新手,目前正在创建一个简单的主题.我试图结合where过滤器和first功能,我无法使其工作.

我想要的是获得该post部分的前10个项目

{{ range where .Data.Pages "Section" "post" }}
    <li><a href="{{.RelPermalink}}">{{.Title}}</a> <em>{{.Summary}}</em></li>
{{ end }}
Run Code Online (Sandbox Code Playgroud)

以上工作正常,但如何让它只返回前10项(以下不起作用):

{{ range first 10 where .Data.Pages "Section" "post" }}
    <li><a href="{{.RelPermalink}}">{{.Title}}</a> <em>{{.Summary}}</em></li>
{{ end }}
Run Code Online (Sandbox Code Playgroud)

go hugo

1
推荐指数
1
解决办法
877
查看次数

在golang中传递字符串参数

刚开始使用Go并遇到基本函数调用问题:

fileContentBytes := ioutil.ReadFile("serverList.txt")
fileContent := string(fileContentBytes)
serverList := strings.Split(fileContent,"\n")
   /*
serverList:
server1,
server2,
server3
   */
for _,server := range serverList {
   fmt.Println("sending ", server)
   processServer(server)
}

func processServer(s string) {
   fmt.Println(s, " : started processing")
}
Run Code Online (Sandbox Code Playgroud)

输出:

sending server1
 : started processing
sending server2
 : started processing
sending server3
 server3: started processing
Run Code Online (Sandbox Code Playgroud)

在for循环的上面代码中,我能够打印数组的所有元素,但只有最后一个元素才能正确传递给函数processServer.

我究竟做错了什么?

转版本:1.8操作系统:Windows 7 x64

go

0
推荐指数
1
解决办法
8735
查看次数

标签 统计

go ×2

.net-core ×1

asp.net-core ×1

c# ×1

dependency-injection ×1

generics ×1

hugo ×1