This project is read-only.

FluentMetadata, a Fluent API for defining metadata for WCF RIA Services entities

With permission of Nikhil Kothari, I've added the source code of Nikhil's Fluent API for defining WCF Ria Services Metadata to RIA Services Contrib. I've made a couple of changes. Most noticeable is the ability to attach a MetadataClass to your domain service using the FluentMetadata attribute. This class mimics the OnModelCreating method in EntityFramework DbContext and allows you to instantiate your Metadata classes. The original version is available here.

The main advantage of this package is that it allows you to separate your datamodel and metadata in separate projects/assemblies. This is not possible with the standard RIA services approach of buddy classes. These have to be defined in the same project as your datamodel. The drawbacks of this are a pollution of your datamodel and (most importantly) that your datamodel project becomes dependent on RIA services-specific assemblies. With the FluentMetadata package you can define metadata in any assembly you like.

Download: the package is available as a Nuget package.

Changes in version 0.2.69239.0
  • The fluent API now supports defining associations, e.g.
metadataContainer.Entity<Bar>()
    .Projection(x => x.FooSet)
        .Association()
            .WithName("MyAssociation")
            .WithThisKey(x => x.Id2, x=>x.Id)
            .WithOtherKey(x => x.Id2, x=>x.Id);
  • Metadata can be defined in a Metadata class per type or directly in your IFluentMetadataConfiguration
  • The Fluent API is more open such that it can be extended in third-party libraries

Using FluentMetadata
  • Install the FluentMetadata Nuget package (or download its source code)
  • Define a MetadataConfiguration class containing the fluent metadata for your entities. For example:

public class FluentMetadataConfiguration : IFluentMetadataConfiguration
{
    public void OnTypeCreation(MetadataContainer metadataContainer)
    {
        metadataContainer.Entity<Foo>().Projection(x => x.SomeProperty).Exclude();
    }
}
  • Alternatively, you can define the metadat for each entity in separate MetadataClass classes. For example
public class FooMetadata : MetadataClass<Foo>
{
    public FooMetadata()
    {
        this.Projection(x => x.ExcludedString).Exclude();
        this.Validation(x => x.RequiredString).Required();
        this.Validation(x => x.RegularExpressionString).RegularExpression("[a-z]");
    }
}
  • ... and instantiate it from your FluentMetadataConfiguration class, like so:
public class FluentMetadataConfiguration : IFluentMetadataConfiguration
{
    public void OnTypeCreation(MetadataContainer metadataContainer)
    {
        metadataContainer.Add(new FooMetadata());
    }
}
  • ... both mechanisms can be combined.
  • Lastly, add the FluentMetadata attribute to your domain service:

[EnableClientAccess()]
[FluentMetadata(typeof(FluentMetadataConfiguration))]
public class FluentMetadataTestDomainService : DomainService
{
}

Last edited Jun 5, 2012 at 1:21 PM by MdeJ, version 9

Comments

No comments yet.