简体   繁体   English

在C#中动态设置查询类型

[英]Set query type dynamically in C#

I need your help. 我需要你的帮助。 I wanna implement a database request with different types depending on the users role. 我想根据用户角色实现具有不同类型的数据库请求。

My current query looks like this: 我当前的查询如下所示:

var myVar = session
            .Query<TypeA>()
            .Where(xyz)
            .ToList();

Now I wanna do it like this: 现在我想这样做:

if(UserIsAdmin)
     Type T = TypeA;
else
     Type T = TypeB;

var myVar= session
           .Query<T>()
           .Where(xyz)
           .ToList();

Notice TypeA an TypeB should be dynamically changed in .Query<T> . 注意TypeATypeB应该在.Query<T>动态更改。

Is there any good way to do this? 有什么好办法吗? Do you need more information? 你需要更多的信息?

Thanks in advance :-) 提前致谢 :-)

EDIT: 编辑:

The scenario looks as follows: 该方案如下所示:

I have to get different models from the database depending on the users roles. 我必须根据用户角色从数据库获取不同的模型。 Lets say the user is Admin he can see firstname, lastname, address , if not he can only see firstname, lastname . 假设用户是Admin,他可以看到firstname, lastname, address ,如果不是,则只能看到firstname, lastname

I thought to create two different models with these attributes and change the type in the query dynamically. 我想用这些属性创建两个不同的模型,并动态更改查询中的类型。

You need to understand first the consept of seperating layers. 您需要首先了解分离层的概念。

So first of all you will have a DB layer , which in your case(though i don't know you db schema), but it should look something like: 因此,首先,您将拥有一个DB layer ,就您而言(尽管我不知道您的数据库模式),但它看起来应该像这样:

public class UsersDbServices 
{
    public UserDbEntity GetUserById(int userId)
    {
        UserDbEntity user = null
        using (context..)
        {
            user = context.Users.Where(u=> u.Id = userId).FirstOfDefault();
        }

        return user;
    }
}

You will call this service from you Logic layer, note that you can use the same method for both normal user and administrator ! 您将从Logic层调用此服务,请注意, normal用户和administrator都可以使用相同的方法!

Now for the clinet side I would return a DTO(data transfer object) which will be the same for both cases!: 现在,对于clinet端,我将返回DTO(数据传输对象),这两种情况都相同!:

public class UserDTO
{
    public int UserId { get;set;}
    public string FirstName { get;set;}
    public string LastName { get;set;}
    public string Address { get;set;}       
}

Now the last part is the controller, here you will get the data from the DBLayer (you can go through another layer of logic, but lets say its not a must), and what you will do now in the controller you can poppulate the DTO (and again you can do this all in the logic layer as well) in this way: 现在最后一部分是控制器,在这里您将从DBLayer获取数据(可以通过另一层逻辑,但是可以说不是必须的),现在您将在控制器中执行的操作可以填充DTO (同样,您也可以在逻辑层中全部完成此操作):

public class UsersController
{
    UsersDbServices m_UsersDbServices = new UsersDbServices ();

    [HttpGet]
    public UserDTO GetUserById(userId)
    {
        UserDTO retVal = null;

        UserDbEntity userDbEntity  = m_UsersDbServices.GetUserById(userId);
        if(userDbEntity == null){
            throw...
        }

        retVal = new UserDTO()
        {
            FirstName  = userDbEntity.FirstName;
            ...
        };

        if(!UserIsAdmin) <- kept it as a bool here
        {   
            retVal.Address  = null;
        }

        return retVal; <- convert to json
    }
}

Another approach is to use another DTO like AdminUserDTO and RegularUserDTO 另一种方法是使用另一个DTO,例如AdminUserDTORegularUserDTO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM