What is the difference between Entity.GetAttributeValue<T>(name) and Entity[name] in Dynamics CRM? - entity-framework

When coding against the CRM SDK using late binding what is the difference between accessing attributes using the GetAttributeValue method and accessing the attributes directly on the Entity object?
I am still very new to .NET and c# so if it is a simple language/platform understanding issue then apologies but maybe that's why I can't find much on the difference.

entity["myattributename"] will throw an exception if you attempt to access an attribute that is not in the attribute collection of the entity. It will also return it as an object.
entity.GetAttributeValue<T> will return the default value of the type if not found, and will not throw an exception if it isn't in the attributes collection of the entity.

The entity.GetAttributeValue<T> will return an IEnumerable object which can be used in LINQ expressions. This may greatly speed-up development time and reduce lines of code logic. Based on Daryl's response (good explaination here: ).

Related

RequestFactory's Entity Relationships

The details of the Request's with() implementation of RequestFactory in GWT is a bit unclear to me. See here for the official documentation.
Question 1:
When querying the server, RequestFactory does not automatically
populate relations in the object graph. To do this, use the with()
method on a request and specify the related property name as a String.
Does this mean that if the Entity at the server uses Lazy Fetching, the returned EntityProxy will have all the requested objects specified in with()? It seems a bit odd to instantiate the whole object graph of the Object server side, to only send a small piece to the client.
Question 2:
Does req.with("foo").with("foo"); do the same as req.with("foo"); ?
Question 3:
Does req.with("foo").with("bar"); do the same as req.with("foo","bar"); ?
NOTE: I'm having a really hard time finding the implementation details of with() in the source code and the API doesn't help me either.
Question 1:
It probably depends on your server side implemenation.
The with invocation will only make sure that the corresponding getter (getFoo()) is called shortly before the RF call returns to the client.
That's the reason why you also have to make sure to use an OpenSessionInView pattern, otherwise you might run into NullPointeterExceptions.
Question 2:
I guess the Request<T> implements a builder pattern.
The end-result will be the same.
However I am not sure if the getter() will be called twice or if the with method will check if the getter is already requested.
Question 3:
Yes it's the same.
As a sidenote. You can use req.with("foo.bar").
On the backend this will lead to a getFoo().getBar() call.

Dynamic resultset with dapper on 3.5

I've got a store procedure call returning a recordset whith field unknown in advance. For some interop reason I need to make it working on 3.5, so I don't have any dynamic support.
Is there some built-in solution in dapper? I did not find any by myself.
If there is no such a solution, does it make sense ( and does it work ) to create on the fly a type exposing the property I would fetch ?
EDIT
I managed to add a completely external solution ( without tweaking the original codebase ) by creating a dynamic object in c# 3.0.
Here is the extension dapper code
and here the factory for the dynamic object.
Well, actually the dynamic support in dapper is implemented via ExpandoObject, which is basically just a dictionary. It would be pretty simple to tweak the existing code to return IDictionary<string,object> instead of dynamic in the case of 3.5

GWT request .with method

I'm sorry in advance if this rather n00bish question actually has an answer in the documentation which I've just failed to find, but
I'm still relatively new to GWT, and try as I might I can't find an explanation of what the request.with(String...) method actually does which I can understand. Please can someone explain to me in words of one sylable what this method does and why you'd use it?
thanks very much
It indeed is in the doc: https://developers.google.com/web-toolkit/doc/latest/DevGuideRequestFactory#relationships
By default, entity proxies referenced from the entity proxy/ies you're fetching are not fetched (properties will simply be null on the client-side). You have to explicitly ask for them using with(), passing the name (can be a dotted path too) of the properties you want to fetch.

How to get an EntityCollection<class> instead of just a class?

I'm trying to convert a one-to-many relationship in entity framework to a many-to-many.
I've done the admin work and generated a database, which seems to be fine but now I'm running through the resulting errors and trying to get the code to compile. Most of it is fine, but one particular change is giving me a headache - I can't figure out the correct syntax. I'm sure I'm being very stupid here.
The original line was:
addedService.CompiledDatabase = context.Databases.OfType<CompiledDatabase>().First();
But addedService.CompiledDatabase is no longer looking for a single instance of CompiledDatabase, it's looking for a collection: an EntityCollection to be precise.
I can't figure out how to get a linq/lambda query that will return a collection of the required type. Assistance gratefully appreciated.
Something like?
addedService.CompiledDatabase.Attach(context.Databases.OfType<CompiledDatabase>());
...if addedService is already in context.

WCF Ria Services ChangeSet.GetOriginal(): How does it work?

I have a fairly simple question to which I cannot seem to find the answer for. I have a silverlight app with Ria Services. In the DomainService class I have an update method like below:
public void UpdateConversationState(ConversationState currentConversationState)
{
var original = ChangeSet.GetOriginal(currentConversationState);
if (original != null)
ObjectContext.ConversationStatesRepository.AttachAsModified(currentConversationState, original);
else
ObjectContext.ConversationStatesRepository.Attach(currentConversationState);
currentConversationState.UpdDat = DateTime.Now;
if(original.Name != currentConversationState.Name)
//Do something extra
}
The problem is that the Name property is always empty. In fact every field except for the Id has default values. I've tried searching for how the GetOriginal method works, but cannot find any help. It seems to mee like it tries to rebuild the original object on the server, based on the changes that are sent back from client to server.
Or maybe anyone knows a better way to check if a certain property of an object has been changed during an update? I could off course compare it to the value in the database, but it seems like I should avoid this extra call to the database.
Any help is again much appreciated :-)
EDIT:
Just found out about the RoundTripOriginalAttribute. This seems to do the trick. Am I the only one by the way that think RIA could be documented a little bit better?
Well, I've been also looking for a way track entity changes with EF4 and after some googling I've found that you need to apply the "RoundTripOriginal" attribute to the properties of the entity you want to track, because RIA (by default) does not send the original values back to the server.
I still have some concerns on this and I asked some of the gurus:
http://forums.silverlight.net/forums/t/218332.aspx
This worked for me, but I still donĀ“t think is the best way out of it.
Hope this helps.