1

In my project I am using following code to cast items.

After some time, I came to know there are two way we can cast using SitecoreService and using glass mapper Sitecore Context.

results.Select(p => SitecoreContext.Cast<PageModel>(p.GetItem(), false, false)).ToList()

Please guide me which is fastest if i am converting more than 50 items

2

They are one and the same. SitecoreContext inherits from AbstractSitecoreContext which inherits from SitecoreService.

If you look at the source code for the latter, you'll see that a call to Cast<T> as in your sample initiates a sequence of calls which eventually leads to an implementation with the following signature:

public object CreateType(Type type, Item item, bool isLazy,
    bool inferType, Dictionary<string, object> parameters,
    params object[] constructorParameters)

Therefore, for optimal performance, your best bet could be to invoke this method directly. I'm not sure how much of a performance difference it will make, however.

If you look at that method's implementation, you can see that it calls InstantiateObject (from the base class AbstractService). If you are casting your objects in a loop, you should get a good performance improvement by calling that method directly and reusing the same instance of SitecoreTypeCreationContext.

Then again, if you need to cast that many items, it's possible that the design needs to be revisited... but hard to say without more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.