简体   繁体   English

如何投射UserStore <IdentityUser> 到其基类IUserStore <IUser> ?

[英]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.

相关问题 如何使用用户管理器<identityuser>与用户存储<identityuser> ?</identityuser></identityuser> - How to use UserManager<IdentityUser> with UserStore<IdentityUser>? IIdentity,IPrincipal,OWIN,IdentityUser和IUser如何 <string> 适合在一起? - How do IIdentity, IPrincipal, OWIN, IdentityUser and IUser<string> fit together? 如何判断 IdentityUser 类的主键“Id”是 IDENTITY(1,1)? - How to tell the primary key 'Id' of IdentityUser class is IDENTITY(1,1)? ASP.NET Core Identity: base class (IdentityUser, IdentityRole, IdentityUserRole etc.) 实现 - ASP.NET Core Identity: the base class (IdentityUser, IdentityRole, IdentityUserRole e.t.c.) Implementation 将基类转换为 T 的泛型类 - Cast base class to generic class of T 在实现自己的IUserStore时,类上的“可选”接口是否实际上是可选的? - When implementing your own IUserStore, are the “optional” interfaces on the class actually optional? 如何通过用户名获取 IdentityUser - How to get IdentityUser by Username 将控件中的对象强制转换为其父控件中的类 - Cast an object in a control to a class in its parent control 动态将派生类类型转换为基类C# - Cast derived class type dynamically to base class type C# 如何扩展Microsoft.AspNet.Identity.IUser <TKey> - How to extend Microsoft.AspNet.Identity.IUser<TKey>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM