开发者

How would I implement a solution for unique keys?

I have 3 tables:

Player => PlayerId (Primary Key), Name, etc  
Team => TeamId (Primary Key), Name, etc  
PlayerTeam => PlayerId, TeamId (The combination must be unique)  

then I have the following:

public interface IEntityKey<TKey>
{
    TKey Id { get; }    
}

public interface IRepository<TKey, TEntity> where TEntity : class, IEntityKey<TKey>
{
  IQueryable<TEntity> All();
  TEntity FindBy(Expression<Func<TEntity, bool>> expression);
  ....
  TEntity FindBy(TKey id);
}

Now while the above would work fine for tables with a single key such as the player or team table, how would I implement it for my playerteam table? What I need is a solution that would cater for any number of keys. I would prefer a solution such as ...

public interface IEntityKey<TKey> where TKey : System.Tuple
{
    TKey Id { get; }   开发者_运维知识库 
}

over

public interface IEntityKey<TKey1, TKey2>
{
    TKey1 Id1 { get; }    
    TKey2 Id2 { get; }    
}


Just use your original IEntityKey interface, with a Tuple as TKey:

public class PlayerTeam : IEntityKey<Tuple<int, int>>
{
    Tuple<int, int> IEntityKey<Tuple<int, int>>.Id
    {
        get { return Tuple.Create(PlayerId, TeamId); }
    }

    public int PlayerId { get; set; }
    public int TeamId { get; set; }
}

As a side note, it seems to me that PlayerTeam is not an entity but merely an association...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