Map List<Int32> using Fluent Nhibernate - nhibernate

I need to map List<Int32> using Fluent Nhibernate.
Sample code:
public class ReportRequest
{
public List<Int32> EntityIds
{
get { return entityIds; }
set { entityIds = value; }
}
}
Please guide.
Thank you!

I have implemented it as :
public class ReportRequestMap : ClassMap<ReportRequest>
{
public ReportRequestMap()
{
Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
HasMany(x => x.EntityIds).Table("ReportEntities").KeyColumn("ReportRequestId").Element("EntityId").AsBag();
}
}

Related

NInject kernel GetAll returns empty

I've two projects (class library projects) which implement one interface:
The first one:
public class MailPlugin : Extensibility.IProductorPlugin
{
...
}
The second one:
public class FileSystemPlugin : Extensibility.IProductorPlugin
{
...
}
Extensibility.IProductorPlugin, is a interface of a third project:
namespace Extensibility
{
public delegate void NotifyDigitalInputs(List<Domain.DigitalInput> digital_inputs);
public interface IProductorPlugin
{
String Name { get; }
String Description { get; }
String Version { get; }
List<Domain.Channel> AvailableChannels { get; }
IEnumerable<Guid> TypeGuids { get; }
event NotifyDigitalInputs OnDigitalInputs;
}
}
In my composition root, I've created this class:
namespace UI
{
public sealed class NinjectServiceLocator
{
private static readonly Lazy<NinjectServiceLocator> lazy = new Lazy<NinjectServiceLocator>(() => new NinjectServiceLocator());
public static NinjectServiceLocator Instance { get { return lazy.Value; } }
public Ninject.IKernel Kernel { get; private set; }
private NinjectServiceLocator()
{
using (var k = this.Kernel = new Ninject.StandardKernel())
{
k.Bind(b => b.FromAssembliesMatching("*")
.SelectAllClasses()
.InheritedFrom(typeof(Extensibility.IProductorPlugin))
.BindAllInterfaces()
);
}
}
}
}
So, when I want to look for all plugins, I just perform this:
protected void initialize()
{
foreach (Extensibility.IProductorPlugin productor_plugin in NinjectServiceLocator.Instance.Kernel.GetAll(typeof(Extensibility.IProductorPlugin)))
{
using (var channel_tile = new DevExpress.XtraBars.Docking2010.Views.WindowsUI.Tile() { Group = "Plugin Channels" })
{
foreach (Domain.Channel channel in productor_plugin.AvailableChannels)
{
channel_tile.Elements.Add(new DevExpress.XtraEditors.TileItemElement() { Text = channel.Name });
channel_tile.Elements.Add(new DevExpress.XtraEditors.TileItemElement() { Text = channel.Description });
this.tileContainer1.Items.Add(channel_tile);
}
}
}
}
However, GetAll returns anything.
What am I doing wrong?
I'll appreciate a lot your help.
Thanks for all.
try removing the using() from around the Kernel instantiation. a using will dispose the object at the end of the scope, which we don't want for a kernel.
using (var k = this.Kernel = new Ninject.StandardKernel())

Error using Autofac.Extras.NHibernate

