Answered by:
Returning Windows Runtime types returned from Windows Store Class library?

Question
-
Hi there,
Does anyone know if it's possible to return a valid WinRT type from separate windows store class library?
What I'm trying to do is return a WinRT type which my store app can use from a library which is able to convert from my non RT entity types.
At present I'm having to construct the WinRT types in my RT library without using the entity types are parameters (as the entity types aren't valid RT types), but I'd like to be able to use a constructor for type mapping as it would be far more convenient and re-usable for various methods in my RT component.
Does anyone have any thoughts/ideas on this kind of process?
Many thanks,
Doug
Saturday, March 30, 2013 11:24 AM
Answers
-
Hi Doug,
I think your current implementation (put the RTUser type in RT component library and also put the mapping, converting code inside RT component's code) is reasonable. So far for windows store javascript app, it can only use pure WinRT components. So you can encapsulate all the converting details (convert a DB entity class to WinRT ViewModel class in a static class's static method) inside the RT component's implementation. But for any classes or methods that will be exposed (to windows store app) from the component, they have to strictly follow the RT component requirements. If you're using C#/VB.NET based windows store app, then you can use standard .NET class library which will be more flexible than WinRT component library.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Song Tian Monday, April 8, 2013 7:05 AM
Tuesday, April 2, 2013 7:08 AMModerator
All replies
-
Hi Doug,
You can surely define some custom types and return instances of them from windows runtime library to javascript windows store app as long as you follow the windows runtime type requreiments. Here are some reference about declaring windows runtime types and creating windows runtime components:
#Creating Windows Runtime Components in C# and Visual Basic
http://msdn.microsoft.com/en-us/library/windows/apps/br230301.aspx#JavaScript Representation of Windows Runtime Types
http://msdn.microsoft.com/en-us/library/hh710232(v=vs.94).aspxPlease remember to mark the replies as answers if they help and unmark them if they provide no help.
Monday, April 1, 2013 3:53 AMModerator -
Hi Steven,
Thanks for the reply, I've had a read over those examples but they don't quite describe what I'm trying to achieve. I'll add some pseudo code below to illustrate the problem a little better:
Say I have a windows store class library:
// class library public sealed class User { public string Name { get; set; } }
public IList<User> GetUsers()
{
return new List { //users in here };
}
And I have a runtime component:
// runtime component public sealed class RTClass {
public IList<User> GetUsersFromClassLib()
{ var classLib = new classLib();
return classLib.GetUsers();
} }
In this situation, my 'User' class is reported as a non valid runtime type (even though there isn't anything which, if placed inside the runtime component wouldn't be valid), presumably because it's not defined inside the runtime components namespace.
To put the example into context, my class library is a BL/Service library which will access entities from a database and what I'd like to be able to do is map between these db entities to 'viewmodel' RT types. My original hope was to user a constructor in my RT component which would take the db entity as a parameter. e.g.
// runtime component public sealed class RTUser { public RTUser(DBUser user) { //do mapping in here, e.g. return new RTUser { FullName = user.FirstName + " " + user.LastName; } } }
I can't seem to make this work as the RT component doesn't like the fact that my DBUser isn't a valid RT type (I can't adjust the DBUser class, which isn't sealed, so I don't have a lot of options here.)
So, my second thought was to create a DTO type library which would be able to perform the mapping between DBUser and RTUser and return instances of RTUser to the RTcomponent. Unfortunately the RT component still doesn't want to return types defined in another class library, even if they are marked as sealed and don't return anything which would be fine if defined in the RT component directly.
You could say 'just define the RTUser type in the RTcomponent and do the mapping in there'..which I am currently doing, but because I can't user the DBUser as a parameter for any mapping function I'm having to create lengthy, non re-usable functions in my RTcomponent, which look something like this:
// runtime component, real example when getting surveys // what I have to write: public IList<SurveyViewModel> GetSurveys() { var data = _surveyService .GetSurveys() .Select(x => new SurveyViewModel { Id = x.Id, SurveyDate = x.Date, SurveyTitle = x.Title, SurveyType = x.SurveyType.Name, Locked = x.Locked, SeverityColour = this.GetMaxSeverity(x.SurveyCompartments), CompletionPercentage = this.GetCompletionPercentage(x.SurveyCompartments), Compartments = x.SurveyCompartments.Select(z => new CompartmentViewModel { Name = z.Compartment.Name }).ToList(), Group = new Group { key = "Survey", title = "Surveys", subtitle = "Surveys" } }); return data.ToList(); } // what I'd like to write: public IList<SurveyViewModel> GetSurveys() { var data = _surveyService .GetSurveys() .Select(x => new SurveyViewModel(x)) return data.ToList(); }
Does anyone have any thoughts on how best to get around this? I'm open to any ideas!
Many thanks,
Doug
Monday, April 1, 2013 11:29 AM -
Hi Doug,
I think your current implementation (put the RTUser type in RT component library and also put the mapping, converting code inside RT component's code) is reasonable. So far for windows store javascript app, it can only use pure WinRT components. So you can encapsulate all the converting details (convert a DB entity class to WinRT ViewModel class in a static class's static method) inside the RT component's implementation. But for any classes or methods that will be exposed (to windows store app) from the component, they have to strictly follow the RT component requirements. If you're using C#/VB.NET based windows store app, then you can use standard .NET class library which will be more flexible than WinRT component library.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Song Tian Monday, April 8, 2013 7:05 AM
Tuesday, April 2, 2013 7:08 AMModerator