复合PK的通用GetById [英] generic GetById for complex PK

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

问题描述

我正在寻找一种创建通用GetById的方式来获取 params object [] 作为参数,知道找到关键字字段,并且知道找到相关的实体。



在找到解决方案的方式中,我考虑了一种返回PK字段定义的通用方法,以及可以根据字段返回实体的通用方法。 >

我正在寻找一个可以在表中使用一个或多个字段作为主键的东西。



编辑
一个或多个字段作为主键示例=

表客户拥有(CompanyId,CustomerName,Address,CreateDate)。

客户的主键是CompanyId是CustomerName。



我正在寻找通用的GetById,它将知道处理这些表。

解决方案

如果您不知道密钥中有多少成员以及他们有什么类型,则无法获得通用方法。我修改了我的单键解决方案到多个键,但是您可以看到它不是通用的 - 它使用定义了哪些键的顺序:

  /具有任何复杂密钥的实体的基础存储库类
public abstract class RepositoryBase< TEntity>其中TEntity:class
{
private readonly string _entitySetName;
private readonly string [] _keyNames;

protected ObjectContext Context {get;私人集合}
protected ObjectSet< TEntity> ObjectSet {get;私人集合

protected RepositoryBase(ObjectContext context)
{
if(context == null)
{
throw new ArgumentNullException(context);
}

上下文=上下文;
ObjectSet = context.CreateObjectSet< TEntity>();

//获取当前实体类型的实体集
var entitySet = ObjectSet.EntitySet;
//为当前实体类型创建实体集的全名
_entitySetName = context.DefaultContainerName +。 + entitySet.Name;
//获取实体的关键属性的名称
_keyNames = entitySet.ElementType.KeyMembers.Select(k => k.Name).ToArray();
}

public virtual TEntity GetByKey(params object [] keys)
{
if(keys.Length!= _keyNames.Length)
{
throw new ArgumentException(Key member of key members);
}

//按照数组
中的顺序合并键名和值var keyPairs = _keyNames.Zip(keys,(keyName,keyValue)=>
新的KeyValuePair< string,object>(keyName,keyValue));

//构建实体键
var entityKey = new EntityKey(_entitySetName,keyPairs);
//查询第一个当前状态管理器,如果实体未找到查询数据库!
return(TEntity)Context.GetObjectByKey(entityKey);
}

//存储库实现的其他部分
}


I am looking a way to create Generic GetById that get params object[] as parameter, knows to find the key/s field/s and know to find the relevant entity.

In the way to find a solution I thought on a generic method that returns the PK fields definition and a generic method that can return the entity based on fields.

I am looking for something I can use in table with one or more fields as primary key.

EDIT one or more fields as primary key example =
table Customers have (CompanyId, CustomerName, Address, CreateDate).
The primary key of Customers are CompanyId are CustomerName.

I am looking for generic GetById that will know to handle also those such of tables.

解决方案

You can't get "generic" approach if you don't know how many members is in the key and what types do they have. I modified my solution for single key to multiple keys but as you can see it is not generic - it uses order in which keys are defined:

// Base repository class for entity with any complex key
public abstract class RepositoryBase<TEntity> where TEntity : class
{
    private readonly string _entitySetName;
    private readonly string[] _keyNames;

    protected ObjectContext Context { get; private set; }
    protected ObjectSet<TEntity> ObjectSet { get; private set; }

    protected RepositoryBase(ObjectContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException("context");
        }

        Context = context;
        ObjectSet = context.CreateObjectSet<TEntity>();

        // Get entity set for current entity type
        var entitySet = ObjectSet.EntitySet;
        // Build full name of entity set for current entity type
        _entitySetName = context.DefaultContainerName + "." + entitySet.Name;
        // Get name of the entity's key properties
        _keyNames = entitySet.ElementType.KeyMembers.Select(k => k.Name).ToArray();
    }

    public virtual TEntity GetByKey(params object[] keys)
    {
        if (keys.Length != _keyNames.Length)
        {
            throw new ArgumentException("Invalid number of key members");
        }

        // Merge key names and values by its order in array
        var keyPairs = _keyNames.Zip(keys, (keyName, keyValue) => 
            new KeyValuePair<string, object>(keyName, keyValue));

        // Build entity key
        var entityKey = new EntityKey(_entitySetName, keyPairs);
        // Query first current state manager and if entity is not found query database!!!
        return (TEntity)Context.GetObjectByKey(entityKey);
    }

    // Rest of repository implementation
}

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

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