Lecture thumbnail 0:00 / 0:00 Before I went into algorithmic trading.

I used to be an ordinary net developer and the first job that I ever got as a net developer, I was

working on a project where people build something called a property proxy.

And this is something that is very common and lots of people are doing it all over the place.

Now, the reason why we had a property proxy in back where I was working was that we had every measurement

being recorded in a measurement system by being exposed in 2 or 3 measurement systems.

So for example, you could expose a property like temperature in Celsius or Fahrenheit or Kelvin, and

you wanted to have a way of quickly converting it and exposing it through some sort of property value.

Now the property proxy is actually a very simple construct to build, but it does have a few caveats.

So let’s actually build one.

Now, a property proxy is basically the idea of using an object as a property instead of just a literal

value.

So here we’ll have a class called a property of T, So this is going to be the type that’s going to

encapsulate a property of type T and it’s going to keep it as an object.

So I’m going to say property of T, where T has a default constructor.

That’s pretty much all that we need and now we need to actually store the property.

So we’re going to have private T value that stores the value that we want to keep in actual fact.

And we may as well want to have some constructors.

So let’s have an empty constructor and let’s also have a constructor which actually initializes the

value.

So I’m going to go into generate code and make a constructor which initializes the value.

Get rid of all these null checks here and now.

In the case of a default invocation here, you may as well remember it can be a reference type, so

you may as well initialize it with whatever the default is for the type of T.

So here, for example, you can say something like the following.

So we can initialize it by calling this.

And here I would typically do something like the following activate or create instance of T, So that

is a factory method on the activator and this actually makes an instance of T.

Now in addition, you can put a default of T here, but the default of t will give you a null for a

reference type.

So if you do it like this and you have a null value, that’s going to be a null.

That’s not so great for strings, for example.

So for a string you might want to have some value instead of no value, but it doesn’t really matter.

The point is we have a bunch of constructors, but we also want to expose this property somehow.

And the reason for that is that we’ll need to actually manipulate this object from the outside in just

a moment and I’ll explain why.

So we’ll have a public t value property with a capital V this time around.

So let’s suppose that in our model and the reason why we’re building a property proxy is that we want

to prevent duplicate assignments.

So if the value is already ten and you try to assign it to ten, we do nothing.

That’s our domain specific behavior.

That’s the reason why we’re building this proxy.

So we may as well leave the getter in the default state so we can say that the getter just returns a

value.

But the setter is more interesting because we check whether we already have this value, and if we do,

we just return.

So we say, if equals this dot value and value, then we simply return.

Otherwise we may as well write line some sort of informational line about what’s going on.

So we’re assigning value to and then let’s put the value in here and we then perform the actual assignment.

So this dot value equals value.

So this is our publicly exposed property.

Now next up, what we need to do is, or what would be nice is some implicit conversions to and from

T, so we may as well do that.

So we’ll have public static implicit operator t where you convert to let’s say an int from a property

of int, something like that.

And here it’s very easy.

Actually I should fix the spelling here.

Do I get to rename it?

Yeah.

Property.

There we go.

Okay.

So in this case we return property dot value.

So this handles the case where you somebody writes int and equals and they use property of int for example.

And the reverse case is public static implicit operator property of T, which takes a value.

And in this case we return you property of T with the value, obviously.

And this takes care of the case.

Like if somebody writes property of int p equals 123, for example.

That’s what the implicit conversion in this case would handle.

In addition, I would typically go into generate code and generate the equality members.

So I would just pick a value of T and you may as well just add everything in the kitchen sink here.

So let’s just press finish and we get lots of generated code.

Now there are caveats here, like for example, the fact that we’re using like if you look at the hash

code, we’re using a non read only value.

So as the object value changes the hash code.

Will change, but that’s okay because we actually in the implementation, what we might want to do is

we might want to actually return value, get hashCode like this.

So this is a more correct implementation.

But then of course, you have this concerns about, well, what if it’s null and all the rest of it?

So it can be quite tedious.

And once again, value is not read only.

So if it’s an primitive type and not a reference type, then you’re asking for trouble because the hashCode

will be changing.

But let’s not digress here.

So we have everything that we need in our property of T and we can now start using it.

So for example, if we were defining a creature in a computer game, you would define it like this,

you would say creature, and then you would say public property of int, agility, get and set, for

example.

Now you might think that this is correct and that everything is fine.

Unfortunately, if you go with this approach, the code that we’ve written for the setter here will

actually not be executed.

Let me explain why.

Let’s suppose that you write something like the following var C equals new creature, and then you assign

the agility.

You say C dot agility equals ten, for example.

Now you’re probably thinking, Well, okay, if I’m doing this, then what’s really happening is somebody

says C dot set underscore agility with the value of ten.

Unfortunately, no, this isn’t what’s going to happen because remember in C sharp, unlike C plus plus

and many other places, you cannot overload the assignment operator.

So what happens with assignment is, as you may have guessed, we’re using an implicit conversion from

T And remember, an implicit conversion makes a new object instead of changing the existing one.

So you’re replacing this with a brand new property.

And essentially what you’re doing is you’re saying something like the following.

You’re saying C dot agility equals new property of int with the value of ten because that’s how we implemented

it.

So the question is, well, what if we want the mutating behavior instead?

How do we do this?

And unfortunately in C sharp the only way to actually get this behavior is to rewrite this as a combination

of field and property.

So you would have your property of int called agility with all the behaviors that you need from a property

event.

And then of course, you would make this private and for the public part, instead of having a property

of int, you just have an int, you have public int agility.

And then for the getter you return agility dot value.

And for the setter obviously you say agility dot value equals value and this is the way that you would

go about implementing this.

So first of all, let’s actually step into the code.

I’m going to stick a breakpoint right in here and let’s debug this and just see the fact that it does

actually work and it does land us in the correct place.

Of course, what we have to do is we also have to initialize the property because otherwise it’s now

by default.

So let’s make a new property of INT and let’s try this again and see how it goes second time around.

So as you can see, we’re here in the setter.

We are comparing the current value, which is zero with the value being assigned, which is ten.

They’re not equal.

So we do the output and then we return.

So that is fine.

And now what we can do is we can call this twice.

We can actually call the assignment to agility twice with the same value and see if we actually get

the same result.

So I’m going to say C dot agility equals ten once again, and we’re going to execute this and just look

at the output.

So as you can see, even though we assign the value twice, we are only actually assigning it once because

our implicit implementation of a property proxy that we’ve built checks for identical assignments and

prevents them by returning from here.

So this is hopefully a good illustration of how to build a property proxy as well as some of the limitations

that you get.

Because typically the this part wouldn’t have to be required in a language like C plus plus because

you could simply overload the assignment operator and specify what happens if you assign an integer

to a property of INT, or if you assign a T to a property of T, But unfortunately in C sharp, you

end up doing these kinds of things which are unpleasant to write, I must admit, but there is really

no way around it.

So hopefully you appreciate this example of properties being expressed as integer classes instead of

just individual values.

Play Play Stop Play Play Play Play Play Play Start Play Play