C# fluent nhibernate - fluent-nhibernate

How should the following mapping configuration be solved?
public abstract class RepositoryEntity
{
public virtual int Id { get; set; }
}
public class Descriptor : RepositoryEntity
{
public virtual String Name { get; set; }
public virtual DateTime Timestamp { get; set; }
}
public class Proxy<TDescriptor> : RepositoryEntity
{
public virtual TDescriptor Descriptor { get; set; }
public virtual Byte[] SerializedValue { get; set; }
};
public class TestUnit : Proxy<Descriptor>
{
};
I receive problems when testing the TestUnit mapping - it says it's impossible to map the item with generic parameters. This happens if I attempt to map every class from the specified before.
If I attempt to map everything, except Proxy<T>, then I receive that there is no persister for the 'TestUnit'.
If I stop inheriting TestUnit from Proxy<Descriptor>, the mapping test works fine.
Does Fluent NHibernate have possibility to automap types inherited from some concrete Class<T> template? Could you help me with mapping these entities?

I used a combination of Fluent and Auto mappings.
Fluent mappings should be used for generics.
Configuration = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.ShowSql().InMemory)
.Mappings(x =>
{
x.FluentMappings.AddFromAssemblyOf<RepositoryEntity>();
x.AutoMappings.Add(autoPersistenceModel);
});

Related

Automapper profile maps its props to my dto

Hi.
I want just map entity to dto but profile props map to the dto.
Now what can i do for the following problem?
public abstract class DtoProfile<TEntity, TDto> : Profile, IProfile
{
public DtoProfile()
{
var profile = CreateMap<TEntity, TDto>();
CustomMapping(profile);
}
public virtual void CustomMapping(IMappingExpression<TEntity, TDto> mapping)
{
}
}
public class UserDto : DtoProfile<Users,UserDto>
{
public int Id { get; set; }
public string FullName { get; set; }
public string NationalCode { get; set; }
public DateTime CreatedDate { get; set; }
}
You're doing this in a wrong way. Mapping Profiles should be for mapping, and not to be inherited from the Dto itself.
You're inherting from Profile in DtoProfile, and these properties are defined in Profile, so they will be there. And as the name of class, it's for mapping profile, and shouldn't be used for Dto.
You should have two classes, one is UserDto where you add all the properties you need to map, and the other is UserDtoProfile which inherits from DtoProfile<User, UserDto> and apply your mappings.

Fluent NHibernate Automap error while mapping XmlDocument Column

I am new to NHibernate and am facing some issues with Fluent NHibernate automap.
I am using Nhibernate 3.3.3.400, Fluent Nhibernate 1.3.0.733 Automapper 2.2.1
I have a column in Database which is of type Xml. When I try to create a ma mapping column it give me the following error.
An association from the table Product refers to an unmapped class: System.Xml.XmlDocument
Following is the code I am trying to implement.
using System.Collections.Generic;
using System.Xml;
//using System.Xml.Linq;
namespace Examples.FirstAutomappedProject.Entities
{
public class Product
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual double Price { get; set; }
public virtual Location Location { get; set; }
public virtual IList<Store> StoresStockedIn { get; set; }
public virtual XmlDocument SalesRange { get; set; }
public Product()
{
StoresStockedIn = new List<Store>();
}
}
}
I have been struggling for a couple of days now anhy help or samples would be greatly appreciated.
it seems it FNH will not map it on it's own. you'll need a Override there
Map(x => x.SalesRange).CustomType<NHibernate.Type.XmlDocType>();
Since Firo has sent his answer in comment I am answering it on his behalf.
basically here is what I ended up doing.
I created an override class as follows
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
using Examples.FirstAutomappedProject.Entities;
using NHibernate.Mapping;
using NHibernate.Type;
namespace Examples.FirstAutomappedProject.Overrides
{
public class OrderQueueOverride : IAutoMappingOverride<Product>
{
public void Override(AutoMapping<Product> mapping)
{
mapping.Map(x => x.SalesRange).CustomType<XmlDocType>();
}
}
}
So the auto mapper will pick this override and map the column accodingly.

Fluent Nhibernate map list of lists

I am writing mapping for the class with Fluent Nhibernate:
public class UniqueFeaturesSet : IEntity
{
public UniqueFeaturesSet(List<List<double>> mfcc)
{
MFCC = mfcc;
}
public virtual List<List<double>> MFCC { get; set; }
public virtual int Id { get; set; }
}
How to map List<List<double>> ?
It is not possible to map nested collections with [Fluent][N]Hibernate. The inner collection needs to be in an own class.

Automapper and NHibernate lazy loading

I am struggling with this issue:
I have a list of NHibernate objects called "Project". These objects contain a lazy - loaded list of "Branches". I am trying to pass a list of Projects to a WCF service so I am using AutoMapper to transform them to flat objects.
The problem is that even though the destination objects called "ProjectContract" does not contain a list of Branches, Automapper still invokes this collection and a lot of queries are made to the database because NHibernate fires the lazy - loading and loads the Branches collection for each project.
Here are the classes and the mapping:
public class Project
{
public virtual int ID
{
get;
set;
}
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual IList<Branch> Branches { get; set; }
}
[DataContract]
public class ProjectContract
{
[DataMember]
public virtual int ID
{
get;
set;
}
[DataMember]
public virtual string Name { get; set; }
[DataMember]
public virtual string Description { get; set; }
}
public class ProjectMappings : Profile
{
protected override void Configure()
{
Mapper.CreateMap<Project, ProjectContract>();
}
}
My question is: Is there a way to tell AutoMapper to not touch the "Branches" collection because I don't care about it and that is a proxy that will trigger many database calls?
I temporarily fixed this with MaxDepth(0), but there are other entities where I have collections that I want to transfer, and collections that I don't want to be touched, like this one. In that case, MaxDepth(0) will not work.
Thank you,
Cosmin
Yes, The AutoMapper Ignore function.
Mapper.CreateMap<Source, Destination>()
.ForMember(dest => dest.SomeValuefff, opt => opt.Ignore());

How do I map a typed Dictionary in fluent nHibernate using automapping?

My class:
[PersistClass]
public class ExternalAccount
{
public virtual AccountType AccountType { get; set; }
public virtual int Id { get; private set; }
public virtual User User { get; set; }
public virtual Dictionary<string, string> Parameters { get; set; }
public ExternalAccount()
{
Parameters = new Dictionary<string, string>();
}
}
The Dictionary is not getting mapped. I understand that automapping doesn't work by default with Dictionaries, how do I configure the mapping? All Parameters is is a list of key/value pairs - so I would expect them to be stored in a table with a foreign key to the externalaccount table. I know I can do this with another class - but it makes access to the parameters in the class more difficult - I'd rather have to configure the complexity once.
Please bear in mind I am new Fluent and to nHibernate.
Thanks
Using a simple class relationship such as the following:
public class Foo {
public virtual IDictionary<string, Bar> Bars { get; set; }
}
public class Bar {
public virtual string Type { get; set; }
public virtual int Value { get; set; }
}
You can map this with Fluent NHibernate in this way:
mapping.HasMany(x => x.Bars)
.AsMap(x => x.Type);
Where Bar.Type is used as the index field into the dictionary.
FluentNHibernate mapping for Dictionary