Is possible to use generics in LINQ-to-SQL mapping?
Is there a way to define the following structure in a DataContext/DBML file?
public class Entity
{
public int Id { get; set; }
public string Name { get; set; }
public EntitySet<IPermission> Permissions { get; set; }
}
public class User : IPermissionHolder
{
public int Id { get; set; }
public string Name { get; set; }
public EntitySet<Permission<User>> Permissions { get; set; }
}
public class Group : IPermissionHolder
{
public int Id { get; set; }
public string Name { get; set; }
public EntitySet<Permission<Group>> Permissions { get; set; }
}
public interface IPermissionHolder
{
int Id { get; set; }
}
public interface IPermission
{
Entity Entity { get; set; }
IPermissionHolder Holder { get; }
}
public class Permission<T> : IPermission where T : class, IPermissionHolder
{
public IPermissionHolder Holder
{
get { return PermissionHolder; }
}
public T PermissionHolder { get; set; }
public Entity Entity { get; set; }
}
If it's not possible, can you seggest another structure that fits my need?
Right now my DB is using two different tables for the GroupPermissions and the UserPermissions.
I don't like to have a common table where i have to add a "type" column... with two different table i have a much more strict control on the DB side.Thanks for any help
P.S.: i'm still with the Framework 3.5, otherwise i could remove the IPermissionHolder interface and use co-variance
P.S.S.: asked also here, but no answer :( http://social.msdn.microsoft.com/Forums/en/linqtosql/thread/04a03c68-79c0-4136-907c-f81440e78c45
EDIT: i'm trying different things and i'm facing two main problems 1) I want to have a IEnumerable, but it will never works because i don't want only to get data, but also to push data and an object can not be covariant and contravariant at the same time. So first of all i should choose: read or write. 2)Here the most difficult issue: how do i map TWO Association to a single property?
User:[global::System.Data.Linq.Mapping.AssociationAttribute(Name = "User_Permission", Storage = "permissions", ThisKey = "Id", OtherKey = "UserId")]
public EntitySet<Permission<User>> Permissions{ ... }
Group
[global::System.Data.Linq.Mapping.AssociationAttribute(Name = "Group_Permission", Storage = "permissions", ThisKey = "Id", OtherKey = "GroupId")]
public EntitySet<Permission<Group>> Permissions { ... }
Permission
[global::Sys开发者_如何学编程tem.Data.Linq.Mapping.AssociationAttribute(Name = "???", Storage = "holder", ThisKey = "HolderId", OtherKey = "Id", IsForeignKey = true)]
public T PermissionHolder { ... }
Maybe i should call the Asscoiation "Holder_Permission"?!?
I tried with many different approach. I can say that with LINQ-TO-SQL is not possible to have generic mapping.
I will try with the Linq-To-Entity.
精彩评论