fluent-nhibernate
[Prev] Thread [Next] | [Prev] Date [Next]
[fluent-nhib] Re: Using Interface proxies Chris O Fri Jun 26 07:01:03 2009
Thanks.
On Jun 26, 9:41 am, Andre Loker <[EMAIL PROTECTED]> wrote:
> : Yes, that's precisely the way I work. I'm not aware of explicit support for
> this in FNH, that's why I wrote the patches I mentioned in the first place.
> I see two options:
> 1. build FNH yourself and apply the patches that I uploaded
> 2. Use some helper methods to inject the feature into FNH (that's what I've
> done before I've modified th FNH code):
> public static class MappingExtensions {
> public static IManyToOnePart EntityIs<TCastTo>(this IManyToOnePart part) {
> return SetEntityType<TCastTo, IManyToOnePart>(part);
> }
> static TMapping SetEntityType<TCastTo, TMapping>(TMapping part) where
> TMapping : IHasAttributes {
> var prop = part.GetType().GetProperty("EntityType");
> var type = typeof(TCastTo);
> prop.SetValue(part, type, null);
> part.SetAttribute("class", string.Format("{0}, {1}", type.FullName,
> type.Assembly.GetName().Name));
> return part;
> }
> /// <summary>
> /// Sets the proxy type of a class.
> /// </summary>
> /// <typeparam name="TProxy">The type of the proxy.</typeparam>
> /// <param name="mapping">The mapping.</param>
> public static void SetProxyType<TProxy>(this IClasslike mapping) {
> if(!(mapping is IHasAttributes)) {
> throw new ArgumentException("Proxy types can only be defined for class
> like mappings with attributes.");
> }
> ((IHasAttributes) mapping).SetAttribute("proxy", string.Format("{0},
> {1}", typeof(TProxy).FullName, typeof(TProxy).Assembly.GetName().Name));
> }
> }
> Usage:
> To set the proxy type of a class:
> public class PersonMapping : ClassMap<Person>{
> public PersonMapping(){
> //...
> this.SetProxyType<IPerson>();
> }
> //...
> }
> At places where you have a reference (N:1) association:
> public class SomeMapping : ClassMap<SomeClass>{
> public SomeMapping(){
> Reference(x=>x.AnIPersonReference).EntityIs<Person>();
> }
> }
> At places where you have a 1:N association:
> public class SomeOtherMapping : ClassMap<SomeOtherClass>{
> public SomeOtherMapping(){
> HasMany<Person>(x=>x.SetOfIPerson);
> }
> }Usingmy patch is probably the better approach, as you can see I had to use
> reflection to "hack" into FNH to add this functionality externally.
> If there's a better approach, I'm more than happy to hear it.
> Regards,
> AndreGood morning. Here's an example: say we have the following:
> ------------------------- database table: Table: Person{ Field: Id bigint
> Field: name varchar(50) Field: phone varchar(20) }
> ------------------------------interfaceand class: publicinterfaceIPerson {
> long Id {get;set;} string name { get; set; } string phone {get; set; } }
> public class Person : IPerson { public long Id {get;set;} public string name
> {get;set;} public string phone {get;set;} }
> ------------------------------------------------ NHibernate allows you to
> have the following mapping file: <?xml version="1.0" encoding="utf-8" ?>
> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
> namespace="MyProject.ItsNamespance" assembly="MyProject"> <class
> name="Person" table="Person" proxy="IPerson"> <id name="Id" column="Id"
> unsaved-value="0"> <generator class="identity" /> </id> <property name="name"
> column="name" type="String" /> <property name="phone" column="phone"
> type="string" /> </class> </hibernate-mapping>
> ----------------------------------------- Question: How would you map the
> above, without changing theinterfaceand/or class definition, with Fluent
> NHibernate? Is there support for this? Comments: My projects are designed
> this way. If I have to go back through and make properties virtual just so I
> can use fluent Nhibernate, that would not happen. Thanks. On Jun 25, 5:23 pm,
> Andre Loker<[EMAIL PROTECTED]>wrote:Seems that my previous reply got lost
> somehow. Anyway: a few days ago, I've submitted patches that address exactly
> this
> problem:http://code.google.com/p/fluent-nhibernate/issues/detail?id=256http://code.google.com/p/fluent-nhibernate/issues/detail?id=257I
> haven't got any reactions yet, however. I don't know whether external
> patches are appreciated or whether no one had the time yet. You can of course
> always build your own version of FNH and use the patches mentioned above.
> Hope I could help. Regards, Andre On 25 Jun., 18:18, Chris O<[EMAIL
> PROTECTED]>wrote:At the risk of sound dumb, is there support for
> usinginterfaceas a proxy? I see in the mappings where theusingthe virtual is
> explained, but there is no reference to the case ofusinganinterface as a
> proxy class for NHibernate.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Fluent NHibernate" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/fluent-nhibernate?hl=en
-~----------~----~----~----~------~----~------~--~---
- [fluent-nhib] Using Interface proxies Chris O
- [fluent-nhib] Re: Using Interface proxies Hudson Akridge
- [fluent-nhib] Re: Using Interface proxies Tuna Toksoz
- [fluent-nhib] Re: Using Interface proxies Tuna Toksoz
- [fluent-nhib] Re: Using Interface proxies Andre Loker
- [fluent-nhib] Re: Using Interface proxies Andre Loker
- [fluent-nhib] Re: Using Interface proxies Chris O
- [fluent-nhib] Re: Using Interface proxies Andre Loker
- [fluent-nhib] Re: Using Interface proxies Chris O <=