Embedded object in RIA POCO? - silverlight

Quick question for all y'all silverlight buffs..
I need to expose a custom POCO object through ria services.. Well kind of, i could probably get away with doing it over a vanilla web service.. so i dont mind if you tell me to do it over a non-RIA service
It's a bit like this:
public partial class Monkey
{
// etc..
// This is an entity framework entity
}
public class MonkeyCollection
{
// This is the POCO
public string MonthName { get; set; }
public Monkey MonkeyForMonth1 { get; set; }
public Monkey MonkeyForMonth2 { get; set; }
// Keep RIA services quiet about the lack of a "key"
[Key]
public int ID { get; set; }
}
// In my service class
public IEnumerable<MonkeyCollection> GetMonkeys()
{
// Churn the data like butter
}
This returns a collection of the POCO's but what it doesn't return is the embedded objects (monkey).
i.e. it returns the primitives (monthname, id) but not the custom objects..
And in fact intellisense in visual studio doesnt even seem to know about those properties on the class..
What should i do?
Thanks
Daniel

You need the IncludeAttribute and the AssociationAttribute.
public partial class Monkey
{
// etc..
// This is an entity framework entity
// fill this with the ID of the collection
public ParentMonkeyCollectionId { get; set; }
}
public class MonkeyCollection
{
// all the rest ...
[Include]
[Association("monkey1", "ID", "ParentMonkeyCollectionId")]
public Monkey MonkeyForMonth1 { get; set; }
[Include]
[Association("monkey2", "ID", "ParentMonkeyCollectionId")]
public Monkey MonkeyForMonth2 { get; set; }
}
Look here for a similar case and the explanation.
Also check WCF RIA Services and DTO with association for more details.

Moved it into it's own web service.. Just feels cleaner!

Related

Which one is a better data design or object model?

