Interface query

Interface query

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


John.sharma posted on Monday, July 13, 2009

Hi,

i am sending this piece of code which is uable to understand

public interface IUndoableObject : IBusinessObject

{

void CopyState();

void UndoChanges();

void AcceptChanges();

}

here are the implmentation of interface method in this UndoableBase class

[Serializable()]

public abstract class UndoableBase : Csla.Core.BindableBase,

Csla.Core.IUndoableObject

{

 

[EditorBrowsable(EditorBrowsableState.Never)]

protected internal void CopyState()       

// why did you write protected internal with copystate function while it is by defalt public in interface

can anyone explain it.

{

Type currentType = this.GetType();

HybridDictionary state = new HybridDictionary();

FieldInfo[] fields;

string fieldName;

do

{

// get the list of fields in this type

fields = currentType.GetFields(

BindingFlags.NonPublic |

BindingFlags.Instance |

BindingFlags.Public);

foreach (FieldInfo field in fields)

{

// make sure we process only our variables

if (field.DeclaringType == currentType)

{

// see if this field is marked as not undoable

if (!NotUndoableField(field))

{

// the field is undoable, so it needs to be processed.

object value = field.GetValue(this);

if (typeof(

Csla.Core.IUndoableObject).IsAssignableFrom(

field.FieldType))

{

// make sure the variable has a value

if (value != null)

{

// this is a child object, cascade the call

((Core.IEditableObject)value).CopyState();

}

}

else

{

// this is a normal field, simply trap the value

fieldName =

field.DeclaringType.Name + "!" + field.Name;

state.Add(fieldName, value);

}

}

}

}

currentType = currentType.BaseType;

} while (currentType != typeof(UndoableBase));

// serialize the state and stack it

using (MemoryStream buffer = new MemoryStream())

{

BinaryFormatter formatter = new BinaryFormatter();

formatter.Serialize(buffer, state);

_stateStack.Push(buffer.ToArray());

}

CopyStateComplete();

}

}

Issue :

 why did you write protected internal with copystate function while it is by defalt public in interface

can anyone explain it.

ayubu replied on Monday, July 13, 2009

The use of public interface makes it visible for any class implementing the interface and it important

The implementation of the the methods are made protected so that the might not be directly visible to the UI developer without casting to the interface. The methods are not designed to be called directly by UI code. 

Regards

Copyright (c) Marimer LLC