Automapper Constructor Parameters - automapper-2

I've just started using this and immediately ran into a problem mapping a parameter to a a constructor parameter.
I've tried every example I can find on SO but none seem to work and the documentation doesn't mention this feature as far as I can see.
The examples show:
Mapper.CreateMap<UserProfile, UserProfileModel>().ConstructUsing(x => new UserProfileModel(x.Id);
I can't figure out the syntax to get access to the Id property on the UserProfile object.
Another example shows:
Mapper.CreateMap<TypeOneDto, TypeOne>().ConstructUsing((Func<ResolutionContext, TypeOne>) (r => new TypeOne()));
On using any of these lambdas I just have access to the ResolutionContext not the parent object?
Any Ideas?

Related

NHibernate QueryOver projection on many-to-one

I am trying to get a QueryOver working using a Projection on a many-to-one.
The class "Post" has a property many-to-one "Creator".
Using
session.QueryOver(Of Post).
Select(Projections.
Property(of Post)(Function(x) x.Creator).
WithAlias(Function() postAlias.Creator)).
TransformUsing(Transformers.AliasToBean(Of Post)()).
List()
works BUT each creator is retrieved by a single query rather than using a join like it is done when not using a select/projection. So if there are 5 posts with 5 different creators, 6 queries will be run 1 for the list of posts and 5 for the creators.
I tried to get it working using a JoinAlias but nothing really did the job.
I already searched for a solution, but all solutions I found did use the Linq-Provider which does not really fit since the actual "field list" is passed via a parameter.
Does anyone know if there is a solution to this other than the linq provider?
There is a solution, we can use projections for many-to-one and then custom result transformer.
DISCLAIMER: I can read VB syntax but do not have enough courage to write... I expect that you can read C# and convert it into VB....
So we can have projection like this:
// aliases
Post root = null;
Creator creator = null;
// projection list
var columns = Projections.ProjectionList();
// root properties
columns.Add(Projections.Property(() => root.ID).As("ID"));
columns.Add(Projections.Property(() => root.Text).As("Text"));
// reference properties
columns.Add(Projections.Property(() => creator.ID).As("Creator.ID"));
columns.Add(Projections.Property(() => creator.FirstName).As("Creator.FirstName"));
// so our projections now do have proper ALIAS
// alias which is related to domain model
// (because "Creator.FirstName" will be use in reflection)
var query = session.QueryOver<Post>(() => root)
.JoinAlias(() => root.Creator, () => creator)
.Select(columns)
Now we would need smart Transformer, our own custome one (plugability is power of NHibernate). Here you can find one:
public class DeepTransformer
And we can continue like this
var list = query
.TransformUsing(new DeepTransformer<Post>())
.List<Post>()
Check also this:
Fluent NHibernate - ProjectionList - ICriteria is returning null values
NHibernate AliasToBean transformer associations

RavenDB does not set all the properties on store

I have a really weird scenario where I try to store domain events (I'm trying to learn CQRS and RavenDB at the same time). The basic structure of the documents I try to store are:
public interface IDomainEvent { ... }
public abstract class BaseDomainEvent : IDomainEvent { ... }
public class DomainEventA : BaseDomainEvent { ... }
public class DomainEventB : BaseDomainEvent { ... }
Given that I want to store DomainEventA and DomainEventB in the same collection in RavenDB and I have managed to do so. But the problem is that in the collection I am missing the properties of DomainEventB, and not all properties are set even though I have checked that the properties are set before I commit the transaction where I store the objects. The following gist shows a working example of what I want to do: https://gist.github.com/2830093, and the test code that fails me is found in this test: https://github.com/mastoj/TJ.CQRS/blob/ravenfail/TJ.CQRS.RavenEvent.Tests/RavenEventStoreTests.cs that is using this RavenDB code: https://github.com/mastoj/TJ.CQRS/blob/ravenfail/TJ.CQRS.RavenEvent/RavenEventStore.cs.
I really can't get my head around this one.
EDIT 1: I can add that in the failing scenario the metadata of the stored object says it is one type but the properties for that type is not stored.
I planned to delete or vote for close but I think more than me might experience this problem at some point. I found the solution in my case and it was that the objects I added to RavenDB had a faulty equals method so RavenDB thought that all my objects were the same one. When I added one more property to check in the equals method everything start working as expected.

Mapping to custom types in NHibernate 3.2

I have just started to use NHibernate 3.2 with its new Conformist API, having used previous versions with Fluent a while back. The basic stuff seems fine but I am currently struggling with trying to map a string to a custom type.
In this specific case, I have a string which is a semi-colon separated list of roles in a column on one of my tables. When I get it out, I want it to be mapped into a "RoleSet" custom object that I have created by passing the string value from the database into its constructor.
I have created a IUserType but I cannot see how to tell it to use it.
Previously with Fluent I would have done this in my map class:
Map(x => x.Roles).CustomType<RoleSetType>();
Is there an equivalent way to do this is in the new API?
Give this a try...
Property(x => x.Roles, x => x.Type(typeof(RoleSetType), null));

Problem with RavenDB 'Hello World' tutorial

I am going through the RavenDB tutorial on the RavenDb.net website.
It was going fine until I got to the code block for creating an index.
This code segment is direct from RavenDB.Net website.
store.DatabaseCommands.PutIndex("OrdersContainingProduct", new IndexDefinition<Order>
{
Map = orders => from order in orders
from line in order.OrderLines
select new { line.ProductId }
});
I get an error on compile: "The non-generic type 'Raven.Database.Indexing.IndexDefinition' cannot be used with type arguments."
If IndexDefinition is non-generic, why is it used as generic in the sample code? Where is the disconnect?
Thank you for your time
Jim
Depending on your using statements you may be referencing the wrong IndexDefinition class (from another Raven assembly). Try adding this to the beginning of your file:
using Raven.Client.Indexes;
You might need to remove other using statements as well. I guess this is one reason why Microsoft recommends using unique names for classes even in the presence of namespaces.

RhinoMocks expecting call with the exact value

Sometimes Rhino.Mocks is driving me mad, 'cause there's not enough documentation on topics that, I suppose, are relatively easy.
What I want to do is to expect call to AddContact("test", contact). So for the second parameter I must use parameter constraint Property.AllPropertiesMatch(contact). But what should I use for the first one?
_contactManagerMock
.Expect(m => m.AddContact(null, null))
.Constraints(??????????, Property.AllPropertiesMatch(contact));
What goes instead of "??????????"
I was looking for this as well, here is a more detailed answer.
This is an example of how to use the AllPropertyMatch in Rhino.Mocks. I tested this in Rhino.Mocks 3.6.
//arrange
var contactManagerMock = MockRepository.GenerateMock<IManager>();
contactManagerMock.Expect(m => m.AddContact(
Arg.Is("test"),
Arg<Contact>.Matches(Property.AllPropertiesMatch(contact))))
//Act
//Perform action here that should result in the above expected call
//Assert
contactManagerMock.VerifyAllExpectations();
This says to expect the AddContact method to be called. The first parameter should be a string with the value 'test' the second should be an object of type Contact that has all the same properties as the instance of contact.
Calling VerifyAllExpectations performs the assertion.
More info on the Rhino.Mocks site.