FluentMappings.Add and FilterDefinitions

491 views
Skip to first unread message

Mira.D

unread,
Mar 14, 2010, 8:27:15 PM3/14/10
to Fluent NHibernate
Hi,

Just like I told in another post (http://groups.google.com/group/
fluent-nhibernate/t/9eb53f6447e9771a) I use to set some dynamic
filters on my entities. Actually I have to do a very ugly workaround
so I can acomplish that, for the definitions I have to emit code to
make the FilterDefinitions inherited classes and for the mapping
classes I have to set a static property with the filters witch it has
to Apply (when just it needs is the filter name). I think it should be
very better if you could add two more overloads in the
ClassMap<T>.ApplyFilter method, those should be:

public ClassMap<T> ApplyFilter(string name)
{
return this.ApplyFilter(name, null);
}
public ClassMap<T> ApplyFilter(string name, string condition)
{
var part = new FilterPart(new TFilter().Name, condition);
filters.Add(part);
return this;
}

And the current one could be changed to:

public ClassMap<T> ApplyFilter<TFilter>(string condition)
where TFilter : FilterDefinition, new()
{
return this.ApplyFilter(new TFilter().Name, condition);
}

That should solve part of my problem, second I would have the
FluentMappingsContainer.Add restricting me to add just types and
forcing me to emit code for the filter definitions as, also, set
static properties of my entities mapping classes so they know witch
filters should they apply when I could just instantiated then, set the
filters on then an pass this instance to the
FluentMappingsContainer.Add.

Would be something like:

