سؤال

Automapper has a very nice extensions for getting projections.
In BL of our application we can just define methods like this:

IEnumerable<TEntityProjection> GetProjections()
{
    return _context.EntitiesSet.Project().To<TEntityProjection>();
}

This helps BL to stay quite simple. We do not need many different methods for each projection variant.
But... WCF service does not support open generics.
So I'm looking for the solution which allows service clients to get only desired properties of the entity.

هل كانت مفيدة؟

المحلول

Possible solution is Linq over WCF (based on BLToolkit). Under the hood, the linq query parse to SqlQuery (SQL AST), serialize and send to WCF LINQService. The service creates a sql query for DB (SQl Server, Oracle, MySql, etc), executes it and returns data. Anonymous types are supported.

Query sample:

var client = new DataModel();

var q = from p in client.Person
        select new   
{
      p.PersonID,
      p.FirstName,
      p.MiddleName,
      p.LastName,
      p.Gender
};

foreach (var p in q)
  Console.WriteLine(p);

More info: http://bltoolkit.net/Doc.LinqDataContext.ashx

Sample: https://github.com/igor-tkachev/bltoolkit/blob/master/Demo/Linq/OverWCF/Program.cs

نصائح أخرى

That will not work in the way you are trying to do it.

If you need specific properties at the receiving end, you can:

  • Create separate methods, where each method exposes a particular pre-defined set of properties per entity
  • Create methods that take a "list of properties" parameter (array of strings, possibly) and that return a string with XML or JSON, which then requires some assembly on the client
  • Just ignore the unneeded properties on the client

I suppose it depends on your case specifics which would be best.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top