How do I teach Automapper to project X to IContainer<Y>? - automapper

For the same reasons as this question, I would like to teach automapper to project into INC<> of vmodel below (and not back in this case), during something like SomeContext.GetIQueryable<model>().ProjectTo<vmodel>()
public interface INC<T> { T Value { get; set; } }
public class NC<T> : INC<T> { public T Value { get; set; } }
public class model { public int value { get; set; } }
public class vmodel { public IC<String> Value { get; set; } }
I'm trying to do it generically in here: https://gist.github.com/flavourous/9a2668daa41cfedb8359b26eac319df2
Here's me trying to do it explicitly: https://gist.github.com/flavourous/d35bac140bb2883ebd82846a2b75ec7e
Here's the problems I'm hitting:
Cannot cast inside nongeneric ProjectUsing of ForAllMaps
When calling ProjectUsing inside ForAllMaps, the argument is of type Expression<Func<object,object>>.
ProjectUsing((object src)=>new NC<String>("test")) works fine, but any cast on object src give me Rewriting child expression..is not allowed because it would change the meaning of the expression.
How to generate an expression mapping to the inner type?
AM needs to map the ProjectUsing expression to a query expression for the IQueryable it translates from the source IQueryable, and I want to express the conversion expression of X->Y that it would normally do if the INC<> wasn't there.
Mapper.Configuration.ExpressionBuilder.GetMapExpression<,>() looks like an option but I can't get a ExpressionBuilder inside Initialise?

Related

Passing dynamically generated value to NUnit Custom Attribute

For our test scenarios - based on configuration of the application, we may want to either enable or disable a scenario. For this purpose, I created a custom IgnoreIfConfig Attribute like this :
public class IgnoreIfConfigAttribute : Attribute, ITestAction
{
public IgnoreIfConfigAttribute(string config)
{
_config = config;
}
public void BeforeTest(ITest test)
{
if (_config != "Enabled") NUnit.Framework.Assert.Ignore("Test is Ignored due to Access level");
}
public void AfterTest(ITest test)
{
}
public ActionTargets Targets { get; private set; }
public string _config { get; set; }
}
Which can be used as follows :
[Test, Order(2)]
[IgnoreIfConfig("Enabled")] //Config.Enabled.ToString()
public void TC002_DoTHisIfEnabledByConfig()
{
}
Now This attribute would only take a constant string as an input. If I were to replace this with something generated dynamically at the runtime, Such as a value from Json file - How can I convert it to a Constant. Constant Expression, TypeOf Expression or Array Creation Expression of Attribute parameter type ? Such as Config.Enabled ?
You can't do as you asked but you can look at the problem differently. Just give the attribute the name of some property in the JSON file to be examined, e.g. "Config".
As per Charlie's suggestion : I implemented it like this -
PropCol pc = new PropCol(); // Class where the framework reads Json Data.
public IgnoreIfConfigAttribute(string config)
{
pc.ReadJson();
if(config = "TestCase") _config = PropCol.TestCase;
// Here TestCase is a Json element which is either enabled or disabled.
}

Return derived type when return type is base type c#

I have a Base abstract Class and a Derived Class
Base abstract class
public abstract class BaseData: IListingData
{
private int? _photos;
public string DataKey { get; set; }
public abstract List<Images> Photos { get; set; }
}
Derived Class
public class DerivedData1 : BaseData
{
public override List<Images> Photos
{
get
{ return new List<Images>(); } set {}
}
}
public class DerivedData2 : BaseData
{
public override List<Images> Photos
{
get
{ return new List<Images>(); } set {}
}
}
I have a Service function:
public List<ListingData> FilterListings(PredicateHandler predicates)
{
//Retrieved from database and based on certain predicates, it will create List of DerivedData1 or DerivedData2
Return new List<DerivedData1>(); //This is where the ERROR is.
}
I am unable to return Derived Type. I tried casting and I get the following same compile error. Cannot convert expression type 'System.Collections.Generic.List< DerivedData1>' to return type 'System.Collections.Generic.List< ListingData>'
I also tried changing the return type of the service function FilterListings() to the Interface IListingData, but I still encounter the a casting error.
I searched on other Stackoverflow posts. Which answers the questions of returning a derived Type from within a Base class. But I think this is a different scenario.
Bottom line, My service-class function has a return type Animal() and from inside the function I want to return Dog()
What am I missing?
In your example code, you cannot return new List of DerivedData1 where the function return type is List of ListingData.
The reason is there is no hierarchical relations between the two list types.
What you can do is:
public List<ListingData> FilterListings(PredicateHandler predicates)
{
var list = new List<BaseData>();
var list.Add(new DerivedData1());
var list.Add(new DerivedData2());
return list;
}
I'd use List<object> if I were in your place, and cast object to whatever is needed when iterating (for example). Your issue is that List<Base> and List<DerivedFromBase> are treated as unrelated (which is the intended behaviour, even if uncomfortable).