Overview
I am designing a mechanism for generating dynamic controls in an ASP.NET MVC application that uses ADO.NET Entity Framework. However, my question has nothing to do with MVC and a little to do with the Entity Framework. It is about comparing two object models.
Problem Statement
In my app, a user must have the ability to interact with Web page A to specify that he wants to add such and such HTML controls to Web Page B.
When he browses Web Page B next, he must see those controls and be able to use them.
What Is Not The Challenge
I have written the code to generate the controls. That was the easy part. I used the Tag Builder, Partial Views, HtmlHelper extensions and Display & Editor templates.
The Challenge
The challenge is in arriving at a database design and an object model generated by Entity Framework to hold the metadata about the controls that need to be generated.
I have come up with a database design as shown below:
You may ignore the User and Permissions tables. They are not relevant to our discussion.
Entity Framework generates the following entities based on the above database design.
Let's call my database design as Design Option A.
I would have wanted a design that looked more like this:
Let's call this second design as Design Option B.
The code (stripped down version) for this second option would look like this:
namespace DynamicControls
{
public class DynamicControlGroup
{
public long Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Controller { get; set; }
public IEnumerable<string> Actions { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public User CreatedByUser { get; set; }
public DateTime CreationDateTime { get; set; }
public User LastModifiedBy { get; set; }
public DateTime ModificationDateTime { get; set; }
// Navigational
public ICollection<DynamicControl<T>> DynamicControls { get; set; }
}
public class DynamicControl<T>
{
public long Id { get; set; } //db Id
public string HtmlId { get; set; }
public bool ValueRequired { get; set; }
public virtual ControlType ControlType { get; protected set; }
// Every control is capable of having a default value but of a different
// type. Most controls have default values of type text (string). The
// multi-select ones (checkboxes, multi-select lists, etc.) have a default
// value of type IEnumerable<string>. So, I want to leave this generic.
// But I am not that hung-up on this. I am fine if I am required to move
// this property DefaultValue from the base class and make it a concrete
// (not generic) property for each individual child class.
// Mostly I just want the heirarchy. And before that, I want to know
// if it is a good idea to model this heirarchy. Or is it better to just
// work with what my Entity Framework produced for my db?
// Should I change my db? I can because I thought-up the design for
// those tables.
public virtual T DefaultValue { get; set; }
// Navigational
public DynamicControlGroup DynamicControlGroup { get; set; }
}
public class TextBox : DynamicControl<string>
{
public override ControlType ControlType
{
get
{
return DynamicControls.ControlType.TextBox;
}
}
public string Label { get; set; }
public int MaxLength { get; set; }
}
public class PasswordControl : TextBox
{
public override ControlType ControlType
{
get
{
return DynamicControls.ControlType.Password;
}
}
}
public class TextArea : TextBox
{
public override ControlType ControlType
{
get
{
return DynamicControls.ControlType.TextArea;
}
}
public int Rows { get; set; }
}
public class DropDownList: DynamicControl<string>
{
public override ControlType ControlType
{
get
{
return ControlType.DropDownList;
}
}
// I want something like this. That I should be able to say
//
// myDropDownListObject.Options...
//
// You'll notice that given my current database design, I have
// no direct way of accessing the options of a, say, drop down list.
// To do that, I have to make a round-about Linq query.
public ICollection<DynamicControlOption> Options { get; set; }
}
public class DynamicControlOption
{
public long Id { get; set; } // db Id
public string OptionHtmlId { get; set; }
public string OptionValue { get; set; }
public string OptionText { get; set; }
// Navigational property
public DynamicControl<IEnumerable<string>> TheControlWhoseOptionIAm { get; set; }
}
public class User
{
}
public class Permission
{
}
public enum ControlType
{
TextBox,
TextArea,
Password,
RadioButton,
Checkbox,
DropDownList,
MultiSelectList,
DatePicker,
TimePicker,
DateTimePicker
}
}
My Question
1) I feel that I'd like Design Option B better. Am I feeling right?
2) I know I can work with Design Option A just as fine but it'll involve a little round-about way to do some things. For example, to get all the options for a drop down list, there's not navigational property on the DropDownList class in Design Option A. I'll have to write a round-about Linq query to do that.
3) Is it possible to have Entity Framework come close to generating Design Option B? How? What changes will I need to make to my database design to achieve that?
Now we are working on a Project like this at our company...
If I got your meaning correctly and If I were you...I implemented inherited structure as my database design like below.
Now you Classes are inheritance but your database design is not.
I have removed Id in TextBox and I have put ControlId as PK and FK in the same time. (not just FK).
in fact,ControlId is both PK for TextBox and FK from DynamicControl
and also this way for PasswordControl and TextArea
and Now ControlId in TextBox is not Identity. It gets it's ControlId from DynamicControl
I also accept Design Option B .I'm always more comfortable than using Design Option A.in my idea It's true and main structure

Why do Entity Framework classes need a virtual member of an unrelated class

Easier to show by example -- I'm using code-first to construct a database. I have the following classes:
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public string AuthorName { get; set; }
public List<Post> Posts { get; set; }
public string BlogCode
{
get
{
return Title.Substring(0, 1) + ":" + AuthorName.Substring(0, 1);
}
}
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public virtual Blog Blog { get; set; }
}
I don't understand why Post needs a public virtual Blog Blog. Does it act as a foreign key in the database to link back to the Blog? It seems like if that were the case you would use the Blog Id.
It does allow the two tables to be related and places a foreign key on Post relating to Blog.
Having public virtual Blog Blog { get; set; } allows you to reference the Blog object from a Post object and then get access to all the properties of the Blog. E.g. myPost.Blog.Id If it used public virtual int BlogId { get; set; }, you would not be able to do this since BlogId would just be an int value.
If your domain objects are lazy loaded, myPost.Blog will not actually be hydrated with data from the database (i.e. no call to the Blog table) until that property is used. As soon as it is used, Entity Framework will make the database call for you and hydrate the object with data from the Blog table. This is part of the beauty of using an ORM... it allows you to focus on the code while it takes care of the database operations.

How to return hierarchical graph with RIA services

