通用保存方法 [英] Generic save method

查看:248
本文介绍了通用保存方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的通用存储库中,我需要编写通用的Save方法,它将编辑存在或根据id添加新实体。

  public void Save< T>(T entity)其中T:TEntity,IKeyId 
{
if(ObjectSet.Any(r =>(r as IKeyId).KeyId == entity.KeyId))
{
编辑(实体);
}
else
{
Add(entity);


但是当我尝试<$ c时,Linq会产生异常$ c> Any(r =>(r as IKeyId)... 。
输入类型为'MyProg.DAL.Appeal'的'TypeAs'表达式和不支持'Claimstrak.DAL.Interfaces.IKeyId'类型的检查。只有实体类型和复杂类型在LINQ to Entities查询中受支持。



如何写它是正确的?

解决方案

嗯,事实是你不需要使用ObjectSet,你可以使用DbContext以更简单的方式执行此操作。

Bu,我会告诉您这不是一个好的模式,可以在存储库中调用Save() 。我会建议你在完成所有工作之后才考虑Context的.SaveSession(),这样你就可以做很多事情来往返te数据库。
$ b $因此,你应该做一个像这样的方法,但不要调用SaveChanges(),而不是Save()方法,到一个UpdateOrInsert()和他们,毕竟是你调用.Save()



但我会举例说明您的请求(但我不建议,我推荐您将IUnitOfWork从IRepository中分离出来)



查看代码非常简单:

 接口IKeyId 
{
int Id {get;组; }
}

DbContext context = new YourContext();

public bool保存< TEntity>(TEntity entity)其中TEntity:class,IKeyId
{
return(entity.Id == 0)?添加< TEntity>(实体):编辑< TEntity>(实体);

$ b $ public bool Edit< TEntity>(TEntity entity)其中TEntity:class,IKeyId
{
var set = context.Set< TEntity>();
set.Attach(entity);
返回true;

$ b $ public bool Add< TEntity>(TEntity entity)其中TEntity:class,IKeyId
{
var set = context.Set< TEntity>();
set.Add(entity);
返回true;
}

我在库中使用类似的方法,我改变了T4(。 tt文件),它从我的数据库中生成POCO类,以便明确地实现我有的一些接口,比如IAuditable,IValidatable和其他接口,所以T4自动地实现了类中的接口。


In my generic repository I need to write generic Save method which will edit existed or add new entity depending on id.

public void Save<T>(T entity) where T : TEntity, IKeyId
{
    if (ObjectSet.Any(r => (r as IKeyId).KeyId == entity.KeyId))
    {
        Edit(entity);
    }
    else
    {
        Add(entity);
    }
}

But Linq generate exception when I try do Any( r=> (r as IKeyId).... The 'TypeAs' expression with an input of type 'MyProg.DAL.Appeal' and a check of type 'Claimstrak.DAL.Interfaces.IKeyId' is not supported. Only entity types and complex types are supported in LINQ to Entities queries.

How to write it correct?

解决方案

Well, the truth is that you dont need to use the ObjectSet, you can just use DbContext to do this, in a much more easy way.

Bu, i would tell that this is not a good pattern to use, to call a Save() in a repository. I would recomend that you consider the .SaveSession() of the Context only after all was done, this way you can do a lot of things befose making a round trip to te database.

So, you should make a method like this, but not call the SaveChanges(), instead of a Save() method, to a UpdateOrInsert() and them, after all is done you call the .Save()

But i will give the examplefollowing your request (but i dont recommend, i recommend you separate IUnitOfWork from IRepository)

See how the code is very simple:

    interface IKeyId
    {
        int Id { get; set; }
    }

    DbContext context = new YourContext();

    public bool Save<TEntity>(TEntity entity) where TEntity : class, IKeyId
    {
        return (entity.Id == 0) ? Add<TEntity>(entity) : Edit<TEntity>(entity);
    }

    public bool Edit<TEntity>(TEntity entity) where TEntity : class, IKeyId
    {
        var set = context.Set<TEntity>();
        set.Attach(entity);
        return true;
    }

    public bool Add<TEntity>(TEntity entity) where TEntity : class, IKeyId
    {
        var set = context.Set<TEntity>();
        set.Add(entity);
        return true;
    }

I use a similar approach im my repositories, i have changed the T4 (.tt file) that generates the POCO classes from my database to excplicitly implement some interfaces that i have, such as IAuditable, IValidatable and other, so the T4 automaticaly implement those interfaces in the classes.

这篇关于通用保存方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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