public class FluentMappingsContainer
{
private readonly IList<IMappingProvider> mappings = new
List<IMappingProvider>();
private readonly IList<IFilterDefinition> filters = new
List<IFilterDefinition>();
...
public FluentMappingsContainer Add(IMappingProvider mapping)
{
mappings.Add(mapping);
WasUsed = true;
return this;
}
public FluentMappingsContainer Add(IFilterDefinition filter)
{
filters.Add(filters);
WasUsed = true;
return this;
}
internal void Apply(Configuration cfg)
{
foreach (var mapping in mappings)
{
model.Add(mapping);
}

foreach (var filter in filters)
{
model.Add(filter);
}

foreach (var assembly in assemblies)
{
model.AddMappingsFromAssembly(assembly);
}

foreach (var type in types)
{
model.Add(type);
}

if (!string.IsNullOrEmpty(exportPath))
model.WriteMappingsTo(exportPath);

model.Configure(cfg);
}

I have posted this last year (http://groups.google.com/group/fluent-
nhibernate/browse_thread/thread/fdc23fd8197d9bbc/
743077793216c027#743077793216c027) but got nothing, to my happiness I
haven't to change fluentnh code that time as in that case was for
dicriminating classes witch have it's behavior changed to use
SubclassMap<T>. I'm not posting a real case because the code is long
and uggly and I don't see that those overloads should be a major
change in the FluentNHibernate structure (enlighten me if I'm wrong),
all those code have been tested here and worked fine I just don't use
them because if I make a change on FluentNHibernate I'll have to peek
it on every new version I download, too messy, error prone,
whatever...

Best Regards,
DM

snicker

unread,
Mar 15, 2010, 12:10:31 PM3/15/10
to Fluent NHibernate
I'm quite confused. Your suggestions exist in the latest revisions of
the repository. If you check out the repo and build from source you'll
have exactly what you wish for.

Daniel Mirapalheta

unread,
Mar 15, 2010, 1:00:09 PM3/15/10
to fluent-n...@googlegroups.com
I just checked both classes (in the following links) and nothing of that have been added.

http://github.com/jagregory/fluent-nhibernate/blob/master/src/FluentNHibernate/Cfg/FluentMappingsContainer.cs
http://github.com/jagregory/fluent-nhibernate/blob/master/src/FluentNHibernate/Mapping/ClassMap.cs

Regards,
DM

2010/3/15 snicker <ngord...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.




--
Daniel Mirapalheta

snicker

unread,
Mar 15, 2010, 4:58:51 PM3/15/10
to Fluent NHibernate
Correct. What I meant to say was "Why do you need this?"

You mention above that you Apply it just by the name. Why? The
FilterDefinition classes are hardly more than a few properties and
methods for generating some mapping information. It's not going to
hurt you particularly badly if you just use the methods already
existing. The code you provided wouldn't compile anyway, I believe in
your second method you meant "new FilterPart(name, condition);"
TFilter is not defined.

Your proposed changes to FluentMappingsContainer are TOTALLY
superfluous. You've added additional overloads for the Add method
taking instances rather than types. Instead of
FluentMappingsContainer.Add(), you can call the PersistenceModel
directly for this purpose:
FluentMappingsContainerInstance.PersistenceModel.Add(IFilterDefinition
or IMappingProvider). If you need to set the WasUsed property on the
mappings container, either pass in an dummy empty mapping class or use
reflection to modify it. Here's an extension method I use:

public static void BecomeUsed(this FluentMappingsContainer m)
{
PropertyInfo pi = m.GetType().GetProperty("WasUsed",
BindingFlags.NonPublic | BindingFlags.Instance);
if (pi != null)
pi.SetValue(m, true, null);
}

On Mar 15, 10:00 am, Daniel Mirapalheta <mirapalh...@gmail.com> wrote:
> I just checked both classes (in the following links) and nothing of that
> have been added.
>

> http://github.com/jagregory/fluent-nhibernate/blob/master/src/FluentN...http://github.com/jagregory/fluent-nhibernate/blob/master/src/FluentN...
>
> Regards,
> DM
>
> 2010/3/15 snicker <ngordon...@gmail.com>

> > fluent-nhibern...@googlegroups.com<fluent-nhibernate%2Bunsu...@googlegroups.com>

Daniel Mirapalheta

unread,
Mar 16, 2010, 8:04:08 AM3/16/10
to fluent-n...@googlegroups.com
  About  the proposed changes to FluentMappingsContainter being totally superflous you are right, I hadn't realized that the model field is being returned in the public property PersistentModel and no, I don't need to set the wasused property (neither I don't see why I would need it).

  Actually I have told why I need that. I have data restriction definitions stored in the database witch I'll set as filters in the initialization and I don't have as classes to be passed to my entities mapping classes so them can't apply those filters. Sorry, "new TFilter().Name" was a bad copy and paste, it was suposed to be just "name" (the parameter). You told me that it won't hurt me to use the already existing methods, take a look:

class DynamicFilter: FilterDefinition
{
  public DynamicFilter() { } // FilterDefinition's constructor is protected, this allow me to create and set a custom filter (runtime)
}
class DummyFilter: FilterDefinition
{
  public DummyFilter() { base.WithName(DummyFilter.DummyName); }
  static string DymmyName;
}
class FooMap: ClassMap<Foo>
{
  public FooMap()
  {
    ... mapping stuff ...
  }
}

foreach (DbFilterDef dbf in filterDefinitions) // supposed list I got from my database
{
  DynamicFilter filter = new DynamicFilter()
  filter.WithName(dbf.Name);
  ... set other filter infos ...
  cfg.Mappings(x => x.FluentMappings.PersistentModel.Add(filter);

  FooMap foo = new FooMap();
  DummyFilter.DummyName = filter.Name;
  foo.ApplyFilter<DummyFilter>();
}

  This kind of ugly code hurts me and I believe that should hurt every programmer who reads it... well, this is just a scratch I wrote to explain the kind of workaround I have to do so I can setup those filters, yes it may not compile (I just wrote it here in this e-mail body), yes it seems silly, but in the whole context it has a reason to exists (user defined data restrictions witch I set as filters in the initialization).

  Regards,
  DM

2010/3/15 snicker <ngord...@gmail.com>
Correct. What I meant to say was "Why do you need this?"
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.




--
Daniel Mirapalheta

James Gregory

unread,
Mar 16, 2010, 8:16:40 AM3/16/10
to fluent-n...@googlegroups.com
I'll just leave this here: contributing.

Daniel Mirapalheta

unread,
Mar 16, 2010, 8:30:39 AM3/16/10
to fluent-n...@googlegroups.com
I'll make that, thanks James (sorry I have never read that).

Regards,
DM
Reply all
Reply to author
Forward
0 new messages