views:

105

answers:

1

I have the following idea:

Business object implemented as interface or abstract class with certain properties as read only to all layers except the DAL layer. I also want my business objects in another assembly than the DAL (for testing purposes), so marking the properties is not an option for me.

Examples could be one to one relationships or other properties.

I have almost solved the issue by doing the following

abstract class User
{
    public virtual long UserId {get; protected set;}
    public virtual string Password {get; protected set;}
    ...
}

In the DAL:

public class DbUser : User
{
    internal virtual void SetPassword(string password) {...}
}

I then map this using fluent as

ClassMap<User> {...}
SubclassMap<DbUser> {...}

The problem I get is that fluent tries to create a table named DbUser.

If I skip the SubclassMap and creates a DbUser object and tries to save it I get an "No persister for this object" error.

Is it possible to solve?

A: 

You could probably override what is done with Fluent

public class DbUser: IAutoMappingOverride<DbUser>
    {
        public void Override(AutoMapping<DbUser> mapping)
        {
            //tell it to do nothing now, probably tell it not to map to table,
            // not 100% on how you'd do this here.                    
        }
    }

Or you could have an attribute

public class DoNotAutoPersistAttribute : Attribute
{
}

And in AutoPersistenceModelGenerator read for attribute in Where clause to exclude it.

Check would be something like

private static bool CheckPeristance(Type t) { 
            var attributes = t.GetCustomAttributes(typeof (DoNotAutoPersistAttribute), true); 
            Check.Ensure(attributes.Length<=1, "The number of DoNotAutoPersistAttribute can only be less than or equal to 1"); 
            if (attributes.Length == 0) 
                return false;
            var persist = attributes[0] as DoNotAutoPersistAttribute; 
            return persist == null; 
        }

Then it kind of depends how you're adding entities but you're probably adding via assembly so this might do it for you:

mappings.AddEntityAssembly(typeof(User).Assembly).Where(GetAutoMappingFilter);
....
...
private static bool GetAutoMappingFilter(Type t)
        {
            return t.GetInterfaces().Any(x => CheckPeristance(x)); //you'd probably have a few filters here
        }
dove