Skip to content

Commit

Permalink
Did a test with a many to many mapping between roles and privledges
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbatum committed Oct 6, 2009
1 parent 3a104e6 commit c9127e2
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 88 deletions.
Binary file modified Binaries/FluentNHibernate.dll
Binary file not shown.
Binary file modified Binaries/FluentNHibernate.pdb
Binary file not shown.
1 change: 1 addition & 0 deletions Console/Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
<ItemGroup>
<Compile Include="MappingConventions.cs" />
<Compile Include="MappingOverrides.cs" />
<Compile Include="Mappings.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Setup.cs" />
Expand Down
9 changes: 1 addition & 8 deletions Console/MappingOverrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,5 @@

namespace Console
{
public class EntityAMap : IAutoMappingOverride<TestEntityA>
{
public void Override(AutoMapping<TestEntityA> mapping)
{
mapping.HasMany(x => x.Children)
.Cascade.All();
}
}

}
39 changes: 39 additions & 0 deletions Console/Mappings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain;
using FluentNHibernate.Mapping;

namespace Console
{
public class RoleMap : ClassMap<Role>
{
public RoleMap()
{
Id(x => x.Id);
Map(x => x.Name).Not.Nullable();
Map(x => x.TypeOfContent)
.Access.CamelCaseField(Prefix.Underscore)
.CustomType<string>();
HasManyToMany(x => x.Privileges)
.Cascade.All()
.Table("RolePrivilege");
Table("roles");
}
}

public class PrivilegeMap : ClassMap<Privilege>
{
public PrivilegeMap()
{
Id(x => x.Id);
Map(x => x.Path).Not.Nullable();
HasManyToMany(x => x.RolesWithThisPrivilege)
.Cascade.All()
.Inverse()
.Table("RolePrivilege");
Table("privileges");
}
}
}
20 changes: 2 additions & 18 deletions Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FluentNHibernate.Automapping;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using Domain;
using HibernatingRhinos.NHibernate.Profiler.Appender;

namespace Console
{
Expand All @@ -18,18 +7,13 @@ class Program

static void Main(string[] args)
{
HibernatingRhinos.NHibernate.Profiler.Appender.NHibernateProfiler.Initialize();
//System.Console.ReadLine();
NHibernateProfiler.Initialize();
var sessionFactory = Setup.CreateSessionFactory();

using (var session = sessionFactory.OpenSession())
using(var tx = session.BeginTransaction())
{
var a = new TestEntityA { Name = "testA" };
var b = new TestEntityB { Comment = "commentB" };
a.AddChild(b);

session.Save(a);

tx.Commit();
}
Expand Down
22 changes: 10 additions & 12 deletions Console/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,16 @@ public static ISessionFactory CreateSessionFactory()
)
.Mappings(m =>
{
m.AutoMappings.Add(
AutoMap.AssemblyOf<TestEntityA>()
.IgnoreBase(typeof(EntityBase<>))
.UseOverridesFromAssemblyOf<Program>()
.Conventions.AddFromAssemblyOf<Program>()
//.Setup(x => x.IsComponentType = type => type == typeof(Address))
);
m.AutoMappings.First().MergeMappings = true;
m.AutoMappings.ExportTo("mappings");
//m.AutoMappings.Add(
// AutoMap.AssemblyOf<TestEntityA>()
// .IgnoreBase(typeof(EntityBase<>))
// .UseOverridesFromAssemblyOf<Program>()
// .Conventions.AddFromAssemblyOf<Program>()
// );
//m.AutoMappings.First().MergeMappings = true;
//m.AutoMappings.ExportTo("mappings");
m.FluentMappings.AddFromAssemblyOf<RoleMap>();
}


Expand Down
72 changes: 32 additions & 40 deletions Domain/Model.cs
Original file line number Diff line number Diff line change
@@ -1,68 +1,60 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace Domain
{
public class EntityBase<T> : PersistentBase

public interface IAggregateRoot
{
private T _id = default(T);

public virtual T Id
{
get
{
return _id;
}
private set
{
_id = value;
}
}
}

public abstract class PersistentBase
{}
public enum DefaultRoleNames
{
Role1,
Role2,
Role3
}

public class TestEntityA : EntityBase<int>
public class Role : IAggregateRoot
{
private string _name;
public virtual int Id { get; private set; }
public virtual DefaultRoleNames Name { get; set; }
public virtual IList<Privilege> Privileges { get; set; }

public virtual string Name
private string _typeOfContent;

public virtual Type TypeOfContent
{
get { return _name; }
set { _name = value; }
get { return Type.GetType(_typeOfContent); }
set { _typeOfContent = value.FullName; }
}

private IList<TestEntityB> _children = new List<TestEntityB>();

public virtual IEnumerable<TestEntityB> Children
public Role()
{
get { return _children; }
//protected set { _children = value.ToList(); }
Privileges = new List<Privilege>();
}

public virtual void AddChild(TestEntityB b)
public virtual void AddPrivilege(Privilege privilege)
{
_children.Add(b);
b.TestEntityA = this;
privilege.RolesWithThisPrivilege.Add(this);
Privileges.Add(privilege);
}
}

public class TestEntityB : EntityBase<int>
public class Privilege : IAggregateRoot
{
private string _comment;

public virtual string Comment
public virtual int Id { get; private set; }
public virtual string Path { get; set; }
public virtual IList<Role> RolesWithThisPrivilege
{
get { return _comment; }
set { _comment = value; }
get;
private set;
}

private TestEntityA _testEntityA = null;

public virtual TestEntityA TestEntityA
public Privilege()
{
get { return _testEntityA; }
set { _testEntityA = value; }
RolesWithThisPrivilege = new List<Role>();
}
}
}
31 changes: 21 additions & 10 deletions Tests/Class1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,46 @@
using Console;
using Domain;
using FluentNHibernate.Testing;
using NHibernate;
using NUnit.Framework;
using System.Collections;

namespace Tests
{
[TestFixture]
public class Class1
{
private ISessionFactory _sessionFactory;

[TestFixtureSetUp]
public void FixtureSetUp()
{
HibernatingRhinos.NHibernate.Profiler.Appender.NHibernateProfiler.Initialize();
_sessionFactory = Setup.CreateSessionFactory();
}

[Test]
public void CanSaveChildEntities()
{
var sessionFactory = Setup.CreateSessionFactory();

IList<TestEntityB> list = new List<TestEntityB> { new TestEntityB{Comment = "Test"}};

using (var session = sessionFactory.OpenSession())
using (var session = _sessionFactory.OpenSession())
using (var tx = session.BeginTransaction())
{
var a = new TestEntityA {Name = "testA"};
var b = new TestEntityB {Comment = "commentB"};
a.AddChild(b);
Role r;
Privilege p = new Privilege();
p.Path = "*.a";
foreach (DefaultRoleNames name in Enum.GetValues(typeof
(DefaultRoleNames)))
{
r = new Role();
r.TypeOfContent = typeof (Privilege);
r.Name = name;
r.AddPrivilege(p); ;
session.Save(r);
}

session.Save(a);

tx.Commit();
}
}

}
}

0 comments on commit c9127e2

Please sign in to comment.