AutoMapper issue - asp.net

Trying to automap some objects.
Source objects has properties with _ before name, destination objects - have not.
Is it possible to implement ONE map creation, that automapper would map all _properties to properties
for all source types.
class MyMapper<TFrom, TTo>{
TTo PerformMap(TFrom fromObject){
Mapper.CreateMap<From, To>(); // ???
TTo result = Mapper.Map<From, To>(fromObject);
//result.Id.ShouldBe(value from TFrom._Id);
return result;
}
}
class From
{
public int _Id { get; set; }
public string _Name { get; set; }
}
class To
{
public int Id { get; set; }
public string Name { get; set; }
}

Something I added recently to AutoMapper might help you - custom naming conventions. If you check out the trunk (R107), look around for INamingConvention. Right now, I have two naming conventions (PascalCase and lower_case_underscore), but it's really just a matter of figuring out the right RegEx to get you going:
INamingConvention.cs
Right now, naming conventions are global and profile-scoped. Since this feature is new, there isn't any documentation other than the tests.

This is how I'm doing it
Mapper.Initialize(cfg =>
{
cfg.RecognizeDestinationPrefixes(new []{"_"});
cfg.RecognizePrefixes(new[] { "_" });
cfg.CreateMap<To, From>().ReverseMap();
});

For this you could add a custom mapping to solve this particular case:
Mapper.CreateMap<From, To>()
.ForMember( dest => dest.Id, opt => opt.MapFrom( src => src._Id ) )
.ForMember( dest => dest.Name, opt => opt.MapFrom( src => src._Name ) );

Related

Does using a navigation collection property in a .Where predicate require explicit hydration?

Say I have two models/tables.
public class ParentEntity
{
public Guid ID { get; set;}
public List<ChildEntity> ChildEntities { get; set; } // navigation property
}
public class ChildEntity
{
public Guid ID { get; set; }
public Guid ParentEntityID { get; set; } // foreign key
}
If I run a query like this:
var parentEntities = await _context.ParentEntities.Where(x => x.ChildEntities.Any()).ToListAsync();
From what I can tell (based mostly on experimentation), this query does not require explict hydration of ChildEntities (using .Include(x => x.ChildEntities)).
If I wanted to do something with the ChildEntities outside of the query/after the list is materialized, I would need to explicitly hydrate ChildEntities:
var parentEntities = await _context.ParentEntities.Include(x => x.ChildEntities)
.Where(x => x.ChildEntities.Any()).ToListAsync();
foreach (var parentEntity in parentEntities)
{
foreach (var childEntity in parentEntities)
{
// do something with childEntity
}
}
That's my understanding, anyway, and that's how it seems to work. However, I'm hoping to find some Microsoft documentation that mentions this explicitly. I haven't been able to find anything (all the search keywords I can think to use point me in the direction of Filtered Includes, which is NOT what I'm wondering about).
I want to be confident that my understanding is correct, and that I haven't just gotten "lucky" by the child entities already being hydrated from other queries in the same context.
Include needed ONLY for loading related entities and ONLY for such purpose. It has no affect on filter or projection. You can omit Include if you do not plan to load related entities.

Entity Framework with ASP.NET one-to-many relation causes NullReference error

In my database I have two entities: DbStatus and DbTask
public class DbStatus
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<DbTask> Tasks { get; set; }
}
public class DbTask
{
public int Id { get; set; }
public string Title { get; set; }
public bool Done { get; set; }
public int StatusId { get; set; }
public virtual DbStatus Status { get; set; }
}
In the OnModelCreating method, I establish the relation with the following code:
modelBuilder.Entity<DbStatus>()
.HasMany(s => s.Tasks)
.WithOne(t => t.Status)
.HasForeignKey(t => t.StatusId);
I also add some sample data in this method, setting the StatusId of newly created DbTasks.
Problem is, when I try to access the Status name of the DbTask using
task.Status.Name
I get a NullReferenceException.
Can anyone help me how to set up the relation properly?
IMPORTANT
For anyone reading this, the quickest solution (and the one fulfilling task-specific criterias) for this was provided Rob. However, you should read and implement the solution provided by Steve Py, for the reasons they also describe in their answer!
When getting your list of DbTasks from the database, you need to tell it to include the child Status objects.
Try something like this:
var tasks = dbContext.DbTasks
.Include(t => t.Status)
.ToList();
Setting a FK on an entity does not automatically cause that related entity to be loaded. When working with navigation properties I recommend avoiding declaring FK fields in entities and using shadow properties to avoid issues like this.
To update a status on a DbTask:
public ActionResult MarkTaskComplete(int taskId)
{
var completeStatus = _context.Statuses.Single(x => x.StatusId = Statuses.Complete);
// TODO: Validation that user can update task etc.
var task = _context.Tasks
.Include(x => x.Status)
.Single(x => x.TaskId == taskId);
task.Status = completeStatus;
_context.SaveChanges();
return Json(new { success = true; status = task.Status.Name } );
}
The issue with FK fields is that the behaviour can differ depending on whether you use the navigation property or the FK, and whether the navigation property is eager loaded or not. From the perspective of the Task, there are two sources of truth for the current Status, some code might check task.StatusId while others use task.Status.StatusId. These values could differ depending on one being updated without the other.
While this can mean a trip to the DB to fetch a status, fetching rows by ID is extremely fast, and also provides a validation that your methods are only using legal values.