I'm trying to inject dependencies im my models that NHibernate creates.
What I'm trying to do is the same here: http://fabiomaulo.blogspot.com.br/2008/11/entities-behavior-injection.html
But my container is Autofac.
So, I've found https://www.nuget.org/packages/Autofac.Extras.NHibernate/
I saw the post http://chadly.net/2009/05/dependency-injection-with-nhibernate-and-autofac/ that I've think is the origin of the Autofac.Extras.NHibernate.
My problem is that the code in Autofac.Extras.NHibernate and described in Chad post are different.
Looking at the source code I (think) figured out how to set the BytecodeProvider using:
Cfg.Environment.BytecodeProvider = new AutofacBytecodeProvider(Container, new DefaultProxyFactoryFactory(), new DefaultCollectionTypeFactory());
But now, I'm getting an exception when I tried to retrieve data from database:
[PropertyAccessException: could not set a property value by reflection setter of NHibernate.Autofac2.App_Start.Model.User.Id]
If I comment the line where I set BytecodeProvider the code works.
I created a POC to simulate:
My model:
public class User
{
private readonly ISomeService _someService;
public User(ISomeService someService)
{
this._someService = someService;
}
public virtual long Id { get; set; }
public virtual string Name { get; set; }
public virtual string GetTranslate
{
get { return this._someService != null ? this._someService.T(this.Name) : " No Translate" + this.Name; }
}
}
My mapping:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.Id);
Map(x => x.Name)
.Length(16)
.Not.Nullable();
}
}
Creation of the Autofac container and SessionFactory using Fluent Nhibernate:
// Create your builder.
var builder = new ContainerBuilder();
builder.RegisterType<SomeService>().As<ISomeService>();
builder.RegisterType<User>().As<IUser>();
Container = builder.Build();
SessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString("Data Source=(local);Initial Catalog=NHibernate.Autofac;User ID=test;Password=102030;Pooling=True"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MvcApplication>())
.ExposeConfiguration(config => config.Properties.Add("use_proxy_validator", "false"))
.ExposeConfiguration(config =>
{
//config.Properties.Add("proxyfactory.factory_class", "");
Cfg.Environment.BytecodeProvider = new AutofacBytecodeProvider(Container, new DefaultProxyFactoryFactory(), new DefaultCollectionTypeFactory());
new SchemaExport(config).Drop(false, false);
new SchemaExport(config).Create(false, true);
})
.BuildSessionFactory();
Well, I've found a solution that works for me.
Now, I'm using NHibernate.DependencyInjection.
The IEntityInjector implemenation:
public class EntityInjector : IEntityInjector
{
private readonly IContainer _container;
public EntityInjector(IContainer container)
{
_container = container;
}
public object[] GetConstructorParameters(System.Type type)
{
var constructor = type.GetConstructors().FirstOrDefault();
if (constructor != null)
return constructor.GetParameters().Select(a => a.ParameterType).Select(b => this._container.Resolve(b)).ToArray();
return null;
}
}
And in Global.asax:
Initializer.RegisterBytecodeProvider(new EntityInjector(Container));
SessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2005.ConnectionString("Data Source=(local);Initial Catalog=NHibernate.Autofac;User ID=XXX;Password=XXXX;Pooling=True"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<MvcApplication>())
.ExposeConfiguration(config => config.Properties.Add("use_proxy_validator", "false"))
.ExposeConfiguration(config =>
{
new SchemaExport(config).Drop(false, false);
new SchemaExport(config).Create(false, true);
})
.BuildSessionFactory();

Automapping a Composite Model with Composite Iteration with FluentNhibernate

