CSLA.NET 6.0.0
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//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using Microsoft.AspNetCore.Mvc;
11using Microsoft.AspNetCore.Mvc.RazorPages;
12using Csla.Rules;
13using System.Linq;
14using Csla.Core;
15using System.Threading.Tasks;
16
18{
22 public class PageModel<T> : PageModel
23 where T : ISavable
24 {
25 private ApplicationContext ApplicationContext { get; set; }
26
30 public PageModel(ApplicationContext applicationContext)
31 {
32 ApplicationContext = applicationContext;
33 }
34
38 [BindProperty]
39 public T Item { get; set; }
40
46 public async Task<bool> SaveAsync(bool forceUpdate = false)
47 {
48 try
49 {
50 if (Item is BusinessBase bb && !bb.IsValid)
51 AddBrokenRuleInfo(Item, null);
52 if (ModelState.IsValid)
53 {
54 Item = (T) await Item.SaveAsync(forceUpdate);
55 return true;
56 }
57 }
58 catch (ValidationException ex)
59 {
60 AddBrokenRuleInfo(Item, ex.Message);
61 }
62 catch (DataPortalException ex)
63 {
64 if (ex.BusinessException != null)
65 ModelState.AddModelError(string.Empty, ex.BusinessException.Message);
66 else
67 ModelState.AddModelError(string.Empty, ex.Message);
68 }
69 catch (Exception ex)
70 {
71 ModelState.AddModelError(string.Empty, ex.Message);
72 }
73 return false;
74 }
75
76 private void AddBrokenRuleInfo(T item, string defaultText)
77 {
78 if (item is BusinessBase bb)
79 {
80 var errors = bb.BrokenRulesCollection.
81 Where(r => r.Severity == RuleSeverity.Error);
82 foreach (var rule in errors)
83 {
84 if (string.IsNullOrEmpty(rule.Property))
85 {
86 ModelState.AddModelError(string.Empty, rule.Description);
87 }
88 else
89 {
90 var modelItem = ModelState
91 .Where(r => r.Key == rule.Property || r.Key.EndsWith($".{rule.Property}")).FirstOrDefault();
92 var key = modelItem.Key ?? string.Empty;
93 ModelState.AddModelError(key, rule.Description);
94 }
95 }
96 }
97 else
98 {
99 ModelState.AddModelError(string.Empty, defaultText);
100 }
101 }
102
103 private readonly Dictionary<string, PropertyInfo> _info = new Dictionary<string, PropertyInfo>();
104
112 public PropertyInfo GetPropertyInfo(string propertyName)
113 {
114 if (!_info.TryGetValue(propertyName, out PropertyInfo info))
115 {
116 info = new PropertyInfo(Item, propertyName);
117 _info.Add(propertyName, info);
118 }
119 return info;
120 }
121
128 public bool CanCreateItem()
129 {
131 }
132
139 public bool CanGetItem()
140 {
142 }
143
150 public bool CanEditItem()
151 {
153 }
154
161 public bool CanDeleteItem()
162 {
164 }
165 }
166}
Provides consistent context information between the client and server DataPortal objects.
Exposes metastate for a property.
Custom PageModel for CSLA .NET
Definition: PageModel.cs:24
bool CanDeleteItem()
Gets a value indicating whether the current user is authorized to delete an instance of the business ...
Definition: PageModel.cs:161
bool CanGetItem()
Gets a value indicating whether the current user is authorized to retrieve an instance of the busines...
Definition: PageModel.cs:139
T Item
Gets or sets the business domain model object.
Definition: PageModel.cs:39
bool CanCreateItem()
Gets a value indicating whether the current user is authorized to create an instance of the business ...
Definition: PageModel.cs:128
PropertyInfo GetPropertyInfo(string propertyName)
Get a PropertyInfo object for a property of the Model.
Definition: PageModel.cs:112
async Task< bool > SaveAsync(bool forceUpdate=false)
Save the Item
Definition: PageModel.cs:46
PageModel(ApplicationContext applicationContext)
Creates an instance of the type.
Definition: PageModel.cs:30
bool CanEditItem()
Gets a value indicating whether the current user is authorized to edit/save an instance of the busine...
Definition: PageModel.cs:150
This is the base class from which most business objects will be derived.
Definition: BusinessBase.cs:38
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(ApplicationContext applicationContext, 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