Ordering Entity Framework items and child items for MVC view

How can I sort a query from a DbSet and include child items which should also be sorted.
Example:
I have a model for scheduling orders.
public class Order
{
public virtual int Id { get; set; }
public virtual int? SchedulingOrder { get; set; }
public virtual int? WeekId { get; set; }
public virtual Week Week { get; set; }
}
public class Week
{
public virtual int Id { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
...
public DbSet<Week> Weeks { get; set; }
public DbSet<Order> Orders { get; set; }
Then an action method
public ActionResult ShopSchedule()
{
return View(db.Weeks.OrderBy(w => w.StartDate)
.Include(w => w.Orders.OrderBy(o => o.SchedulingOrder))
.ToList());
}
This doesn't work I think because of the nature of Include. Do I have to create a separate view model and map to it? Or is there some way to get around it right there in the query? There is some kind of syntax where people say new { left = right, etc } within the query?
related questions:
Ordering Entity Framework sub-items for EditorFor
C# Entity Framework 4.1 Lambda Include - only select specific included values
It's worth noting that the other 2 solutions here pull the data via SQL, then reorder things in memory, which is very wasteful in terms of performance during both the query and the post-processing. This solution gets things in one go via SQL alone, without the extra in-memory step.
It can be done as described in the second approach here:
How to order child collections of entities in EF
Like:
db.VendorProducts.Select(p =>
new { Product = p, S = p.Schedules.OrderBy(s => s.From) })
.FirstOrDefault(q => q.Product.Id == id).Product
So instead of an Include statement, you call up the related data in an anonymous object along with the original root data you were going to fetch, order it in that subquery and finally return the root data. The ordering remains intact. Twisted but it works.
To stick with your original code:
db.Weeks.Select(w => new { W = w, O = w.Orders.OrderBy(o => o.SchedulingOrder) })
.OrderBy(q => q.W.StartDate).Select(q => q.W);
You are right, you can't use orders in Include, it's not meant to work that way. But you could sort the results within the view using the OrderBy on the Orders collection. Also, you're returning a result directly, shouldn't it be return View(db.Weeks...);
Something like this should work :
public ActionResult ShopSchedule()
{
var vw = db.Weeks.OrderBy(w => w.StartDate)
.Include(w => w.Orders)
.ToList();
vw.Orders = vw.Orders.OrderBy(o => o.SchedulingOrder).ToList()
return view(vw);
}

AutoMapper and reflection

My shared hosting company doesn't allow Reflection.
How can I use AutoMapper?
Do I have to specify for each property a .ForMember?
Mapper.CreateMap<Person, PersonData>()
.ForMember(dest => dest.Name, o => o.MapFrom(src => src.Name))
.ForMember(dest => dest.Address, o => o.MapFrom(src => src.Address));
thanks,
Filip
Automapper uses reflection.emit, are you sure you can use Automapper?
[Edit]
Dont know of any that uses without reflection, even the one I had created XmlDataMapper on CodePlex uses reflection. It would difficult to design one without reflection or reflection.emit
The simplest and basic way to do this would be this, you can use any of the two or both techniques.
public class ConversionHelper
{
public static ClassB Convert(ClassA item)
{
return new ClassB() { Id = item.Id, Name = item.Name };
}
public static List<ClassB> Convert(List<ClassA> list)
{
return list.Select(o => new ClassB() { Id = o.Id, Name = o.Name }).ToList();
}
}
public class ClassA
{
public int Id { get; set; }
public string Name { get; set; }
}
public class ClassB
{
public int Id { get; set; }
public string Name { get; set; }
}
From the sample you have given where you are anyways trying to map property one by one, this is on the same lines, but with lesser code.
You cannot use Automapper or any other mapping architecture that I know of without reflection. This is logically obvious. How could you map two unknown entities to one another without using any of their reflected properties? Your only option in this case is to create a custom package to convert one object into another.
Not at all. AutoMapper did a great job on intelligent mapping. If the property name of your source and destination object is the same, AutoMapper will map this proprties automatically for you.

Mocking ChildProperty cannot get it to work?

I am trying to test a property that is nested in a child class.
I always get an error.
Am I missing something?
Is it possible to test a child property in moq.
I have the following
[Test]
public void Should_be_able_to_test_orderCollection()
{
var orderViewMock = new Mock<IOrderView>();
orderViewMock.SetupGet(o => o.Customer.OrderDataCollection.Count).Returns(2);
orderViewMock.SetupSet(o => o.Customer.OrderDataCollection[1].OrderId = 1);
orderViewMock.VerifySet(o => o.Customer.OrderDataCollection[1].OrderId=1);
}
public class CustomerTestHelper
{
public static CustomerInfo GetCustomer()
{
return new CustomerInfo
{
OrderDataCollection = new OrderCollection
{
new Order {OrderId = 1},
new Order {OrderId = 2}
}
};
}
}
public class CustomerInfo
{
public OrderCollection OrderDataCollection { get; set; }
}
public class OrderCollection:List<Order>
{
}
public class Order
{
public int OrderId { get; set; }
}
public interface IOrderView
{
CustomerInfo Customer { get; set; }
}
You can't mock the OrderDataCollection property of CustomerInfo because it's a non-virtual property on a concrete class.
The best way to fix this would be to extract an interface from CustomerInfo and let IOrderView return that instead:
public interface IOrderView
{
ICustomerInfo Customer { get; set; }
}
It is definitely possible if you have the right abstractions. You need to mock your Customer and its children too, for your example to work, like:
var customerMock = new Mock<ICustomer>();
orderViewMock.SetupGet(o => o.Customer).Returns(customerMock.Object);
etc. for the entire hierarchy of child objects you want to control with mocks. Hope it makes sense.
/Klaus
You will get a runtime error, as you've found:
System.ArgumentException: Invalid setup on a non-overridable member:
o => o.Customer.OrderDataCollection.Count
at Moq.Mock.ThrowIfCantOverride(Expression setup, MethodInfo methodInfo)
You can mock the IOrderView and return any CustomerInfo instance you want, but you're also trying to mock CustomerInfo and OrderCollection. As Mark Seemann mentioned, you can only mock interfaces and virtual properties/methods. This will hold true for almost any mocking/isolation framework except for Typemock (commercial).
As others have already stated, one way to solve the problem is to return an interface for the customer.

Resources