Fully Initialized Class

I use the initiazling property in the class and I want to run the validation method after it is fully initialized. I cannot use the constructor for obvious reasons. is there any way to do this in some kind of initialized class event?

var t = new Foo { foo = ""; } class Foo { public string foo {get; set;} ... public bool validate {get ; set;} private void validation() { if(foo == "") validate = false; if ... } } 
+4
source share
5 answers

You can add validation logic to properties. Check if the class is initialized after the assigned property and static static event, if initialization is complete. You can get a link to an instance by sending the event sender to Foo.

 public string Foo { get { return _foo; } set { _foo = value; if (IsInitialized) OnClassInitialized(); } } public static event EventHandler ClassInitialized; private OnClassInitialized() { if (ClassInitialized != null) ClassInitialized(this, EventArgs.Empty); } 

Using:

 Foo.ClassInitialized += (sender, e) => { Foo foo = sender as Foo; ... }; 
0
source

(Note: for clarity, I renamed the property to Bar to easily distinguish it from the Foo type)

If the Bar property must be valid when building, why don't you require it in the constructor? Why do you allow the creation of invalid objects?

 class Foo { public Foo(string bar) { if(!IsValidBar(bar)) throw new ArgumentException("bar is not valid.", "bar"); this.Bar = bar; } public string Bar {get; set;} private bool IsValidBar(string bar) { // blah blah } } 

Alternatively, if you can build an instance of Foo without the value of the Bar property, but you do not want to allow Bar to be set to an invalid value, you can check this in the installer:

 class Foo { private string bar; public string Bar { get { return bar; } set { if(!IsValidBar(value)) throw new ArgumentException("bar is not valid.", "value"); bar = value; } } private bool IsValidBar(string bar) { // blah blah } } 
+2
source

One approach is an interface designed for verification purposes; IValidation for example. IValidation may contain a Validate method. Classes that should provide behavior can now do this in a controlled manner.

This prevents bloating inside the constructor, for which IMHO has a poor design.

0
source

You can avoid the use of privilege initializers and simply move all this code to the constructor using optional parameters if there are many properties. This way you get the property initializer constructor and still be able to check the class after initialization is complete. Something like that:

 class Foo { public string Foo {get; set;} public string Bar {get; set;} public bool IsValid {get ; set;} private void Validation() { if(foo == "") IsValid = false; if ... } public void Foo(string foo = string.Empty, string bar = string.Empty) { Foo = foo; Bar = bar; Validation(); } } ..... var t = new Foo (Foo = "SomeString"); 

The downside is that it is a relatively new C # 4 syntax.

If you cannot use C # 4, you can use property accessors to enable validation, for example. as:

 public string Foo { get { return foo; } set { foo = value; Validation(); } } 

but this will evaluate the validity for each set and can be slow if you set a lot of properties right away. You can also use the get accessory in combination with some lazy loading, something like this:

 public bool IsValid { get { if (!isValidated) Validation(); return isValid; } private set { isValid = value; } } public string Foo { get { return foo; } set { foo = value; isValidated := false; } } 
0
source

You can use Aspect Oriented Programming like postsharp. http://www.postsharp.org/ . But you are losing productivity.

0
source

Source: https://habr.com/ru/post/1339225/


All Articles