Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
220 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...