logo
down
shadow

Exposing interfaces instead of concrete classes


Exposing interfaces instead of concrete classes

Content Index :

Exposing interfaces instead of concrete classes
Tag : chash , By : Blight
Date : November 23 2020, 01:01 AM

I wish this helpful for you
Of course I can return any type that implements IEnumerable but how will the external user of the class know that?

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

Why should I create interfaces for my concrete DataProvider classes


Tag : chash , By : John Q.
Date : March 29 2020, 07:55 AM
may help you . You should create interfaces in order to make testing code that depends on your data easier.
Having interfaces and coding to these interfaces means you can inject mocks and stubs in your tests.

Concrete classes vs interfaces: When to use?


Tag : development , By : James
Date : March 29 2020, 07:55 AM
seems to work fine In your IConnection example you're basically describing an abstract Connect() method, since each class will have to implement its own version. Usually (always?) abstract methods can only be defined with the same parameters, so Connect(username, password) and Connect(key) couldn't be implementations of the same Connect() method from an interface.
Now, at this point, you could define it as IConnection::Connect(SomeConnectionData) and derive UsernamePasswordConnectionData and KeyConnectionData, etc., etc. from that SomeConnectionData class but all this would be so hard to explain and implement that its pretty good clue that interfaces and inheritance aren't helping the situation.

How to deserialize collection of interfaces when concrete classes contains other interfaces


Tag : chash , By : alchemist
Date : March 29 2020, 07:55 AM
hope this fix your issue Your basic problem is that your ConcreteConverter is designed to deserialize something declared as an interface as a concrete type -- e.g. IItemModel as Item -- but you are not using it in that way. You are using it to deserialize a concrete list of interfaces as a concrete list of concrete types, e.g.:
[JsonProperty( "items" )]
[JsonConverter( typeof( ConcreteConverter<List<IItemModel>, List<Item>>) )]
public List<IItemModel> Items { get; set; }
public class ThumbnailJobModel : IJobModel
{
    [JsonProperty("items", ItemConverterType = typeof(ConcreteConverter<IItemModel, Item>))]
    public List<IItemModel> Items { get; set; }
public class Item : IItemModel
{
    [JsonProperty("shoots", ItemConverterType = typeof(ConcreteConverter<ICamSettings, ShootSettings>))]
    public List<ICamSettings> CamSettings { get; set; }
public class StrictStringEnumConverter : StringEnumConverter
{
    public StrictStringEnumConverter() { this.AllowIntegerValues = false; }
}
public class ConcreteConverter<IInterface, TConcrete> : JsonConverter where TConcrete : IInterface
{
}
public override bool CanConvert( Type objectType )
{
    return objectType == typeof( string );
}
public override bool CanConvert(Type objectType)
{
    return objectType == typeof(ExportType);
}
public interface IJobModel
{
    string ClientBaseURL { get; set; }
    string UserEmail { get; set; }
    ExportType Type { get; set; }
    List<IItemModel> Items { get; set; }
}

public interface IItemModel
{
    string Id { get; set; }
    string ImageSize { get; set; }
    string ImagePpi { get; set; }
    List<ICamSettings> CamSettings { get; set; }
}

public interface ICamSettings
{
    string FileName { get; set; }
}

public enum ExportType
{
    Thumbnails,
}

public class ThumbnailJobModel : IJobModel
{
    [JsonProperty("clientBaseURL")]
    public string ClientBaseURL { get; set; }

    [JsonProperty("userEmail")]
    public string UserEmail { get; set; }

    [JsonProperty("type")]
    [JsonConverter(typeof(ExportTypeConverter))]
    public ExportType Type { get; set; }

    [JsonProperty("items", ItemConverterType = typeof(ConcreteConverter<IItemModel, Item>))]
    public List<IItemModel> Items { get; set; }

    public ThumbnailJobModel()
    {
        Type = ExportType.Thumbnails;
        Items = new List<IItemModel>();
    }

    public class Item : IItemModel
    {
        [JsonProperty("id")]
        public string Id { get; set; }

        [JsonProperty("imageSize")]
        public string ImageSize { get; set; }

        [JsonProperty("imagePpi")]
        public string ImagePpi { get; set; }

        [JsonProperty("shoots", ItemConverterType = typeof(ConcreteConverter<ICamSettings, ShootSettings>))]
        public List<ICamSettings> CamSettings { get; set; }

        public Item()
        {
            CamSettings = new List<ICamSettings>();
        }
    }

    public class ShootSettings : ICamSettings
    {
        [JsonProperty("orientation")]
        [JsonConverter(typeof(StrictStringEnumConverter))]
        public Orientation Orientation { get; set; }

        [JsonProperty("clothShape")]
        [JsonConverter(typeof(StrictStringEnumConverter))]
        public Shape Shape { get; set; }

        [JsonProperty("fileName")]
        public string FileName { get; set; }

        public ShootSettings()
        {
            Orientation = Orientation.Perspective;
            Shape = Shape.Folded;
            FileName = null;
        }
    }

    public enum Orientation
    {
        Perspective = 0,
        Oblique = 1,
        Front = 2,
        Back = 3,
        Left = 4,
        Right = 5,
        Up = 6,
        Down = 7
    }

