存储库模式应该如何经过更新对象的ID保存到数据库的方法? [英] How should the repository pattern update the ID of the object after a save to database method?

查看:166
本文介绍了存储库模式应该如何经过更新对象的ID保存到数据库的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我在内存中创建我的POCO,我称之为存储库对象的Save方法。我需要再与保存操作过程中创建的数据库ID更新POCO。我应该传递对象使用参考,只需保存方法返回的ID和手动调用页面,或者是更新对象?

下面是一些示例code:

 公共GiftCertificateModel
{
    公众诠释GiftCerticiateId {获取;设置;}
    公共字符串code {获取;设置;}
    公共小数金额{获取;设置;}
    公共DateTime的到期日期{获取;设置;}    公共BOOL的IsValid()
    {}
}
公共GiftCertificateRepository
{    公共GiftCertificateModel GetById(INT GiftCertificateId)
    {
        //调用数据库得到一个并返回单一模式
    }    公开名单< GiftCertificateModel> GetMany()
    {
        //调用数据库得到许多并返回列表
    }    公共字符串GetNewUnique code()
    {
        //随机生成独特code
        返回code;
    }    公共GiftCertificateModel CreateNew()
    {
        GiftCertificateModel GC =新GiftCertificateModel();
        。GC code = GetNewUnique code();
        返回GC;
    }
    //这个应该由裁判或只返回ID或返回一个完整的新模式?
    公共无效保存(GiftCertificateModel GC)
    {
        //调用数据库保存
    }
}GiftCertificateRepository gcRepo =新GiftCertificateRepository();
GiftCertificateModel GC = gcRepo.CreateNew();
gc.Amount = 10.00M;
gc.ExpirationDate = DateTime.Today.AddMonths(12);
gc.Notes =测试气相色谱法;
gcRepo.Save(GC);


解决方案

库应该保存您的POCO所以只需填写 gc.Id 保存通过查询标识方法持久化对象并调用方法后,会看到这一点。

  GiftCertificateRepository gcRepo =新GiftCertificateRepository();
GiftCertificateModel GC = gcRepo.CreateNew();
gc.Amount = 10.00M;
gc.ExpirationDate = DateTime.Today.AddMonths(12);
gc.Notes =测试气相色谱法;
gcRepo.Save(GC);
INT n = gc.Id; //保存填充标识

After I create my POCO in memory, I call the Save method on the repository object. I need to then update the POCO with the database ID created during the save operation. Should I pass the object in using ref, simply have the save method return the ID and manually update the object from the calling page, or what?

Here is some sample code:

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);

解决方案

Repository should save your POCO so simply fill gc.Id in the Save method by querying the Id after persisting the object and calling method will see that.

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

这篇关于存储库模式应该如何经过更新对象的ID保存到数据库的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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