Nestor
Nestor

Reputation: 8384

How to create POCO classes based on a WCF Data Service

I have a consumable WCF Data Service (let's assume that I can't access its source code).

I need to have POCO classes that represent the same classes that are provided by the data service.

Here is an example.

Service:

public class Info
{
  public int ID {get; set}

  public string Data {get; set}

}

public IQueryable<Info> GetInfo()
{
  return from info in context.Info
         select info;
}

Client:

//should be generated
public class Info
{
  public int ID {get; set}

  public string Data {get; set}

}

All I can access is the service URI.

Obviously I could use the built-in proxy generator tool (datasvcutil.exe), but it generates a lot of code that I do not require.

Like the context:

public partial class MyEntities : global::System.Data.Services.Client.DataServiceContext
    {
        /// <summary>
        /// 
        /// </summary>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
        public MyEntities(global::System.Uri serviceRoot) : 
                base(serviceRoot)
        {
            this.OnContextCreated();
        }
        partial void OnContextCreated();
        /// <summary>
        /// 
        /// </summary>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
        public global::System.Data.Services.Client.DataServiceQuery<Info> Info
        {
            get
            {
                if ((this.Info == null))
                {
                    this._Info = base.CreateQuery<Info>("Info");
                }
                return this._Info;
            }
        }

Or the classes that are full of annotations and methods:

[global::System.Data.Services.Common.DataServiceKeyAttribute("ID")]
public partial class Info
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="Data"></param>        
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
    public static Info CreateInfo(int id,  string data)
    {
        Info info = new Info();
        info.ID = id;
        info.Data = data;            
        return info;
    }

All I want is the pure POCO classes.

Is there a good approach to generate those classes based on the data service?

Upvotes: 1

Views: 288

Answers (2)

Nestor
Nestor

Reputation: 8384

As I couldn't find any suitable solution, I created a library that creates the necessary POCO classes based on the WCF Data Service metadata.

It is open source, you can get it here: https://github.com/nestorium/DS2POCO

I hope you will find it useful :)

Upvotes: 0

user6544336
user6544336

Reputation:

Conceptional a WCF service generates a data contract. So i think you will have all information you need. If you still want to generate classes try using Reflection and CodeDom. https://msdn.microsoft.com/en-gb/library/saf5ce06(v=vs.110).aspx

Upvotes: 1

Related Questions