Is it a good practice to make multiple separate database calls from an ASP.NET MVC web application?

I have a controller attribute called [RequiresCompletedProfile], which I can apply to action methods to prevent the user from going there if they have not completed their profile.

This worked fine when I had one kind of user, but since then the application has turned into two kinds of users: sellers and customers.

So, there is no more table "User_Profile". Now there are "Client_Profile" and "Vendor_Profile", and their schemes are different. I should note that I use LINQ, but I rewrite all LINQ objects in POCO before I render them.

My solution was to create an interface called "User_Type", which had the following method:

bool IsProfileCompleted();

Now, objects of my Client and Vendor objects can implement the interface and be responsible for determining whether their fields / members will fill out their profile.

However, now that I have several types of users, I can’t be sure which table to get from the profile, so I should do something like this:

public class RequiresCompleteProfileAttribute : ActionFilterAttribute
{
    IUserRepository userRepo = new SqlUserRepository();
    IClientProfileRepository clientProfileRepo = new SqlClientProfileRepo();
    IVendorProfileRepository vendorProfileRepo = new SqlVendorProfileRepo();

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.HttpContext.User.Identity.IsAuthenticated)
        {

            // Database call #1
            var user = userRepo.GetUserByUsername(User.Identity.Name);

            UserType profile;

            if (user.UserTypeName == "Client")
            {
            // Database call #2
            profile = clientProfileRepo.GetClientByUserName(filterContext.HttpContext.User.Identity.Name);

            }
            else
            {
            // Database call #2
            profile = vendorProfileRepo.GetClientByUserName(filterContext.HttpContext.User.Identity.Name);
             }

            if (!profile.IsProfileCompleted())
                filterContext.HttpContext.Response.Redirect("/admin/editprofile/");


        }

        base.OnActionExecuting(filterContext);
    }

}

Here you can see that I need to make 2 calls to the database, one to determine the type of user, and the other to get the profile from the corresponding table.

Is this a bad practice? If so, what should I do instead?

+3
source share
1 answer

, - , .

+4

Source: https://habr.com/ru/post/1714255/


All Articles