使用只读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) 我是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并遇到基本函数调用问题:
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