CSLA.NET 5.4.2
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
PageModel.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="CslaPageModel.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Custom PageModel for CSLA .NET</summary>
7//-----------------------------------------------------------------------
8#if !BLAZOR
9using System;
10using System.Collections.Generic;
11using Microsoft.AspNetCore.Mvc;
12using Microsoft.AspNetCore.Mvc.RazorPages;
13using Csla.Rules;
14using System.Linq;
15using Csla.Core;
16using System.Threading.Tasks;
17
19{
23 public class PageModel<T> : PageModel
24 where T : ISavable
25 {
29 [BindProperty]
30 public T Item { get; set; }
31
37 public async Task<bool> SaveAsync(bool forceUpdate = false)
38 {
39 try
40 {
41 if (Item is BusinessBase bb && !bb.IsValid)
42 AddBrokenRuleInfo(Item, null);
43 if (ModelState.IsValid)
44 {
45 Item = (T) await Item.SaveAsync(forceUpdate);
46 return true;
47 }
48 }
49 catch (ValidationException ex)
50 {
51 AddBrokenRuleInfo(Item, ex.Message);
52 }
53 catch (DataPortalException ex)
54 {
55 if (ex.BusinessException != null)
56 ModelState.AddModelError(string.Empty, ex.BusinessException.Message);
57 else
58 ModelState.AddModelError(string.Empty, ex.Message);
59 }
60 catch (Exception ex)
61 {
62 ModelState.AddModelError(string.Empty, ex.Message);
63 }
64 return false;
65 }
66
67 private void AddBrokenRuleInfo(T item, string defaultText)
68 {
69 if (item is BusinessBase bb)
70 {
71 var errors = bb.BrokenRulesCollection.
72 Where(r => r.Severity == RuleSeverity.Error);
73 foreach (var rule in errors)
74 {
75 if (string.IsNullOrEmpty(rule.Property))
76 {
77 ModelState.AddModelError(string.Empty, rule.Description);
78 }
79 else
80 {
81 var modelItem = ModelState.Where(r => r.Key == rule.Property || r.Key.EndsWith($".{rule.Property}")).FirstOrDefault();
82 ModelState.AddModelError(modelItem.Key, rule.Description);
83 }
84 }
85 }
86 else
87 {
88 ModelState.AddModelError(string.Empty, defaultText);
89 }
90 }
91
92 private readonly Dictionary<string, PropertyInfo> _info = new Dictionary<string, PropertyInfo>();
93
101 public PropertyInfo GetPropertyInfo(string propertyName)
102 {
103 if (!_info.TryGetValue(propertyName, out PropertyInfo info))
104 {
105 info = new PropertyInfo(Item, propertyName);
106 _info.Add(propertyName, info);
107 }
108 return info;
109 }
110
117 public static bool CanCreateItem()
118 {
119 return BusinessRules.HasPermission(AuthorizationActions.CreateObject, typeof(T));
120 }
121
128 public static bool CanGetItem()
129 {
130 return BusinessRules.HasPermission(AuthorizationActions.GetObject, typeof(T));
131 }
132
139 public static bool CanEditItem()
140 {
141 return BusinessRules.HasPermission(AuthorizationActions.EditObject, typeof(T));
142 }
143
150 public static bool CanDeleteItem()
151 {
152 return BusinessRules.HasPermission(AuthorizationActions.DeleteObject, typeof(T));
153 }
154 }
155}
156#endif
Custom PageModel for CSLA .NET
Definition: PageModel.cs:25
static bool CanDeleteItem()
Gets a value indicating whether the current user is authorized to delete an instance of the business ...
Definition: PageModel.cs:150
static bool CanEditItem()
Gets a value indicating whether the current user is authorized to edit/save an instance of the busine...
Definition: PageModel.cs:139
T Item
Gets or sets the business domain model object.
Definition: PageModel.cs:30
PropertyInfo GetPropertyInfo(string propertyName)
Get a PropertyInfo object for a property of the Model.
Definition: PageModel.cs:101
static bool CanCreateItem()
Gets a value indicating whether the current user is authorized to create an instance of the business ...
Definition: PageModel.cs:117
async Task< bool > SaveAsync(bool forceUpdate=false)
Save the Item
Definition: PageModel.cs:37
static bool CanGetItem()
Gets a value indicating whether the current user is authorized to retrieve an instance of the busines...
Definition: PageModel.cs:128
This is the base class from which most business objects will be derived.
virtual bool IsValid
Returns true if the object and its child objects are currently valid, false if the object or any of i...
This exception is returned for any errors occurring during the server-side DataPortal invocation.
Tracks the business rules for a business object.
static bool HasPermission(AuthorizationActions action, Type objectType)
Checks per-type authorization rules.
Exception class indicating that there was a validation problem with a business object.
Specifies that the object can save itself.
Definition: ISavableT.cs:19
AuthorizationActions
Authorization actions.
RuleSeverity
Values for validation rule severities.
Definition: RuleSeverity.cs:16