I want to be able to instantiate any object in my application using this code:
SmartForm smartForm = SmartForm.Create("id = 23"); Customer customer = Customer.Create("id = 222");
Now I am discussing what Create () should return if this object does not exist.
if Create () returns an empty object , then this is a kind of "zero template", and I can still pass this object around my application and call the call methods on it, which makes programming with this model convenient and easy
if Create () returns null , then I need to check after each instance if the object is zero or not, which makes programming more tedious, but more explicit. The problem is that if you forget to check for zero, your application can run for a long time, not knowing that you are not checking for zero, and then suddenly crash
if Create () throws an exception , it's basically the same as returning null, but makes programming even more tedious by trying, then finally a block for each instance, but you can use different types of exceptions (which you cannot use with a null solution) that could bubble up so you can more clearly handle deep errors in the user interface, so I would think that this is the most reliable solution, although it will produce try / catch code bloat
So, it looks like lightness / reliability . Does anyone have any experience making a decision in accordance with these lines where you have encountered advantages or disadvantages due to this decision?
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestFactory234.Models { public class SmartForm : Item { public string IdCode { get; set; } public string Title { get; set; } public string Description { get; set; } public int LabelWidth { get; set; } public SmartForm() { } private SmartForm(string loadCode) { _loadCode = loadCode; TryToInstantiateFromDatabase(); } public static SmartForm Create(string loadCode) { SmartForm smartForm = new SmartForm(loadCode); if (!smartForm.IsEmpty()) { return smartForm; } else { return null; } } } }
source share