Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在保存到数据库方法之后,存储库模式应该如何更新对象的ID?_C#_Asp.net_Repository Pattern - Fatal编程技术网

C# 在保存到数据库方法之后,存储库模式应该如何更新对象的ID?

C# 在保存到数据库方法之后,存储库模式应该如何更新对象的ID?,c#,asp.net,repository-pattern,C#,Asp.net,Repository Pattern,在内存中创建POCO后,我调用repository对象上的Save方法。然后,我需要使用保存操作期间创建的数据库ID更新POCO。我应该使用ref传递对象,让save方法返回ID并从调用页手动更新对象,还是什么 以下是一些示例代码: public GiftCertificateModel { public int GiftCerticiateId {get;set;} public string Code {get;set;} public decimal Amount

在内存中创建POCO后,我调用repository对象上的Save方法。然后,我需要使用保存操作期间创建的数据库ID更新POCO。我应该使用ref传递对象,让save方法返回ID并从调用页手动更新对象,还是什么

以下是一些示例代码:

public GiftCertificateModel
{
    public int GiftCerticiateId {get;set;}
    public string Code {get;set;}
    public decimal Amount {get;set;}
    public DateTime ExpirationDate {get;set;}

    public bool IsValid()
    {}
}




public GiftCertificateRepository
{

    public GiftCertificateModel GetById(int GiftCertificateId)
    {
        //call db to get one and return single model
    }

    public List<GiftCertificateModel> GetMany()
    {
        //call db to get many and return list
    }

    public string GetNewUniqueCode()
    {
        //randomly generates unique code
        return code;
    }

    public GiftCertificateModel CreateNew()
    {
        GiftCertificateModel gc = new GiftCertificateModel();
        gc.Code = GetNewUniqueCode();
        return gc;
    }


    //should this take by ref or just return the id or return a full new model?
    public void Save(GiftCertificateModel gc)
    {
        //call db to save
    }
}

GiftCertificateRepository gcRepo = new GiftCertificateRepository();
GiftCertificateModel gc = gcRepo.CreateNew();
gc.Amount = 10.00M;
gc.ExpirationDate = DateTime.Today.AddMonths(12);
gc.Notes = "Test GC";
gcRepo.Save(gc);
public GiftCertificateModel
{
public int GiftCerticiateId{get;set;}
公共字符串代码{get;set;}
公共十进制数{get;set;}
公共日期时间过期日期{get;set;}
公共bool是有效的()
{}
}
公共礼品证书职位
{
公共GiftCertificateModel GetById(int GiftCertificateId)
{
//调用db获取一个并返回单个模型
}
公共列表GetMany()
{
//调用db获取多个并返回列表
}
公共字符串GetNewUniqueCode()
{
//随机生成唯一代码
返回码;
}
公共礼品证书模型CreateNew()
{
GiftCertificateModel gc=新的GiftCertificateModel();
gc.Code=GetNewUniqueCode();
返回gc;
}
//这是按ref进行的还是只返回id或返回一个全新的模型?
公共作废保存(GiftCertificateModel gc)
{
//调用数据库保存
}
}
GiftCertificatePository gcRepo=新的GiftCertificatePository();
GiftCertificateModel gc=gcRepo.CreateNew();
gc.金额=10.00M;
gc.ExpirationDate=DateTime.Today.AddMonths(12);
gc.Notes=“测试gc”;
gcRepo.Save(gc);

存储库应该保存您的POCO,因此只要在保存对象并调用方法后查询
Id
,在
save
方法中填写
gc.Id

GiftCertificateRepository gcRepo = new GiftCertificateRepository();
GiftCertificateModel gc = gcRepo.CreateNew();
gc.Amount = 10.00M;
gc.ExpirationDate = DateTime.Today.AddMonths(12);
gc.Notes = "Test GC";
gcRepo.Save(gc);
int Id = gc.Id; // Save populate the Id

我就是这么做的。在POCO上有一个可设置的ID会让人觉得“很有趣”,但似乎效果最好。可能需要对其他字段(如CreateDate等)执行相同的操作。ID的可访问性取决于定义POCO的层(程序集)。如果它与存储库在同一层中定义,则可以将ID setter设置为内部,但您可能不想这样做,因为它会破坏ASP.NET中数据绑定的某些场景。是的,您可以对其他自动填充的属性执行相同的操作。@Ladislav Mrnka-感谢您的回复,但是您实际上是如何从回购中获取ID的?创建的原始gc是否应该是在repo中设置的实例?(请参阅原始帖子中的示例代码)很高兴看到我至少走在了正确的轨道上!
GiftCertificateModel
是一个类吗?在这种情况下,传递给
Save
方法的参数是一个引用,对其内容所做的每个更改也将在调用方法中可见。@Ladislav Mrnka-是的,它是一个类,但Save方法位于单独的类giftCertificatePository中,该类处理所有持久性。据我所知,POCO不应该有任何Get/Save方法。