I am a big believer in coding to interfaces. So I would like to make interfaces for all of my business objects.
For example if I have the following interfaces:
public interface IFoo : IEditableBusinessObject { Guid Id { get; } string Name { get; set; }}
interface
IFoos : IEditableCollection { IFoo Add(); IFoo GetFooById(Guid p_id); void Remove(Guid p_id); Foos Save();}
Based on these two interfaces I declare the following classes:
public class Foo : BusinessBase<Foo>, IFoo{} public class Foos : BusinessListBase<Foos, IFoo>, IFoos {}The problem is that I want to create an editable collection of IFoo's but the methods NewFoo, GetFoo, DeleteSelf, Insert, and Update are not defined in the IFoo interface. Without out making these methods public so that they can be included in the interface, it is not possible for the editable collection of IFoo's to invoke any of these methods it's IFoo childern.
Does anyone know of way to make a editable collection of an interface without out exposing these methods to the world.
Thanks for any ideas.
rgoggin53
This is what I did:
internal interface IEditableChildObject
{
void DeleteSelf();
void Insert(Guid parentId);
void Update();
}
internal static IFoo GetFoo(DataRow row)
{
if (row["FooType_Id"] == FooType.A)
return TypeAFoo.GetTypeAFoo(row);
etc...
}
}
Since the interface is internal you'll have to implement the methods in Foo like this:
void
IEditableChildObject.Update()And you'll have to do some casting in the collection:
foreach (IFoo foo in this)I think this accomplishes what you're after.
Jeff,
Thanks for the response. I believe what you did works except for the case of overriding AddNewCore(). Since AddNewCore does not take any parameters there is no way to know which concrete type of IFoo to create.
On a side note:
internal static IFoo NewFoo(FooType fooType)
{
case Type A:
return TypeAFoo.NewTypeAFoo();
etc...
}
I think the previous code could be replaced with the following and eliminate the need for the case statement.
internal static IFoo NewFoo(Type fooType){
return (IFoo)Activator.CreateInstance(fooType);}
Copyright (c) Marimer LLC