I recommend reading the Expert 2008 Business Objects book, as this is discussed in many places through the book.
The short answer is that the static PropertyInfo<T> field maintains metadata about your property, and acts as a strongly-typed token you can use instead of using string literal values all over the place.
Without the PropertyInfo<T> field your property would look like this:
public string City
{
get { return GetProperty<string>("City"); }
set { SetProperty<string>("City", value); }
}
You'd have to supply the property type because it couldn't be inferred, and you'd have to supply the property name as a hard-to-maintain string literal. Very bad.
Lambdas can be hard to read to start - no doubt there!
the (c => c.Id) is a simple lambda expression where 'c' represents an instance of the current business object type. So that expression is returning the Id property of the current business object type.
The RegisterProperty() method uses some reflection behind the scenes to take that property and get a reflection object that describes the property, which allows us to pull out the property's name and data type.
The real value here is that the 'c.Id' part is a strongly-typed, compiler-checked construct. And even better, if you do a symbolic rename (rename refactor) on the property, the rename will fix this code too, where it wouldn't fix a string literal.
So while the syntax looks a bit odd the first time you see it, the value in terms of maintainabilty is really high, and so it is (imo) worth using.
svc.GetDataCompleted += (o, e) =>
{
if (e.Error == null)
{
using (BypassPropertyChecks)
{
Id = e.Result.Id;
Name = e.Result.Name;
City = e.Result.City;
}
}
handler(this, e.Error);
};
Also,
How does to know to call the "GetData" method on the service?
svc.GetDataAsync(criteria.Value);
It
is typed in framework based on generic type parameter for BB / you class.
Sergey Barskiy
Principal Consultant
office: 678.405.0687 |
mobile: 404.388.1899
Microsoft Worldwide Partner of the Year | Custom
Development Solutions, Technical Innovation
From: Mark Richardson
[mailto:cslanet@lhotka.net]
Sent: Thursday, July 02, 2009 2:47 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] Can someone explain this Syntax?
Rocky,
Thanks for the response.
But how does "(c => c.Id)" know that "c" represents an
instance of the current business object type?
It looks like you just magically declared "c" and something
automatically knew it's type.
What am I missing here?
Look
in CSLA business base’s register property – it will make more
sense.
Sergey Barskiy
Principal Consultant
office: 678.405.0687 |
mobile: 404.388.1899
Microsoft Worldwide Partner of the Year | Custom
Development Solutions, Technical Innovation
From: Mark Richardson
[mailto:cslanet@lhotka.net]
Sent: Thursday, July 02, 2009 5:08 PM
To: Sergey Barskiy
Subject: Re: [CSLA .NET] RE: Can someone explain this Syntax?
Not following you here?
RockfordLhotka:
Lambdas can be hard to read to start - no doubt there!
the (c => c.Id) is a simple lambda expression where 'c' represents an instance of the current business object type. So that expression is returning the Id property of the current business object type.
The RegisterProperty() method uses some reflection behind the scenes to take that property and get a reflection object that describes the property, which allows us to pull out the property's name and data type.
The real value here is that the 'c.Id' part is a strongly-typed, compiler-checked construct. And even better, if you do a symbolic rename (rename refactor) on the property, the rename will fix this code too, where it wouldn't fix a string literal.
So while the syntax looks a bit odd the first time you see it, the value in terms of maintainabilty is really high, and so it is (imo) worth using.
The identifier you use (‘c’ in my case) is
arbitrary. It is like the identifier you use for a type parameter in a generic –
you can use whatever you’d like.
The type of the identifier is determined by the declaration of
the parameter as defined by the RegisterProperty() method. As Sergey suggested,
you can look in BusinessBase<T> to see how RegisterProperty() is defined,
and what you’ll discover is that this parameter matches a specific method
signature, where the type is defined as being the type of the business class.
This allows the compiler to infer the type of your identifier (‘c’)
as being the type of the business class.
Rocky
Copyright (c) Marimer LLC