我正在尝试使用mgo(mongodb)创建一个简单的CRUD。这是我的代码:packagemainimport("fmt""time""gopkg.in/mgo.v2""gopkg.in/mgo.v2/bson")typeStatestruct{CustomerIdstringCreatedOntime.TimeCreatedBystringDescriptionstring}typeDataStorestruct{session*mgo.Sessionerrerror}func(ds*DataStore)getCol(collectionNamestring)*mgo.Collect
我正在尝试实现一个通用的GetById(Tid)方法,该方法将满足可能具有不同ID类型的类型。在我的示例中,我有一个实体,其ID类型为int,其中一个ID类型为string。但是,我一直收到错误,我不知道为什么:“int”类型必须是引用类型,以便在方法IEntity的泛型类型中将其用作参数“TId”实体接口(interface):为了迎合我的域模型,它可以具有int或string类型的Id。publicinterfaceIEntitywhereTId:class{TIdId{get;set;}}实体实现:publicclassEntityOne:IEntity{publicintId{
我正在使用Asp.NetMVC构建一个简单的博客应用程序,我想使用Redis数据库。我为我的用户类创建了一个存储库,其中包含一些方法(创建、getbyid和更新)。问题是GetById(longid)方法。usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingServiceStack.Redis;usingDomain.Entities;usingRedisDataLayer.AbstractRepository;usingSystem.Linq;namespaceRedisDataLayer.Concret
我写了下面的方法。publicTGetByID(intid){vardbcontext=DB;vartable=dbcontext.GetTable();returntable.ToList().SingleOrDefault(e=>Convert.ToInt16(e.GetType().GetProperties().First().GetValue(e,null))==id);}基本上它是通用类中的一个方法,其中T是DataContext中的一个类。该方法从T的类型(GetTable)获取表格,并检查输入参数的第一个属性(始终是ID)。问题是我必须先将元素表转换为列表才能对属性执行