I have a tree structured model and designed it with composite Pattern. for iterating through the entire hierachy Im using Composite Iteration.
I have used this tutorial:
http://www.blackwasp.co.uk/Composite.aspx
but when I want to AutoMap the model, I encounter this problem:
{"The entity '<GetEnumerator>d__0' doesn't have an Id mapped. Use the
Id method to map your identity property. For example: Id(x => x.Id)."}
but getEnumerator is a method. I don't know why handle this like an Entity!!
public IEnumerator<MenuComponent> GetEnumerator()
{
foreach (MenuComponent child in menuComponents)
yield return this;
}
here is my AutoMapping Configuration :
public class AutomappingConfiguration: DefaultAutomappingConfiguration
{
//As we do not explicitly map entities or value objects, we define conventions or exceptions
//for the AutoMapper. We do this by implementing a configuration class.
//this method instructs the AutoMapper to consider only those classes for mapping
//which reside in the same namespace as the Employeeentity.
public override bool ShouldMap(Type type)
{
return type.Namespace == typeof(Menu).Namespace;
}
}
Uploaded the sample code:
public abstract class CombatElement
{
public virtual string Name { get; set; }
public virtual Guid Id { get; set; }
public virtual void Add(
CombatElement element)
{
throw new NotImplementedException();
}
public virtual void
Remove(CombatElement element)
{
throw new NotImplementedException();
}
public virtual
IEnumerable<CombatElement>
GetElements()
{
throw new NotImplementedException();
}
public abstract void Fight();
public abstract void Move();
}
//////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;
namespace FluentNHibernateMvc3.Models
{
public class Formation : CombatElement
{
private List<CombatElement> _elements;
public virtual IEnumerable<CombatElement> Elements { get { return _elements; } }
public Formation()
{
_elements = new List<CombatElement>();
}
public override void Add(
CombatElement element)
{
_elements.Add(element);
}
public override void
Remove(CombatElement element)
{
_elements.Remove(element);
}
public override void Fight()
{
Debug.WriteLine(this.Name + " Formation is fighting");
}
public override void Move()
{
Debug.WriteLine(this.Name + " Formation is moving");
}
public override
IEnumerable<CombatElement>
GetElements()
{
// yield up this current object first
yield return this;
// iterate through all child elements
foreach (CombatElement fe in
_elements)
{
// + iterate through each of its elements
foreach (CombatElement feInner
in fe.GetElements())
yield return feInner;
}
}
}
}
/////////
public class Soldier : CombatElement
{
public virtual int Rank { get; set; }
public override void Fight()
{
Debug.WriteLine(this.Name + " soldier is fighting");
}
public override void Move()
{
Debug.WriteLine(this.Name + " soldier is fighting");
}
public override
IEnumerable<CombatElement>
GetElements()
{
yield return this;
}
}
and here how I create session factory
// Returns our session factory
private static ISessionFactory CreateSessionFactory()
{
//m => m.FluentMappings.AddFromAssemblyOf<FormationMap>()
return Fluently.Configure()
.Database( CreateDbConfig )
.Mappings(m => m.AutoMappings.Add(CreateMappings()))
.ExposeConfiguration( UpdateSchema )
.CurrentSessionContext<WebSessionContext>()
.BuildSessionFactory();
}
// Returns our database configuration
private static MsSqlConfiguration CreateDbConfig()
{
return MsSqlConfiguration
.MsSql2008
.ConnectionString( c => c.FromConnectionStringWithKey( "testConn" ) );
}
// Returns our mappings
private static AutoPersistenceModel CreateMappings()
{
var cfg = new AutomappingConfiguration();
return AutoMap
.Assemblies(cfg,System.Reflection.Assembly.GetCallingAssembly()).IncludeBase<CombatElement>()
.Conventions.Setup( c => c.Add( DefaultCascade.SaveUpdate() ) );
}
// Updates the database schema if there are any changes to the model,
// or drops and creates it if it doesn't exist
private static void UpdateSchema( Configuration cfg )
{
new SchemaUpdate( cfg )
.Execute( false, true );
}
Does anyone has any idea?

Overriding FluentNHibernate DefaultCascade for many types at once

I have an abstract entity base class defined like this:
public abstract class SessionItem : Entity
{
public virtual Session Session { get; set; }
}
In addition, I'm using auto mapping:
private AutoPersistenceModel CreateAutomappings()
{
return AutoMap
// configuration
.Conventions.Add(DefaultCascade.All())
// more configuration
}
SessionItem has several derived classes/tables, and I'd like to override the cascading policy for all of them. I tried the following:
public class SessionItemAutommapingOverride : IAutoMappingOverride<SessionItem>
{
public void Override(AutoMapping<SessionItem> mapping)
{
mapping.References(x => x.Session).Cascade.None();
}
}
But unfortunately the override is not called since SessionItem is abstract (and is not mapped). I prefer to avoid overriding it for each subclass (using IAutoMappingOverride).
Is there any way to override cascading for multiple types, without using IAutoMappingOverride<> for each one?
public class SessionReferenceCascadeNone : IReferenceConvention, IReferenceConventionAcceptance
{
public void Accept(IAcceptanceCriteria<IManyToOneInspector> criteria)
{
criteria.Expect(x =>
typeof(SessionItem).IsAssignableFrom(x.EntityType) &&
x.Property.PropertyType == typeof(Session));
}
public void Apply(IManyToOneInstance instance)
{
instance.Cascade.None();
}
}
Apparently this is possible by using IReferenceConvention:
public class CascadeNoneOverrideConvention : IReferenceConvention
{
public void Apply(IManyToOneInstance instance)
{
// override
}
}

nhibernate 3.0 optimistic lock and related entities

I've noticed in my database (SQL Server) that the column I'm using for versioning is getting updated for an entity whenever that entity is added to a related table.
So, for example, if I have a Person entity and I add a Person to a Club, the Person's record in the database will have a new version. Of course, in the database, it's the Club's record that is added/updated with the Person's primary key.
I'm a bit surprised by that behavior because no other data for the Person entity is changing.
My question is if there is a way to turn that off; that is, to only have the version column updated when data in that row changes. What might the implications of that kind of configuration be?
EDIT Here's the real code
Model
public partial class ObligationProgramFund : IMyObject
{
private System.Int32 _id;
// ... many other properties
private IList<ObligationProgramFundOrganization> _obligationPFOrganizations;
private System.Byte[] _recordVersion;
public ObligationProgramFund() : base()
{
_obligationPFOrganizations = new List<ObligationProgramFundOrganization>();
}
public virtual System.Int32 Id
{
get { return _id; }
set { _id = value; }
}
public virtual IList<ObligationProgramFundOrganization> ObligationPFOrganizations
{
get { return _obligationPFOrganizations; }
set { _obligationPFOrganizations = value; }
}
public virtual System.Byte[] RecordVersion
{
get { return _recordVersion; }
set { _recordVersion = value; }
}
}
public partial class ObligationProgramFundOrganization : IMyObject
{
private ObligationProgramFund _obligationProgramFund;
private System.Int32 _id;
private Organization _organization;
private System.Byte[] _recordVersion;
// .. other properties
public ObligationProgramFundOrganization() : base()
{
}
public virtual System.Int32 Id
{
get { return _id; }
set { _id = value; }
}
public virtual System.Byte[] RecordVersion
{
get { return _recordVersion; }
set { _recordVersion = value; }
}
public virtual ObligationProgramFund ObligationProgramFund
{
get { return _obligationProgramFund; }
set { _obligationProgramFund = value; }
}
public virtual Organization Organization
{
get { return _organization; }
set { _organization = value; }
}
}
public partial class Organization : IMyObject
{
private IList<ObligationProgramFundOrganization> _obligationPFOrganizations;
private System.Int32 _id;
private System.Byte[] _recordVersion;
// other properties
public Organization() : base()
{
_obligationPFOrganizations = new List<ObligationProgramFundOrganization>();
}
public virtual System.Int32 Id
{
get { return _id; }
set { _id = value; }
}
public virtual System.Byte[] RecordVersion
{
get { return _recordVersion; }
set { _recordVersion = value; }
}
public virtual IList<ObligationProgramFundOrganization> ObligationPFOrganizations
{
get { return _obligationPFOrganizations; }
set { _obligationPFOrganizations = value; }
}
}
Mapping
public partial class ObligationProgramFundMap : ClassMap<ObligationProgramFund>
{
public ObligationProgramFundMap()
{
Table("[MySchema2].[dbo].[ObligationProgramFund]");
OptimisticLock.Version();
DynamicUpdate();
LazyLoad();
Id(x=>x.Id)
.Access.CamelCaseField(Prefix.Underscore)
.Column("[Id]")
.GeneratedBy.Identity();
Version(x=>x.RecordVersion)
.Access.CamelCaseField(Prefix.Underscore)
.Column("[RecordVersion]")
.CustomSqlType("timestamp")
.Not.Nullable()
.UnsavedValue("null")
.CustomType("BinaryBlob")
.Generated.Always();
// other properties/components
HasMany(x=>x.ObligationPFOrganizations)
.Access.CamelCaseField(Prefix.Underscore)
.Cascade.AllDeleteOrphan()
.Fetch.Select()
.Inverse()
.LazyLoad()
.KeyColumns.Add("ObligationProgramFundId");
}
}
public partial class ObligationProgramFundOrganizationMap : ClassMap<ObligationProgramFundOrganization>
{
public ObligationProgramFundOrganizationMap()
{
Table("[MySchema2].[dbo].[ObligationProgramFundOrganization]");
OptimisticLock.Version();
DynamicUpdate();
LazyLoad();
Id(x=>x.Id)
.Access.CamelCaseField(Prefix.Underscore)
.Column("[Id]")
.GeneratedBy.Identity();
Version(x=>x.RecordVersion)
.Access.CamelCaseField(Prefix.Underscore)
.Column("[RecordVersion]")
.CustomSqlType("timestamp")
.Not.Nullable()
.UnsavedValue("null")
.CustomType("BinaryBlob")
.Generated.Always();
// other properties
References(x=>x.ObligationProgramFund)
.Access.CamelCaseField(Prefix.Underscore)
.Cascade.SaveUpdate()
.Fetch.Select()
.Columns("ObligationProgramFundId");
References(x=>x.Organization)
.Access.CamelCaseField(Prefix.Underscore)
.Cascade.SaveUpdate()
.Fetch.Select()
.Columns("OrganizationId");
}
}
public partial class OrganizationMap : ClassMap<Organization>
{
public OrganizationMap()
{
Table("[MySchema2].[dbo].[Organization]");
OptimisticLock.Version();
DynamicUpdate();
LazyLoad();
Id(x=>x.Id)
.Access.CamelCaseField(Prefix.Underscore)
.Column("[Id]")
.GeneratedBy.Identity();
Version(x=>x.RecordVersion)
.Access.CamelCaseField(Prefix.Underscore)
.Column("[RecordVersion]")
.CustomSqlType("timestamp")
.Not.Nullable()
.UnsavedValue("null")
.CustomType("BinaryBlob")
.Generated.Always();
// other properties
HasMany(x=>x.ObligationPFOrganizations)
.Access.CamelCaseField(Prefix.Underscore)
.Cascade.AllDeleteOrphan()
.Fetch.Select()
.Inverse()
.LazyLoad()
.KeyColumns.Add("OrganizationId");
}
}
So my entities are
Organization 1 - ∞ ObligationProgramFundOrganization ∞ - 1 ObligationProgramFund
The issue I've observed is when a new ObligationProgramFundOrganization is created (the associative table), the record version for ObligationProgramFund is updated.
The actual senario is that I have a table of ObligationProgramFundOrganizations on the ObligationProgramFund edit form. A ObligationProgramFundOrganization is added via AJAX calls. When I go to then save the ObligationProgramFund, I get a concurrency exception. I am sure no other user actually edited the ObligationProgramFund as it's all running locally.
(I've posted an answer to a similar question: Why NHibernate UPDATE reference entity?)
You have version column specified. Which means that any property change (even collection) trigger version update.
In order to prevent a certain property/collection to change version, optimistic-lock="false" property should be set in xml mapping.