使用一个接口为mvc 5中的两个数据库模型创建通用代码

本文关键字:两个 数据库模型 代码 创建 mvc 一个 接口 | 更新日期: 2023-09-27 18:20:24

我正试图添加一个接口,为两个数据库存储库模型生成通用代码,但在每个数据库模型中都返回不同的类型,我一直无法找到解决方案。这似乎应该是可能的,但我不知道有谁能给我指明正确的方向,或者告诉我这是否不可能。

代码-接口:

interface IPermissions
{
    //List<User> getUser(string userID);
    List<Role> getUserPermissions(string userName);
    List<Role> getAllPermissions();
    void enable();
    void disable();
    void addPermission(string permissionName);
    void removePermission(string permissionName);
}

型号1:

public List<AUser> getUser(string userName)
{
    IEnumerable<AUser> users = from x in a.AUsers
                                    where x.UserID == userName
                                    select x;
    List<AUser> usersList = users.ToList();
    return usersList;
}
public List<AGroup> getUserPermissions(string userName)
{
    IEnumerable<AGroup> usersPermisions = from abfug in a.UserGroups
                                              join abfg in a.Group on abfug.GroupID equals abfg.ID
                                              join au in a.AUsers on abfug.UserID equals au.ID
                                              where au.UserID == userName
                                              select abfg;
    List<Group> usersList = usersPermisions.ToList();
    return usersList;
}

型号2:

public List<UCUser> getUCUser(string userName)
{
    IEnumerable<UCsUser> users = from y in UC.UCUsers
                                     where y.UserID == userName
                                     select y;
    List<UCyUser> usersList = users.ToList();
    return usersList;
}
public List<UCGroup> getUCUserPermissions(string userName)
{
    IEnumerable<UCGroup> userPermissions = from bfug in UC.UCUserGroups
                                               join bfg in UC.UCGroups on bfug.GroupID equals bfg.ID
                                               join u in UC.UCUsers on bfug.UserID equals u.ID
                                               where u.UserID == userName
                                               select bfg;
    List<UCGroup> usersList = userPermissions.ToList();
    return usersList;
}

如果代码中有任何拼写错误,请耐心等待。

如有任何帮助,我们将不胜感激。

使用一个接口为mvc 5中的两个数据库模型创建通用代码

当您使用通用模式时,您可以使接口通用:

public interface IRepository<TModel>
{
    IEnumerable<TModel> Retrieve();
    void Create(TModel model);
    void Delete(TModel model);
    void Update(TModel model);
}
public class RoleRepository : IRepository<Role>
{
    IEnumerable<TModel> Retrieve()
    {
        using (var context = new MyContext())
        {
            return context.Roles.ToList(); // iterating through them hits the database
        }
    }
    public void Create(TModel model)
    {
        using (var context = new MyContext())
        {
            context.Set<TModel>().Attach(model);
            context.SaveChanges();
        }
    }
    public void Delete(TModel model) {}
    public void Update(TModel model) {}
}
public class MyController : Controller
{
    private readonly IRepository<Role> roleRepository;
    public MyController(IRepository<Role> roleRepository)
    {
        this.roleRepository = roleRepository;
    }
}

一旦你有了这个,你通常会把它藏在服务后面。然后你就有了适当的顾虑。存储库层隐藏上下文,因此可以轻松更改。服务层隐藏存储库,以便轻松更改,还可以将这些特定于操作的功能添加到服务中。

public class MyService : IMyService
{
    private readonly IRepository<Role> roleRepository;
    public MyService(IRepository<Role> roleRepository)
    {
        this.roleRepository = roleRepository;
    }
    public IEnumerable<Role> LetsDoSomeCoolThings()
    {
        // some complicated role specific function to return a complex set of results
    }
}

现在很明显,您可以用控制器中的Repository来代替此服务