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.
Controller.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="Controller.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Provides methods that respond to HTTP requests</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12#if NETSTANDARD2_0 || NET5_0 || NETCORE3_1
13using System.Threading.Tasks;
14using Csla.Core;
15using Csla.Rules;
16using Microsoft.AspNetCore.Mvc;
17#else
18using System.Web.Mvc;
19#endif
20
21namespace Csla.Web.Mvc
22{
27#if NETSTANDARD2_0 || NET5_0 || NETCORE3_1
28 public class Controller : Microsoft.AspNetCore.Mvc.Controller
29#else
30 public class Controller : System.Web.Mvc.Controller
31#endif
32 {
33#if NETSTANDARD2_0 || NET5_0 || NETCORE3_1
43 protected async Task<bool> SaveObjectAsync<T>(T item, bool forceUpdate)
44 where T : class, Core.ISavable
45 {
46 return await SaveObjectAsync(item, null, forceUpdate);
47 }
48
59 protected virtual async Task<bool> SaveObjectAsync<T>(T item, Action<T> updateModel, bool forceUpdate)
60 where T : class, Core.ISavable
61 {
62 try
63 {
64 ViewData.Model = item;
65 updateModel?.Invoke(item);
66 if (item is BusinessBase bb && !bb.IsValid)
67 {
68 AddBrokenRuleInfo(item, null);
69 return false;
70 }
71 ViewData.Model = await item.SaveAsync(forceUpdate);
72 return true;
73 }
74 catch (ValidationException ex)
75 {
76 AddBrokenRuleInfo(item, ex.Message);
77 return false;
78 }
79 catch (DataPortalException ex)
80 {
81 if (ex.BusinessException != null)
82 ModelState.AddModelError(string.Empty, ex.BusinessException.Message);
83 else
84 ModelState.AddModelError(string.Empty, ex.Message);
85 return false;
86 }
87 catch (Exception ex)
88 {
89 ModelState.AddModelError(string.Empty, ex.Message);
90 return false;
91 }
92 }
93
94 private void AddBrokenRuleInfo<T>(T item, string defaultText) where T : class, ISavable
95 {
96 if (item is BusinessBase bb)
97 {
98 var errors = bb.BrokenRulesCollection.
99 Where(r => r.Severity == RuleSeverity.Error);
100 foreach (var rule in errors)
101 {
102 if (string.IsNullOrEmpty(rule.Property))
103 ModelState.AddModelError(string.Empty, rule.Description);
104 else
105 ModelState.AddModelError(rule.Property, rule.Description);
106 }
107 }
108 else
109 {
110 ModelState.AddModelError(string.Empty, defaultText);
111 }
112 }
113#else
123 protected bool SaveObject<T>(T item, bool forceUpdate)
124 where T : class, Core.ISavable
125 {
126 return SaveObject(item,
127 null,
128 forceUpdate);
129 }
130
141 protected virtual bool SaveObject<T>(T item, Action<T> updateModel, bool forceUpdate)
142 where T : class, Core.ISavable
143 {
144 try
145 {
146 ViewData.Model = item;
147 updateModel?.Invoke(item);
148#if NETSTANDARD1_6
149 ViewData.Model = item.SaveAsync(forceUpdate).Result;
150#else
151 ViewData.Model = item.Save(forceUpdate);
152#endif
153 return true;
154 }
155 catch (DataPortalException ex)
156 {
157 if (ex.BusinessException != null)
158 ModelState.AddModelError(string.Empty, ex.BusinessException.Message);
159 else
160 ModelState.AddModelError(string.Empty, ex.Message);
161 return false;
162 }
163 catch (Exception ex)
164 {
165 ModelState.AddModelError(string.Empty, ex.Message);
166 return false;
167 }
168 }
169#endif
170
192 protected void LoadProperty<P>(object obj, PropertyInfo<P> propertyInfo, P newValue)
193 {
194 new ObjectManager().LoadProperty(obj, propertyInfo, newValue);
195 }
196
197 private class ObjectManager : Server.ObjectFactory
198 {
199 public new void LoadProperty<P>(object obj, PropertyInfo<P> propertyInfo, P newValue)
200 {
201 base.LoadProperty(obj, propertyInfo, newValue);
202 }
203 }
204 }
205}
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.
Exception BusinessException
Gets the original server-side exception.
Maintains metadata about a property.
Provides methods that respond to HTTP requests in an ASP.NET MVC web site.
Definition: Controller.cs:32
bool SaveObject< T >(T item, bool forceUpdate)
Performs a Save() operation on an editable business object, with appropriate validation and exception...
Definition: Controller.cs:123
void LoadProperty< P >(object obj, PropertyInfo< P > propertyInfo, P newValue)
Loads a property's managed field with the supplied value calling PropertyHasChanged if the value does...
Definition: Controller.cs:192
RuleSeverity
Values for validation rule severities.
Definition: RuleSeverity.cs:16