Base Model

General information

"ModelBase.cs" is the class that all Models inherit from that is found under the "Utilities" directory of the Business layer of the application. It enables connection to the database by providing the basic methods that serve as tools that can be used to implement logic, so the user does not need to directly interact with the database.

Methods Overview

In the following, these methods and their functionality will be explained:

UnitWork

"UnitWork" is the container of all data repositories over which communication with the database is done.

public IUnitWork UnitWork { get; private set; }

HttpWebContext

The method "HttpWebContext" stores all the information regarding the user's request, such as query strings, attached files, etc.

public HttpContext HttpWebContext
{
    get => _httpContext ?? (_httpContext = HttpContext.Current);
}

ThrowError

The method "ThrowError" is used when you want to catch an error and inform the user of the details of the error. It displays a pop-up window containing the error message you inserted as parameter.

protected void ThrowError(string message)
{
    throw new HttpException(MiddleContext.CustomError, message);
}

Example 1:

The following code illustrates the function call:

if (model.StartTime > model.EndTime)
    ThrowError("StartTime must be less or equal EndTime.");

The "EndTime" of the Model is not supposed to be earlier than the "StartTime", so if the user accidentally inputs the times falsely, a pop-up with the error message "StartTime must be less or equal EndTime." gets displayed to the user.

Example 2:

The following code illustrates the function call using an embedded variable:

if (!UnitWork.ProductionOrders.Exists(x => x.ID == id))
    ThrowError($"Order {id} does not exist!");

Before a Production Order is fetched using an ID, it is first checked if a Production Order exists that has that ID. If not, a pop-up with the error message "$"Order {id} does not exist!" gets displayed to the user where "{id}" is the inputted id that should be checked.

When using one or more embedded variables in an error message, a "$" should precede the string that is inserted as input parameter.

ActiveUser

"ActiveUser" returns the Name of the current user.

It is a shorter alternative to "Thread.CurrentPrincipal.Identity.Name".

public string ActiveUser
{
    get
    {
        if (string.IsNullOrWhiteSpace(_userName))
        {
            if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                //get user name
                _userName = Thread.CurrentPrincipal.Identity.Name;
            }
        }

        return _userName;
    }
}

Example 1:

The following code illustrates the function call:

string userName = ActiveUser;

It sets the Property "userName" of the Model to "ActiveUser"

Location

"Location" returns the Plant the User is currently located at.

public string Location
{
    get
    {
        if (string.IsNullOrWhiteSpace(_location))
        {
            _location = UnitWork.UserProfiles.GetByID(ActiveUser).Location;
        }

        return _location;
    }
}

AccessScope

"AccessScope" returns a List of the data (Plants) the current user has access to.

public List<string> AccessScope
{
    get
    {
        if (_accessScope == null || _accessScope?.Count == 0)
            _accessScope = MiddleContext.Scopes;
		return _accessScope;
    }
}

Example 1:

The following code illustrates the function call:

foreach (var plant in AccessScope)
    PlantList.Add(new SelectListItem() { Text = plant, Value = plant });

A foreach loop iterates through the items (Plants) in AccessScope and for each one, a SelectListIitem is added to PlantList.

Example 2:

The following code illustrates the function call of "Contains" from AccessScope:

if (!AccessScope.Contains(order.Location))
{
    ThrowError("You don't have access to this data.");
}

It checks if the Location of a specific order is included in the AccessScope of the user. If it is not, an error is thrown that informs the user that they do not have access to data of this Plant.

Example 3:

The following code illustrates the function call of "Contains" from AccessScope within the filter for a Fetch from a data repository:

OrderList = UnitWork.ProductionOrders.Get(x => AccessScope.Contains(x.Location)).ToList();

All Production Orders get fetched whose Location is included in the AccessScope of the user and stored into the "OrderList".

DefaultScope

"DefaultScope" is the default Plant whose data the user has access to.

If the User has access to the data of the Location he is located in, then the DefaultScope is the same as their Location. However, if the User does not have access to the data of their own Location, then the first item in their AccessScope should be used as Default.

public string DefaultScope
{
    get
    {
        if (string.IsNullOrWhiteSpace(_defaultScope))
        {
            _defaultScope = AccessScope.Contains(Location) ? Location : AccessScope.First();
        }

        return _defaultScope;
    }
}

Language

"Language" is the language of the system the application is currently running on. For example, the default language is English (United States), which is stored as "en-US".

public string Language
{
    get
    {
        if (string.IsNullOrWhiteSpace(_lang))
        {
            //get culture code
            _lang = Thread.CurrentThread.CurrentCulture.Name;
        }

        return _lang;
    }
}

Last updated