Click here to Skip to main content
15,896,269 members
Home / Discussions / C#
   

C#

 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Richard Deeming14-Apr-15 9:18
mveRichard Deeming14-Apr-15 9:18 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 12:21
professionalBillWoodruff14-Apr-15 12:21 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Richard Deeming15-Apr-15 1:03
mveRichard Deeming15-Apr-15 1:03 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff15-Apr-15 2:58
professionalBillWoodruff15-Apr-15 2:58 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Sascha Lefèvre15-Apr-15 3:39
professionalSascha Lefèvre15-Apr-15 3:39 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 9:32
mvePete O'Hanlon14-Apr-15 9:32 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 12:35
professionalBillWoodruff14-Apr-15 12:35 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Pete O'Hanlon14-Apr-15 20:37
mvePete O'Hanlon14-Apr-15 20:37 
Hey Bill - glad you liked the comments. Okay, so about the use of LoadLayout and SaveLayout, passing in the underlying model. Strictly speaking, I would normally code up the model so that it really looked like this:
C#
public interface IMyModel
{
  string Name { get; set; }
  string FriendlyName { get; set; }
  DateTime LastLogin { get; set; }
}
public class MyModel : IMyModel
{
  public string Name { get; set; }
  public string FriendlyName { get; set; }
  public DateTime LastLogin { get; set; }
}
I then use IoC to create the instance of MyModel for me, and this would be passed in to the layout class like so:
C#
public class MyModelPersistence : IPersist<IMyModel>
{
  public void LoadLayout(IMyModel instance)
  {
    // Work directly with items such as item.Name
  }
 
  public void SaveLayout(IMyModel instance)
  {
    // Work directly with items such as item.Name
  }
}
Note: I didn't put this in my original example because I didn't want to confuse people into thinking that a simple interface was the same as a generic interface. When you see code like this though, you can see that it's incredibly trivial to mock the underlying code so that you can write tests like this:
C#
private IMyModel instance;
[TestInitialize]
public void Setup()
{
  instance = Mock.Create<IMyModel>();
  instance.Name = "Bob";
  instance.FriendlyName = "Extreme CP Overlord";
  Mock.Arrange(()=> instance.LastLogin).Returns(GetFixedDateTimeForTest());
}

[Test]
public void WhenValidModelPassedIn_ToLoadLayout_NameIsSet()
{
  MyModelPersistence persistence = new MyModelPersistence();
  persistence.LoadLayout(instance);
  Assert.AreEqual(instance.Name);
}
There you can see that we are creating a mocked version of IMyModel that we can use to test how our code works. For such a trivial object, I normally wouldn't bother with this level of mocking and would use the class itself, but if IMyModel was more complex in behaviour then this can become a necessity.

So, why am I not returning an instance of IMyModel from the LoadLayout? It's not the responsibility of persistence to instantiate the object. In other words, we would be violating SRP by doing this - instead, let's use IoC to instantiate our instance elsewhere and pass this in to the persistence method for hydration of the properties. That way, we are keeping a clear delineation between what each layer of code should be doing. The only thing you have to watch out for here is that you need to be very clear about the lifetime of your objects, and you will end up with lots of small classes, each doing one thing. This isn't a bad thing, but you need to be really, really clear about how you name things to make your life easy later on. In other words, try to match your interface name to your class name wherever possible.

You're right that it's not, strictly speaking, loading the properties, but the intent behind my example was to show that we were loading the layout for the model.

A final note - the sad thing is that I have typed my examples entirely here in the CP editor. When you take this approach to breaking your code up like this for a while, it becomes second nature and you find that it's easy to type up even into Notepad.
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Eddy Vluggen14-Apr-15 10:16
professionalEddy Vluggen14-Apr-15 10:16 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff14-Apr-15 12:41
professionalBillWoodruff14-Apr-15 12:41 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Eddy Vluggen15-Apr-15 0:24
professionalEddy Vluggen15-Apr-15 0:24 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
BillWoodruff15-Apr-15 3:08
professionalBillWoodruff15-Apr-15 3:08 
GeneralRe: Generic interface and generic delegate in simple terms. Pin
Eddy Vluggen15-Apr-15 3:16
professionalEddy Vluggen15-Apr-15 3:16 
QuestionHow do I get images without using picturebox? Pin
Member 1138819913-Apr-15 22:29
Member 1138819913-Apr-15 22:29 
AnswerRe: How do I get images without using picturebox? Pin
Richard MacCutchan13-Apr-15 23:25
mveRichard MacCutchan13-Apr-15 23:25 
GeneralRe: How do I get images without using picturebox? Pin
Member 1138819913-Apr-15 23:59
Member 1138819913-Apr-15 23:59 
GeneralRe: How do I get images without using picturebox? Pin
Richard MacCutchan14-Apr-15 0:02
mveRichard MacCutchan14-Apr-15 0:02 
GeneralRe: How do I get images without using picturebox? Pin
Member 1138819914-Apr-15 0:07
Member 1138819914-Apr-15 0:07 
GeneralRe: How do I get images without using picturebox? Pin
Richard MacCutchan14-Apr-15 0:16
mveRichard MacCutchan14-Apr-15 0:16 
GeneralRe: How do I get images without using picturebox? Pin
Member 1138819914-Apr-15 0:22
Member 1138819914-Apr-15 0:22 
GeneralRe: How do I get images without using picturebox? Pin
Richard MacCutchan14-Apr-15 5:30
mveRichard MacCutchan14-Apr-15 5:30 
AnswerRe: How do I get images without using picturebox? Pin
V.13-Apr-15 23:55
professionalV.13-Apr-15 23:55 
AnswerRe: How do I get images without using picturebox? Pin
OriginalGriff14-Apr-15 0:27
mveOriginalGriff14-Apr-15 0:27 
AnswerRe: How do I get images without using picturebox? Pin
Dave Kreskowiak14-Apr-15 1:16
mveDave Kreskowiak14-Apr-15 1:16 
GeneralRe: How do I get images without using picturebox? Pin
OriginalGriff14-Apr-15 4:35
mveOriginalGriff14-Apr-15 4:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.