Hi there,
Lets say I have a generated entity class which is mapped to a table and I add BL code to the class that operates on the data from an instance of that class. Often what happens is that I'll bring similar data into my app via a view but of course the view doesn't have the code on it to operate on the similar data unless I duplicate the code. Which of course I don't want to do!
So for example here is an entity class that I've add some business logic to - its a method called 'IsBig' which operates on a generated property which maps to a field in a db table.
public partial class MyEntity
{
public bool IsBig
{
get { return MyProperty > 5; }
}
}
Now lets suppose I create a view which happens to select the MyProperty field from the MyEntity table. This view is mapped to another new entity class called 'VwMyEntity'. The question is, how do I go about calling 'IsBig' on VwMyEntity without duplicating the code?
I can see a few options:
- Don't use views and only ever have one property in one's solution which maps to a db field.
- Link VwMyEntity to MyEntity in LLBLGen designer and prefetch a MyEntity instance for every VwMyEntity returned and defer all BL logic to the MyEntity instance.
- Factor out an interface and have both MyEntity and VwMyEntity implement it and have them both delegate their implementation to a third class that one passes the instance data to to be operated upon.
So the above example would look something like this:
public interface IMyEntity
{
bool IsBig { get; }
}
public class IMyEntityImp
{
public bool IsBig(int i)
{
return i > 5;
}
}
public partial class MyEntity : IMyEntity
{
public bool IsBig
{
get
{
var obj = new IMyEntityImp();
return obj.IsBig(MyProperty);
}
}
}
public partial class VwMyEntity : IMyEntity
{
public bool IsBig
{
get
{
var obj = new IMyEntityImp();
return obj.IsBig(MyProperty);
}
}
}
None of these seem very satisfying. Any ideas? Surely I'm not the only one that's come up against this.
Ian.