[英]How to cast UserStore<IdentityUser> to its base class IUserStore<IUser>?
I am trying to use the new asp.net identity provider with my abstraction layer of my models domain, wich have a implementation of Entity Framework, so I would like to use the out of box version of identity with entity framework in my data access layer. 我正在尝试将新的asp.net身份提供程序与我的模型域的抽象层一起使用,其中有一个Entity Framework的实现,因此我想在我的数据访问层中使用带有实体框架的现成版本的身份。
How can I convert an 如何转换
UserStore<IdentityUser>
to its base interface 到其基本界面
IUserStore<IUser>
Once UserStore is an implementation of IUserStore, I can get the cast by this: 一旦UserStore是IUserStore的实现,我就可以通过以下方式进行转换:
UserStore<IdentityUser> as IUserStore<IdentityUser>
But I want to avoid the IdentityUser from EntityFramework references and dependences, to keep my domain layer loosely coupled. 但是我想避免从EntityFramework引用和依赖项中获取IdentityUser,以使我的域层保持松散耦合。 So, IdentityUser also are an implementation of IUser I can cast it:
因此,IdentityUser也是IUser的实现,可以将其强制转换为:
IdentityUser as IUser
Both cases works, then I would like to do some like this (wich in fact doesnt works directly): 两种情况都有效,那么我想做些类似的事情(实际上并不直接起作用):
UserStore<IdentityUser> as IUserStore<IUser>
So, how can I achieve this? 那么,我该如何实现呢?
If we look at this diagram http://i3.asp.net/media/4459023/1.png , we will see what I would like to achieve. 如果我们看一下该图 http://i3.asp.net/media/4459023/1.png ,我们将看到我想要实现的目标。 Referencing just the Microsoft.AspNet.Identity.Core in my domain layer, but using Microsoft.AspNet.Identity.EntityFramework implementation at DataAccess layer by returning the core interfaces for the domain layer.
在我的域层中仅引用Microsoft.AspNet.Identity.Core,但通过返回域层的核心接口在DataAccess层使用Microsoft.AspNet.Identity.EntityFramework实现。
The cast doesn't work because UserStore<UserIdentity>
is not the same as UserStore<IUser>
(even if IdentityUser
implements IUser
). UserStore<UserIdentity>
不起作用,因为UserStore<UserIdentity>
与UserStore<IUser>
(即使IdentityUser
实现IUser
)。
If you need UserStore<IUser>
then I suggest you implement it in that way and cast to the concrete type internally, you can ensure that the concrete type is IdentityUser
with a constraint eg 如果您需要
UserStore<IUser>
那么我建议您以这种方式实现它并在内部UserStore<IUser>
转换为具体类型,则可以确保具体类型为IdentityUser
并带有约束,例如
public class UserStore<T> : IUserStore<T> where T: IUser
{
}
You would also need to make your interface covariant ie 您还需要使您的接口协变,即
public interface IUserStore<out T> where T : IUser
{
}
This should allow you to do the following (assuming IdentityUser
supports IUser
) 这应该允许您执行以下操作(假设
IdentityUser
支持IUser
)
IUserStore<IUser> userStore = new UserStore<IdentityUser>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.