ServiceStack AutoQuery, Multiple IJoin

In my example I have the following database structure. Order has many OrderLine, which has one Product.
I am trying to return the following DTO:
public class OrderLineDto {
public int Id { get; set; }
public int Quantity { get; set; }
public string OrderType { get; set; }
public string ProductName { get; set; }
}
This should be possible by use of the following Query Route:
[Route("/orderlines")]
public class FindOrderLines : QueryBase<OrderLine, OrderLineDto>,
IJoin<OrderLine, Order>,
IJoin<OrderLine, Product>
{ }
What I am trying to do here is join OrderLine in both directions to bring in Type from Order, and Name from Product and return it in an OrderLineDto.
I am able to do these things individually by only using one IJoin, however AutoQuery appears only to use the first IJoin interface declaration, and does not perform the second join.
If I attempt to do a join like this: IJoin<OrderLine, Order, Product>
I get the following exception: Could not infer relationship between Order and Product
Is it possible to achieve what I am trying to do here with auto query or should I go back to writing standard REST services, abandoning AutoQuery?
I have submitted a pull request to ServiceStack which will now allow this behavior.
https://github.com/ServiceStack/ServiceStack/pull/955

Including a base member doesn't seem to work in Entity Framework 5

here are my entities:
public abstract class ResourceBase
{
[Key]
int Id { get; set; }
[ForeignKey("Resource")]
public Guid ResourceId { get; set; }
public virtual Resource Resource { get; set; }
}
public class Resource
{
[Key]
public Guid Id { get; set; }
public string Type { get; set; }
}
public class Message : ResourceBase
{
[MaxLength(300)]
public string Text { get; set; }
}
And then my query is something like this:
var msgs = messages.Where(x=>x.Id == someRangeOfIds).Include(m=>m.Resource).Select(x => new
{
message = x,
replyCount = msgs.Count(msg => msg.Id = magicNumber)
});
I am running this with proxy creation disabled, and the result is all the messages BUT with all the Resource properties as NULL. I checked the database and the Resources with matching Guids are there.
I drastically simplified my real life scenario for illustration purposes, but I think you'll find you can reproduce the issue with just this.
Entity Framework 5 handles inherited properties well (by flattening the inheritence tree and including all the properties as columns for the entity table).
The reason this query didn't work was due to the projection after the include. Unfortunately, the include statement only really works when you are returning entities. Although, I did see mention of a solution which is tricky and involves invoking the "include" after the shape of the return data is specified... If anyone has more information on this please reply.
The solution I came up with was to just rephrase the query so I get all messages in one query, and then in another trip to the database another query that gets all the reply counts.
2 round trips when it really should only be 1.

Entity Framework and base type with same name

I get the following error: The type 'EfTest.Person' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.
when running this code:
using System.Data.Entity;
namespace EfTest
{
internal class Program
{
private static void Main(string[] args)
{
using (var db = new PersonContext())
{
db.Persons.Add(new Person());
db.SaveChanges();
}
}
}
public class PersonContext : DbContext
{
public DbSet<Person> Persons { get; set; } //people ;)
}
public class Person : EfTest2.Person
{
public int PersonId { get; set; }
public string Name { get; set; }
}
}namespace EfTest2
{
public class Person
{
public int Age { get; set; }
}
}
can you help and explain why new type can not have same name as its base?
I would focus on the keyword nested in the error. I'm sure taking it out of EfTest2 namespace and keeping the namespace the same will correct this issue. Of course then you must change the base class name from Person to some thing else.
EF has quite complex multi-layered mapping and some of these layers are not directly visible outside. This is a problem of mapping objects into their entity representation in EDM. The point is that EF is not using full class names (including namespaces) but only bare class names so your Person classes are in collision and only one can be resolved.
The origin of this behavior is probably in EF 4.0 when it was used to simplify class creation for POCO models and make namespaces independent on the EDMX.
I think you cannot write the class next to the main session.
You have to separate it to another cs file.
I solved the problem by doing that. Worth a try

Resources