I would like a DomainService as follows:
[EnableClientAccess]
public class FamilyService : DomainService
{
public IQueryable<Person> GetPeople() // for the time being fake out a set of people
{
var people = new[]{ new Person(), ... };
return people.AsQueryable();
}
}
The Person class should be simple enough:
public class Person
{
[Key]
public Guid Id { get; set; }
public Person Parent { get; set; }
public String Name { get; set; }
public Person[] Children { get; set; }
}
In the Silverlight client, the Person class that is generated does not include the Parent or Children properties. What am I missing?
(Perhaps I should point out that while this is obviously a mock example, I am planning on using a loosely coupled approach, that this mimics. In fact most of my data doesn't reside in a database at all A significant majority of the RIA samples use an end-to-end parity of model from the database to the UI, which is not an option in my case.)
Try adding the [Include] attribute to the Parent & Children properties.
public class Person
{
[Key]
public Guid Id { get; set; }
[Include]
public Person Parent { get; set; }
public String Name { get; set; }
[Include]
public Person[] Children { get; set; }
}
If the Person class is coming out of the DB (in your real world app) and you are using EF, then you need to force them to be loaded eagerly, using the .Include() method.

Entity Framework / RIA Services Include not working

I've got a SL4 / WCF RIA Services / EF 4 application. I'm having trouble getting my Included entity into my SL4 data context.
In the server side service portion of the application, this is my method:
[Query(IsDefault = true)]
public IQueryable<ToolingGroup> GetToolingGroups()
{
var groups = this.ObjectContext.ToolingGroups.Include("MetaData").OrderBy(g => g.Name);
return groups; //breakpoint set here
}
I assigned it to the var groups to allow it to be inspected before the method returns. If I set a breakpoint before the method returns and add a line to my Watch window the MetaData is there:
groups.First().MetaData
When I let the method return and check it in the silverlight ui completed event MetaData is null.
void loadOperation_Completed(object sender, System.EventArgs e)
{
grid.ItemsSource = _toolingContext.ToolingGroups;
UpdateUI(); //breakpoint set here
}
When I do this in my watch window MetaData is null:
_toolingContext.ToolingGroups.First().MetaData
I checked to make sure the ToolingGroup returned by the call to .First() in both cases was the same entity and it was.
Why is MetaData lost (eg. null) between the service method and my ui method?
SOLUTION:
// The MetadataTypeAttribute identifies ToolingGroupMetadata as the class
// that carries additional metadata for the ToolingGroup class.
[MetadataTypeAttribute(typeof(ToolingGroup.ToolingGroupMetadata))]
public partial class ToolingGroup
{
// This class allows you to attach custom attributes to properties
// of the ToolingGroup class.
//
// For example, the following marks the Xyz property as a
// required property and specifies the format for valid values:
// [Required]
// [RegularExpression("[A-Z][A-Za-z0-9]*")]
// [StringLength(32)]
// public string Xyz { get; set; }
internal sealed class ToolingGroupMetadata
{
// Metadata classes are not meant to be instantiated.
private ToolingGroupMetadata()
{
}
public int Id { get; set; }
[Include] // Added so MetaData gets serialized
public MetaData MetaData { get; set; }
public Nullable<int> MetaDataId { get; set; }
public string Name { get; set; }
public ToolingCategory ToolingCategory { get; set; }
public int ToolingCategoryId { get; set; }
public EntityCollection<ToolingType> ToolingTypes { get; set; }
}
}
There are two layers at play here, EF and RIA Services. You've handled the EF part. Now you need to tell RIA services to include that property when it serializes your entities across the wire. In your metadata for the entity, add the [Include] attribute. Like this...
[MetadataType(typeof(ToolingGroup.MetaData)]
public partial class ToolingGroup {
private class MetaData {
// adding this attribute tells RIA services
// to also send this property across
[Include]
public MetaData MetaData { get; set; }
}
}
It's a bad coincidence that your type is called "Metadata", the ToolingGroup.MetaData class is the metadata that RIA services uses.

Modeling a Generic Relationship (expressed in C#) in a Database

This is most likely one for all you sexy DBAs out there:
How would I effieciently model a relational database whereby I have a field in an "Event" table which defines a "SportType"?
This "SportsType" field can hold a link to different sports tables E.g. "FootballEvent", "RubgyEvent", "CricketEvent" and "F1 Event".
Each of these Sports tables have different fields specific to that sport.
My goal is to be able to genericly add sports types in the future as required, yet hold sport specific event data (fields) as part of my Event Entity.
Is it possible to use an ORM such as NHibernate / Entity framework / DataObjects.NET which would reflect such a relationship?
I have thrown together a quick C# example to express my intent at a higher level:
public class Event<T> where T : new()
{
public T Fields { get; set; }
public Event()
{
EventType = new T();
}
}
public class FootballEvent
{
public Team CompetitorA { get; set; }
public Team CompetitorB { get; set; }
}
public class TennisEvent
{
public Player CompetitorA { get; set; }
public Player CompetitorB { get; set; }
}
public class F1RacingEvent
{
public List<Player> Drivers { get; set; }
public List<Team> Teams { get; set; }
}
public class Team
{
public IEnumerable<Player> Squad { get; set; }
}
public class Player
{
public string Name { get; set; }
public DateTime DOB { get; set;}
}
DataObjects.Net supports automatic mappings for open generics. Some details on this are described here.
You can do this by having all the Event types inherit from an abstract Event base class. This make sense to me because all the events share some common properties: date, venue, etc. You can use a table per concrete class or table per subclass strategy to store the objects in a relational database. Here are some links to articles describing inheritance mapping with NHibernate:
Chapter 8. Inheritance Mapping
Fluent NHibernate and Inheritance Mapping
NHibernate Mapping – Inheritance
The example converted to DO4 must look as follows:
// I'd add this type - adding an abstract base makes design more clean + allows you to share
// the behavior among all the descendants
[Serializable]
[HierarchyRoot]
public abstract class EventBase : Entity
{
[Key]
Guid Id { get; private set; } // Or some other type
}
[Serializable]
public class Event<T> : EventBase
where T : IEntity, new() // IEntity indicates DO4 must try to map its descendants automatically
// Although I'd put some stronger requirement, e.g. by using IEventData instead of IEntity here
{
public T Data { get; set; }
public Event(T data)
{
Data = data;
}
}
[Serializable]
[HierarchyRoot]
public class FootballEvent
{
// You need [Key] here
public Team CompetitorA { get; set; }
public Team CompetitorB { get; set; }
}
[Serializable]
[HierarchyRoot]
public class TennisEvent
{
// You need [Key] here
public Player CompetitorA { get; set; }
public Player CompetitorB { get; set; }
}
[Serializable]
[HierarchyRoot]
public class F1RacingEvent
{
// You need [Key] here
public EntitySet<Player> Drivers { get; private set; }
public EntitySet<Team> Teams { get; private set; }
}
[Serializable]
[HierarchyRoot]
public class Team
{
// You need [Key] here
public EntitySet<Player> Squad { get; set; }
}
[Serializable]
[HierarchyRoot]
public class Player
{
public string Name { get; set; }
public DateTime DOB { get; set; }
}
In this case Event instances will be available (= mapped automatically) for all suitable Ts from model. E.g. in this case they'll be:
- EventBase // Yes, even it, coz it's suitable
- FootballEvent
- TennisEvent
- F1RacingEvent
- Team
- Player
If you'd like to restrict this to just certain types, you must do the following:
- Add an interface inherited from IEntity all these types will support, e.g. IEventData.
- Use it as generic parameter constraint for generic parameter T in Event.
Cross Posted from: http://forum.x-tensive.com/viewtopic.php?f=29&t=5820,
Answer By Alex Yakunin,
Chief Executive DataObjects.NET
There are a bunch of options like XML columns and EAV (also known as database within a database), but none of which will translate well with an ORM to traditional static object-oriented languages, and all of which have drawbacks with respect to data type safety and referential integrity at the database level.
If you need this level of dynamic structure in both the database and the client, you might need to go with an object or document database (and language) which is much more dynamic by design - relational databases tend to work best with static relationships and data models.

Resources