2

Here is a method that I've defined in my own library:

public override void Setup(Feed feed)
{

    SetTitle(feed.title);
    SetText(feed.text);
    SetDate(feed.CreationTime);
    SetSubTitle(feed.subtitle);
    SetLikes(feed.likes);
}

The Feed class is defined in another library that I'm not the owner of that library and also I don't want to use it in this library project (layer) to avoid the dependency; how can I create a placeholder for the Feed class in my own library so that after building the library it makes me available to inject the dependency to the method on the main application.

S.A.Parkhid
  • 2,772
  • 6
  • 28
  • 58

1 Answers1

1

It is the case where design principle program to interfaces, not implementations can be shown in action.

So let's create an abstraction of class Feed:

public interface IFeed
{
    string Title { get; }

    string Text { get; }

    string CreationTime { get; }

    string Subtitle { get; }

    string Likes { get; }
}

and this would be concrete implementation of your abstraction:

public class Feed : IFeed
{
    public string Title { get; set; }
     
    public string Text { get; set; }

    public string CreationTime { get; set; }

    public string Subtitle { get; set; }

    public string Likes { get; set; }
}

and then your method would look like this:

public override void Setup(IFeed feed)
{

    SetTitle(feed.title);
    SetText(feed.text);
    SetDate(feed.CreationTime);
    SetSubTitle(feed.subtitle);
    SetLikes(feed.likes);
}

On the other hand, it is possible to use dynamic. However, it should be avoided as it would devastate static type checking. Just for demonstrating purposes code snippet would be shown:

public override void Setup(dynamic feed)
{

    SetTitle(feed.title);
    SetText(feed.text);
    SetDate(feed.CreationTime);
    SetSubTitle(feed.subtitle);
    SetLikes(feed.likes);
}
StepUp
  • 36,391
  • 15
  • 88
  • 148
  • thanks for your reply, but your answer is not correct since the main Feed class is owned by another party and they would not implement the IFeed. if you read my question carefully, I've written about that. anyway, I've solved this issue by passing the Feed properties such as title, likes and etc. instead of the Feed class itself, so my class has no dependency on the Feed class Now, but it is about the deletion of the problem not solving it. maybe some sort of other pattern helps but I don't know how. – S.A.Parkhid Nov 15 '22 at 18:36
  • @S.A.Parkhid And you cannot edit that another party? – StepUp Nov 15 '22 at 19:48
  • No I can't edit that. – S.A.Parkhid Nov 16 '22 at 11:17