    public enum Shape
    {
        Folded = 0,
        Hanger = 1,
        Mannequin = 2
    }

    public class ConcreteConverter<IInterface, TConcrete> : JsonConverter where TConcrete : IInterface
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(IInterface) == objectType;
        }

        public override object ReadJson(JsonReader reader,
         Type objectType, object existingValue, JsonSerializer serializer)
        {
            return serializer.Deserialize<TConcrete>(reader);
        }

        public override bool CanWrite { get { return false; } }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }

    public class ExportTypeConverter : StringEnumConverter
    {
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            reader.Skip(); // Skip anything at the current reader's position.
            return ExportType.Thumbnails;
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(ExportType);
        }
    }

    public class StrictStringEnumConverter : StringEnumConverter
    {
        public StrictStringEnumConverter() { this.AllowIntegerValues = false; }
    }

    public static void HandleDeserializationError(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs errorArgs)
    {
        errorArgs.ErrorContext.Handled = true;
        var currentObj = errorArgs.CurrentObject as ShootSettings;

        if (currentObj == null) return;

        currentObj.Orientation = Orientation.Perspective;
        currentObj.Shape = Shape.Folded;
    }
}

Interfaces exposing properties and Kotlin data classes


Tag : spring-boot , By : user107021
Date : December 24 2020, 04:01 AM
hop of those help? Here's how I'd define these in kotlin, just using a name property (no need for a getName() method) and being sure to use the override keyword:
interface HasName {
   val name: String
}

data class Image(override val name: String, val url: URL): HasName

Mapping Interfaces to concrete classes


Tag : chash , By : FriendL
Date : March 29 2020, 07:55 AM
Related Posts Related QUESTIONS :
  • How do i increase a number by 1 in every line that contain the number 1
  • Add binding to elements that are created in codebehind
  • How to add a column in an existing AspNetUsers table
  • Order a list of elements with another list of doubles
  • How to setup a NuGet package to copy content files to output build directory?
  • In SignalR Core using ChannelWriter: Do I need to call TryComplete twice if there's an exception?
  • C# GetProcessesByName: issue with colon
  • c# wpf | create complex object with user-defined name to Serialize into JSON
  • How can I get a instance of a generic list with reflection?
  • WPF XAML - Design time and visibility of textbox
  • EF Core and MySql query is too slow
  • Getting Registered App Display Name from an App Id
  • How to get all variables from a string
  • Delete entity with all childs connected
  • Azure Build agent cant´t find class library referance
  • Initialize Nested Dictionaries in c#
  • .Net Core Binding
  • Generic event test method, preventing code duplication
  • How do I keep the ellipses in the center when the screen is resized
  • How to require a property using JsonSchema.NET?
  • C# XDocument Element/Elements returns null
  • Autofac keyed service with IEnumerable relationship type
  • Installing EntityFramework via NuGet manager
  • Always Check if there is Internet Connection Xamarin forms
  • WCF OneWay service slows down when aspNetCompatibilityEnabled is set to false
  • Can we use JsonRequestBehavior.AllowGet with [HttpPost] attribute?
  • How to customize the Setup wizard with custom forms in Visual Studio setup project
  • C# ASP.NET - Use method from another class to create labels
  • C# List IList or IEnumerable as argument
  • Parsing File with C# And Replace method
  • Losing special unicode characters in encryption (C#)
  • Getting stored procedure returned value instead of row affected
  • How can I construct HTML using NameValuePair in android?
  • Loading a pop up page in ASP.net through a js file
  • How to pass alert or notification message from controller to View?
  • C# to pause, turn on ssas server, backup cube.... how to?
  • How to execute DataTable.Select() for a column of custom class type for a particular element in that C#
  • how to connect mysql8.0 with C#
  • Passing incorrect values into MultiValueConverter by MultiBinding
  • Can i use IEnumerator as Update func?
  • How to convert API Json response to C# Array?
  • Blazor Textfield Oninput User Typing Delay
  • Performing both layout and render transform results in wrong output
  • uwp beforetextchanged cursor moving in front of text
  • How to keep duplicates from a string[] exclude words from a List and print them out
  • .Net Core Strings.Asc/Mid/Chr/Len missing even after importing Microsoft.VisualBasic
  • How to return to previous search page without being asked to Confirm Form Re-submission and keeping the results on ASP.N
  • How set a identity scaffolding item/page how initial page in asp.net MVC core?
  • LINQ isn't calling Dispose on my IEnumerator when using Union and Select, expected behavior or bug?
  • What is "ByteArray.uncompress()" in AS3 equivalent to in C#?
  • Getting a specific letter from a string variable for my simple guessing game for clues
  • Send an email with Outlook without a subject --- dialog box issue
  • passing List<MyModel> from my controller in the "WebInterfaceProject" to the processor method in "D
  • How to convert Word document created from template by OpenXML into MemoryStream?
  • How can I make a single slider that changes the color of an object?
  • Remap JSON parameter in c#
  • What is the difference between "this ref" and "ref this" when talking about C# 7.2 ref extension met
  • Convert OpenSSL encryption into native C#
  • Accessing Properties in Razor Pages
  • How to get SOAP element value
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com