简体   繁体   中英

Net Core: Hold Multiple Class Types for Methods

I have a bunch of Class methods, which take a List of Image URLs, and place them into Tagbuilders, apply functions, etc. What is good way to accomodate flexible design, and should I use dependency injection? I only need ImageURL and Title.

History:

I conducted this initially. I am beginner and learned this is not loosely coupled well.

public void RunSomeprocess(List<string> ImageList)
{
....

Then someone came up with a Dictionary with Image Titles, and I changed into Dictionary to hold URL, and additional title

public void RunSomeprocess(Dictionary<string,string> ImageList)
{
....

Then people came up with a class, one had caption, another had length/height. The only two columns I need are ImageSource and Title. What the best way to deal with this situation?

public class ImageListwithCaption
{
    string ImageSource {get;set;}
    string ImageTitle {get;set;}
    string ImageCaptionDescription {get;set;}


public class ImageListwithLengthHeight
{
    string ImageSource {get;set;}
    string ImageTitle {get;set;}
    int PixelWidth {get;set;}
    int PixelHeight{get;set;}

Dictionary meets your requirements. You should stick with that until you need additional fields, ie width, height. At that point, you can refactor imagelist into its own model.

You can't have two classes with the same name in the same namespace.

You have a method which requires a List of Images, and these need to have a Source and a Title . Your 'people' would like to have additional but different attributes on their Image classes. The way to handle this is to define a base class Image and the 'people' can create subclasses of that:

public class Image
{
    public string Source{ get; set; }
    public string Title{ get; set; }
}
public class ImageWithCaption : Image
{
    public string CaptionDescription{ get; set; }
}
public class ImageWithSize : Image
{
    public int PixelWidth{ get; set; }
    public int PixelHeight{ get; set; }
}

Now your method can accept an IEnumerable of Image , and callers of that method are free to pass in lists of ImageWithSize or ImageWithCaption or any others they dream up next week

public void RunSomeprocess(IEnumerable<Image> ImageList)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM