Using interfaces with CSLA

Using interfaces with CSLA

Old forum URL: forums.lhotka.net/forums/t/2456.aspx


rgoggin53 posted on Thursday, March 01, 2007

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

jeff replied on Thursday, March 01, 2007

This is what I did:

internal interface IEditableChildObject
{
   void
DeleteSelf();
   void Insert(Guid
parentId);
   void
Update();
}

internal static class FooFactory
{
   internal static IFoo NewFoo(FooType fooType)
   {
      case Type A:
         return TypeAFoo.NewTypeAFoo();
      etc...
   }

   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)
{
   if (foo.IsNew)
      ((IEditableChildObject)foo).Insert(fooParent.Id);
   else
      ((IEditableChildObject)foo).Update();
}

I think this accomplishes what you're after.

 

Jimbo replied on Friday, March 02, 2007

Whats is that in vb speak?
After all csla is multi lingual.



rgoggin53 replied on Friday, March 02, 2007

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);

}

boo replied on Friday, March 02, 2007

So I hate to ask, but why do you believe that you should code to interfaces?

What are you gaining by doing so?  Are you going to suddenly swap out on the fly another business object that has the same exact signature?

Copyright (c) Marimer LLC