Apply constraint to class, adhere to interface










0















I have the requirement to be able to perform many conversions of external models to my own internal models.



I have decided to apply the Adapter pattern, but I want to make it as generic as possible. So effectively, I want it to be handle both "single" POCO's, but if i need to pass/adapt a collection then this also must work eg:



IEnumerable<IAdaptee> OR IList<TAdaptee>


and return my own adapted object(s):



IEnumerable<IAdapted> OR IList<TAdapted>


I want to do something like the following:



public interface IGenericAdapter

TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee);



Where I am coming unstuck is when I build my "Adapter" class, and then implement the above interface, I am getting constraint mismatch errors. This of course, makes sense, because If i am applying constraints to the classes which implement the interface, and the interface doesn't have them, then of course errors occur.



So:



public class AToBAdapter

public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee)
where TAdapted: IList<FooAdapted>
where TAdaptee: IList<FooAdaptee>

// I want my constraints to be active here, as I need to perform specific operations here




The above works fine in and of itself, which is fine. But I want to hide all this behind a generic interface that I can use whenever it suits.



Of course, Once i add this it fails due to no constraints on the interface, yet constraints on the implementing class.



public class AToBAdapter:IAdapterGeneric


What's the magic bullet here which will enable me to build a truly generic Adapter - I'm guessing certain constraints on the interface? casting? but need assistance on the best course of action.



Thanks,
Chud










share|improve this question






















  • what is the purpose and benefits of use IGenericAdapter ?

    – Z.R.T.
    Nov 15 '18 at 3:43











  • I will have many adapters, coming from many different sources. So i want to simplify my ability to homogenise my multi source data, into a single source (my model) with a singular adapter if possible

    – The_Chud
    Nov 15 '18 at 3:45












  • could you explain " ability to homogenise my multi source data, into a single source (my model) with a singular adapter". i dont understand your problem

    – Z.R.T.
    Nov 15 '18 at 3:50











  • Ok. Let's use comething like a hotel comparison site (it's not what I'm doing but an example) Let's say i want to pull data from tripadvisor,agoda,booking,hotelscombined. Each of these have their own "Models" in place fo what they name a hotel. Ex - HotelName, Name,PropertyName etc. I want to to get all this data and adapt it into my own. EX IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,TripAdvisorModel> and IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,AgodaModel> and effectively "Adapt" to MY model.

    – The_Chud
    Nov 15 '18 at 3:53















0















I have the requirement to be able to perform many conversions of external models to my own internal models.



I have decided to apply the Adapter pattern, but I want to make it as generic as possible. So effectively, I want it to be handle both "single" POCO's, but if i need to pass/adapt a collection then this also must work eg:



IEnumerable<IAdaptee> OR IList<TAdaptee>


and return my own adapted object(s):



IEnumerable<IAdapted> OR IList<TAdapted>


I want to do something like the following:



public interface IGenericAdapter

TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee);



Where I am coming unstuck is when I build my "Adapter" class, and then implement the above interface, I am getting constraint mismatch errors. This of course, makes sense, because If i am applying constraints to the classes which implement the interface, and the interface doesn't have them, then of course errors occur.



So:



public class AToBAdapter

public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee)
where TAdapted: IList<FooAdapted>
where TAdaptee: IList<FooAdaptee>

// I want my constraints to be active here, as I need to perform specific operations here




The above works fine in and of itself, which is fine. But I want to hide all this behind a generic interface that I can use whenever it suits.



Of course, Once i add this it fails due to no constraints on the interface, yet constraints on the implementing class.



public class AToBAdapter:IAdapterGeneric


What's the magic bullet here which will enable me to build a truly generic Adapter - I'm guessing certain constraints on the interface? casting? but need assistance on the best course of action.



Thanks,
Chud










share|improve this question






















  • what is the purpose and benefits of use IGenericAdapter ?

    – Z.R.T.
    Nov 15 '18 at 3:43











  • I will have many adapters, coming from many different sources. So i want to simplify my ability to homogenise my multi source data, into a single source (my model) with a singular adapter if possible

    – The_Chud
    Nov 15 '18 at 3:45












  • could you explain " ability to homogenise my multi source data, into a single source (my model) with a singular adapter". i dont understand your problem

    – Z.R.T.
    Nov 15 '18 at 3:50











  • Ok. Let's use comething like a hotel comparison site (it's not what I'm doing but an example) Let's say i want to pull data from tripadvisor,agoda,booking,hotelscombined. Each of these have their own "Models" in place fo what they name a hotel. Ex - HotelName, Name,PropertyName etc. I want to to get all this data and adapt it into my own. EX IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,TripAdvisorModel> and IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,AgodaModel> and effectively "Adapt" to MY model.

    – The_Chud
    Nov 15 '18 at 3:53













0












0








0








I have the requirement to be able to perform many conversions of external models to my own internal models.



I have decided to apply the Adapter pattern, but I want to make it as generic as possible. So effectively, I want it to be handle both "single" POCO's, but if i need to pass/adapt a collection then this also must work eg:



IEnumerable<IAdaptee> OR IList<TAdaptee>


and return my own adapted object(s):



IEnumerable<IAdapted> OR IList<TAdapted>


I want to do something like the following:



public interface IGenericAdapter

TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee);



Where I am coming unstuck is when I build my "Adapter" class, and then implement the above interface, I am getting constraint mismatch errors. This of course, makes sense, because If i am applying constraints to the classes which implement the interface, and the interface doesn't have them, then of course errors occur.



So:



public class AToBAdapter

public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee)
where TAdapted: IList<FooAdapted>
where TAdaptee: IList<FooAdaptee>

// I want my constraints to be active here, as I need to perform specific operations here




The above works fine in and of itself, which is fine. But I want to hide all this behind a generic interface that I can use whenever it suits.



Of course, Once i add this it fails due to no constraints on the interface, yet constraints on the implementing class.



public class AToBAdapter:IAdapterGeneric


What's the magic bullet here which will enable me to build a truly generic Adapter - I'm guessing certain constraints on the interface? casting? but need assistance on the best course of action.



Thanks,
Chud










share|improve this question














I have the requirement to be able to perform many conversions of external models to my own internal models.



I have decided to apply the Adapter pattern, but I want to make it as generic as possible. So effectively, I want it to be handle both "single" POCO's, but if i need to pass/adapt a collection then this also must work eg:



IEnumerable<IAdaptee> OR IList<TAdaptee>


and return my own adapted object(s):



IEnumerable<IAdapted> OR IList<TAdapted>


I want to do something like the following:



public interface IGenericAdapter

TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee);



Where I am coming unstuck is when I build my "Adapter" class, and then implement the above interface, I am getting constraint mismatch errors. This of course, makes sense, because If i am applying constraints to the classes which implement the interface, and the interface doesn't have them, then of course errors occur.



So:



public class AToBAdapter

public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee)
where TAdapted: IList<FooAdapted>
where TAdaptee: IList<FooAdaptee>

// I want my constraints to be active here, as I need to perform specific operations here




The above works fine in and of itself, which is fine. But I want to hide all this behind a generic interface that I can use whenever it suits.



Of course, Once i add this it fails due to no constraints on the interface, yet constraints on the implementing class.



public class AToBAdapter:IAdapterGeneric


What's the magic bullet here which will enable me to build a truly generic Adapter - I'm guessing certain constraints on the interface? casting? but need assistance on the best course of action.



Thanks,
Chud







c# generics interface adapter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 2:22









The_ChudThe_Chud

220212




220212












  • what is the purpose and benefits of use IGenericAdapter ?

    – Z.R.T.
    Nov 15 '18 at 3:43











  • I will have many adapters, coming from many different sources. So i want to simplify my ability to homogenise my multi source data, into a single source (my model) with a singular adapter if possible

    – The_Chud
    Nov 15 '18 at 3:45












  • could you explain " ability to homogenise my multi source data, into a single source (my model) with a singular adapter". i dont understand your problem

    – Z.R.T.
    Nov 15 '18 at 3:50











  • Ok. Let's use comething like a hotel comparison site (it's not what I'm doing but an example) Let's say i want to pull data from tripadvisor,agoda,booking,hotelscombined. Each of these have their own "Models" in place fo what they name a hotel. Ex - HotelName, Name,PropertyName etc. I want to to get all this data and adapt it into my own. EX IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,TripAdvisorModel> and IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,AgodaModel> and effectively "Adapt" to MY model.

    – The_Chud
    Nov 15 '18 at 3:53

















  • what is the purpose and benefits of use IGenericAdapter ?

    – Z.R.T.
    Nov 15 '18 at 3:43











  • I will have many adapters, coming from many different sources. So i want to simplify my ability to homogenise my multi source data, into a single source (my model) with a singular adapter if possible

    – The_Chud
    Nov 15 '18 at 3:45












  • could you explain " ability to homogenise my multi source data, into a single source (my model) with a singular adapter". i dont understand your problem

    – Z.R.T.
    Nov 15 '18 at 3:50











  • Ok. Let's use comething like a hotel comparison site (it's not what I'm doing but an example) Let's say i want to pull data from tripadvisor,agoda,booking,hotelscombined. Each of these have their own "Models" in place fo what they name a hotel. Ex - HotelName, Name,PropertyName etc. I want to to get all this data and adapt it into my own. EX IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,TripAdvisorModel> and IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,AgodaModel> and effectively "Adapt" to MY model.

    – The_Chud
    Nov 15 '18 at 3:53
















what is the purpose and benefits of use IGenericAdapter ?

– Z.R.T.
Nov 15 '18 at 3:43





what is the purpose and benefits of use IGenericAdapter ?

– Z.R.T.
Nov 15 '18 at 3:43













I will have many adapters, coming from many different sources. So i want to simplify my ability to homogenise my multi source data, into a single source (my model) with a singular adapter if possible

– The_Chud
Nov 15 '18 at 3:45






I will have many adapters, coming from many different sources. So i want to simplify my ability to homogenise my multi source data, into a single source (my model) with a singular adapter if possible

– The_Chud
Nov 15 '18 at 3:45














could you explain " ability to homogenise my multi source data, into a single source (my model) with a singular adapter". i dont understand your problem

– Z.R.T.
Nov 15 '18 at 3:50





could you explain " ability to homogenise my multi source data, into a single source (my model) with a singular adapter". i dont understand your problem

– Z.R.T.
Nov 15 '18 at 3:50













Ok. Let's use comething like a hotel comparison site (it's not what I'm doing but an example) Let's say i want to pull data from tripadvisor,agoda,booking,hotelscombined. Each of these have their own "Models" in place fo what they name a hotel. Ex - HotelName, Name,PropertyName etc. I want to to get all this data and adapt it into my own. EX IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,TripAdvisorModel> and IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,AgodaModel> and effectively "Adapt" to MY model.

– The_Chud
Nov 15 '18 at 3:53





Ok. Let's use comething like a hotel comparison site (it's not what I'm doing but an example) Let's say i want to pull data from tripadvisor,agoda,booking,hotelscombined. Each of these have their own "Models" in place fo what they name a hotel. Ex - HotelName, Name,PropertyName etc. I want to to get all this data and adapt it into my own. EX IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,TripAdvisorModel> and IList<MyHotelData> = Adapter.Adapt<MyAdaptedData,AgodaModel> and effectively "Adapt" to MY model.

– The_Chud
Nov 15 '18 at 3:53












2 Answers
2






active

oldest

votes


















1














If you have access to your external models, you could use an interface as a marker:



public interface IAdaptee 
public interface IAdapted


And use those interfaces as your adapter interface constraints:



public interface IGenericAdapter<out TAdapted, in TAdaptee>
where TAdaptee : IAdaptee
where TAdapted : IAdapted

TAdapted Adapt(TAdaptee adaptee);



You could pass this adapter into a helper method for adapting multiple objects (assuming the adapt logic stays the same for multiple):



public IEnumerable<TAdapted> AdaptMultiple<TAdapted, TAdaptee>
(IEnumerable<TAdaptee> adaptees, IGenericAdapter<TAdapted, TAdaptee> adapter)
where TAdaptee : IAdaptee
where TAdapted : IAdapted

return adaptees.Select(adapter.Adapt);



For example, we can construct the following concrete classes:



public class ConcreteAdaptee : IAdaptee 

public class ConcreteAdapted : IAdapted

public class ConcreteAdapter : IGenericAdapter<ConcreteAdapted, ConcreteAdaptee>

public ConcreteAdapted Adapt(ConcreteAdaptee adaptee)

// Adapt Logic

return new ConcreteAdapted();




And adapt them as such:



IGenericAdapter<ConcreteAdapted, ConcreteAdaptee> adapter = new ConcreteAdapter();

var adaptee = new ConcreteAdaptee();
var adapted = adapter.Adapt(adaptee);

var adaptees = new List<ConcreteAdaptee>();
var adapteds = AdaptMultiple(adaptees, adapter);





share|improve this answer























  • Hi Kyle, This is close to the mark. I have to go out shortly so I can't fully test/investigate this. My question to you is .... what about casting inside the adapt method. If we are using a specific ConcreteAdapter, then the concrete classes themselves know what is going in -> going out ... so public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee) IList<External> external = (IList<External)adaptee; //perform full adapt; return internal model;??

    – The_Chud
    Nov 15 '18 at 4:56






  • 1





    I think you would want to create an adapter specific to each unique adaptee/adapted pair you have, so that you wouldn't have to typecheck inside the adapter. If you need a way to find the correct adapter, you could use dependency injection to inject an adapter based on the input object's type and the concrete object you'd like to adapt to.

    – Kyle Polansky
    Nov 15 '18 at 5:34



















0














consider the following example :



interface IDog

void Bark();


interface ICat

void Meow();

class SomeDog : IDog

public void Bark()

Console.WriteLine(@"bark bark");



class SomeCat : ICat

public void Meow()

Console.WriteLine(@"meow meow");



class CatToDogAdapter : IDog

private ICat cat;
public CatToDogAdapter(ICat cat)

this.cat = cat;


public void Bark()

cat.Meow();



new Dog().Bark(); // bark bark
new CatToDogAdapter().Bark();// meow meow


this is how adapter works.



Let say you have a model MyAdaptedData and you recive data containing HotelName, Name,PropertyName from agoda , booking and tripadviser
for each booking model you need to write adapter



AgodaAdapter : MyAdaptedData
public AgodaAdapter(AgodaModel agodaModel)
....

public stirng HotelName
get
return agodaModel.HotelNamebyAgoda;


....



Same for booking and tripadvisor.
Adapter pattern helps you to retrieve necessary data from external models.



Then you need to create specific adapter depending on the adaptee type. Use Factory method pattern



MyAdaptedData AdaptersFactory(object adaptee)

if(adaptee is AgodaModel)
return new AgodaAdapter(adaptee);
....
if(adaptee is XBookingModel)
return new XBookingAdapter(adaptee);






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311525%2fapply-constraint-to-class-adhere-to-interface%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you have access to your external models, you could use an interface as a marker:



    public interface IAdaptee 
    public interface IAdapted


    And use those interfaces as your adapter interface constraints:



    public interface IGenericAdapter<out TAdapted, in TAdaptee>
    where TAdaptee : IAdaptee
    where TAdapted : IAdapted

    TAdapted Adapt(TAdaptee adaptee);



    You could pass this adapter into a helper method for adapting multiple objects (assuming the adapt logic stays the same for multiple):



    public IEnumerable<TAdapted> AdaptMultiple<TAdapted, TAdaptee>
    (IEnumerable<TAdaptee> adaptees, IGenericAdapter<TAdapted, TAdaptee> adapter)
    where TAdaptee : IAdaptee
    where TAdapted : IAdapted

    return adaptees.Select(adapter.Adapt);



    For example, we can construct the following concrete classes:



    public class ConcreteAdaptee : IAdaptee 

    public class ConcreteAdapted : IAdapted

    public class ConcreteAdapter : IGenericAdapter<ConcreteAdapted, ConcreteAdaptee>

    public ConcreteAdapted Adapt(ConcreteAdaptee adaptee)

    // Adapt Logic

    return new ConcreteAdapted();




    And adapt them as such:



    IGenericAdapter<ConcreteAdapted, ConcreteAdaptee> adapter = new ConcreteAdapter();

    var adaptee = new ConcreteAdaptee();
    var adapted = adapter.Adapt(adaptee);

    var adaptees = new List<ConcreteAdaptee>();
    var adapteds = AdaptMultiple(adaptees, adapter);





    share|improve this answer























    • Hi Kyle, This is close to the mark. I have to go out shortly so I can't fully test/investigate this. My question to you is .... what about casting inside the adapt method. If we are using a specific ConcreteAdapter, then the concrete classes themselves know what is going in -> going out ... so public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee) IList<External> external = (IList<External)adaptee; //perform full adapt; return internal model;??

      – The_Chud
      Nov 15 '18 at 4:56






    • 1





      I think you would want to create an adapter specific to each unique adaptee/adapted pair you have, so that you wouldn't have to typecheck inside the adapter. If you need a way to find the correct adapter, you could use dependency injection to inject an adapter based on the input object's type and the concrete object you'd like to adapt to.

      – Kyle Polansky
      Nov 15 '18 at 5:34
















    1














    If you have access to your external models, you could use an interface as a marker:



    public interface IAdaptee 
    public interface IAdapted


    And use those interfaces as your adapter interface constraints:



    public interface IGenericAdapter<out TAdapted, in TAdaptee>
    where TAdaptee : IAdaptee
    where TAdapted : IAdapted

    TAdapted Adapt(TAdaptee adaptee);



    You could pass this adapter into a helper method for adapting multiple objects (assuming the adapt logic stays the same for multiple):



    public IEnumerable<TAdapted> AdaptMultiple<TAdapted, TAdaptee>
    (IEnumerable<TAdaptee> adaptees, IGenericAdapter<TAdapted, TAdaptee> adapter)
    where TAdaptee : IAdaptee
    where TAdapted : IAdapted

    return adaptees.Select(adapter.Adapt);



    For example, we can construct the following concrete classes:



    public class ConcreteAdaptee : IAdaptee 

    public class ConcreteAdapted : IAdapted

    public class ConcreteAdapter : IGenericAdapter<ConcreteAdapted, ConcreteAdaptee>

    public ConcreteAdapted Adapt(ConcreteAdaptee adaptee)

    // Adapt Logic

    return new ConcreteAdapted();




    And adapt them as such:



    IGenericAdapter<ConcreteAdapted, ConcreteAdaptee> adapter = new ConcreteAdapter();

    var adaptee = new ConcreteAdaptee();
    var adapted = adapter.Adapt(adaptee);

    var adaptees = new List<ConcreteAdaptee>();
    var adapteds = AdaptMultiple(adaptees, adapter);





    share|improve this answer























    • Hi Kyle, This is close to the mark. I have to go out shortly so I can't fully test/investigate this. My question to you is .... what about casting inside the adapt method. If we are using a specific ConcreteAdapter, then the concrete classes themselves know what is going in -> going out ... so public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee) IList<External> external = (IList<External)adaptee; //perform full adapt; return internal model;??

      – The_Chud
      Nov 15 '18 at 4:56






    • 1





      I think you would want to create an adapter specific to each unique adaptee/adapted pair you have, so that you wouldn't have to typecheck inside the adapter. If you need a way to find the correct adapter, you could use dependency injection to inject an adapter based on the input object's type and the concrete object you'd like to adapt to.

      – Kyle Polansky
      Nov 15 '18 at 5:34














    1












    1








    1







    If you have access to your external models, you could use an interface as a marker:



    public interface IAdaptee 
    public interface IAdapted


    And use those interfaces as your adapter interface constraints:



    public interface IGenericAdapter<out TAdapted, in TAdaptee>
    where TAdaptee : IAdaptee
    where TAdapted : IAdapted

    TAdapted Adapt(TAdaptee adaptee);



    You could pass this adapter into a helper method for adapting multiple objects (assuming the adapt logic stays the same for multiple):



    public IEnumerable<TAdapted> AdaptMultiple<TAdapted, TAdaptee>
    (IEnumerable<TAdaptee> adaptees, IGenericAdapter<TAdapted, TAdaptee> adapter)
    where TAdaptee : IAdaptee
    where TAdapted : IAdapted

    return adaptees.Select(adapter.Adapt);



    For example, we can construct the following concrete classes:



    public class ConcreteAdaptee : IAdaptee 

    public class ConcreteAdapted : IAdapted

    public class ConcreteAdapter : IGenericAdapter<ConcreteAdapted, ConcreteAdaptee>

    public ConcreteAdapted Adapt(ConcreteAdaptee adaptee)

    // Adapt Logic

    return new ConcreteAdapted();




    And adapt them as such:



    IGenericAdapter<ConcreteAdapted, ConcreteAdaptee> adapter = new ConcreteAdapter();

    var adaptee = new ConcreteAdaptee();
    var adapted = adapter.Adapt(adaptee);

    var adaptees = new List<ConcreteAdaptee>();
    var adapteds = AdaptMultiple(adaptees, adapter);





    share|improve this answer













    If you have access to your external models, you could use an interface as a marker:



    public interface IAdaptee 
    public interface IAdapted


    And use those interfaces as your adapter interface constraints:



    public interface IGenericAdapter<out TAdapted, in TAdaptee>
    where TAdaptee : IAdaptee
    where TAdapted : IAdapted

    TAdapted Adapt(TAdaptee adaptee);



    You could pass this adapter into a helper method for adapting multiple objects (assuming the adapt logic stays the same for multiple):



    public IEnumerable<TAdapted> AdaptMultiple<TAdapted, TAdaptee>
    (IEnumerable<TAdaptee> adaptees, IGenericAdapter<TAdapted, TAdaptee> adapter)
    where TAdaptee : IAdaptee
    where TAdapted : IAdapted

    return adaptees.Select(adapter.Adapt);



    For example, we can construct the following concrete classes:



    public class ConcreteAdaptee : IAdaptee 

    public class ConcreteAdapted : IAdapted

    public class ConcreteAdapter : IGenericAdapter<ConcreteAdapted, ConcreteAdaptee>

    public ConcreteAdapted Adapt(ConcreteAdaptee adaptee)

    // Adapt Logic

    return new ConcreteAdapted();




    And adapt them as such:



    IGenericAdapter<ConcreteAdapted, ConcreteAdaptee> adapter = new ConcreteAdapter();

    var adaptee = new ConcreteAdaptee();
    var adapted = adapter.Adapt(adaptee);

    var adaptees = new List<ConcreteAdaptee>();
    var adapteds = AdaptMultiple(adaptees, adapter);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 15 '18 at 4:40









    Kyle PolanskyKyle Polansky

    36136




    36136












    • Hi Kyle, This is close to the mark. I have to go out shortly so I can't fully test/investigate this. My question to you is .... what about casting inside the adapt method. If we are using a specific ConcreteAdapter, then the concrete classes themselves know what is going in -> going out ... so public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee) IList<External> external = (IList<External)adaptee; //perform full adapt; return internal model;??

      – The_Chud
      Nov 15 '18 at 4:56






    • 1





      I think you would want to create an adapter specific to each unique adaptee/adapted pair you have, so that you wouldn't have to typecheck inside the adapter. If you need a way to find the correct adapter, you could use dependency injection to inject an adapter based on the input object's type and the concrete object you'd like to adapt to.

      – Kyle Polansky
      Nov 15 '18 at 5:34


















    • Hi Kyle, This is close to the mark. I have to go out shortly so I can't fully test/investigate this. My question to you is .... what about casting inside the adapt method. If we are using a specific ConcreteAdapter, then the concrete classes themselves know what is going in -> going out ... so public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee) IList<External> external = (IList<External)adaptee; //perform full adapt; return internal model;??

      – The_Chud
      Nov 15 '18 at 4:56






    • 1





      I think you would want to create an adapter specific to each unique adaptee/adapted pair you have, so that you wouldn't have to typecheck inside the adapter. If you need a way to find the correct adapter, you could use dependency injection to inject an adapter based on the input object's type and the concrete object you'd like to adapt to.

      – Kyle Polansky
      Nov 15 '18 at 5:34

















    Hi Kyle, This is close to the mark. I have to go out shortly so I can't fully test/investigate this. My question to you is .... what about casting inside the adapt method. If we are using a specific ConcreteAdapter, then the concrete classes themselves know what is going in -> going out ... so public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee) IList<External> external = (IList<External)adaptee; //perform full adapt; return internal model;??

    – The_Chud
    Nov 15 '18 at 4:56





    Hi Kyle, This is close to the mark. I have to go out shortly so I can't fully test/investigate this. My question to you is .... what about casting inside the adapt method. If we are using a specific ConcreteAdapter, then the concrete classes themselves know what is going in -> going out ... so public TAdapted Adapt<TAdapted,TAdaptee>(TAdaptee adaptee) IList<External> external = (IList<External)adaptee; //perform full adapt; return internal model;??

    – The_Chud
    Nov 15 '18 at 4:56




    1




    1





    I think you would want to create an adapter specific to each unique adaptee/adapted pair you have, so that you wouldn't have to typecheck inside the adapter. If you need a way to find the correct adapter, you could use dependency injection to inject an adapter based on the input object's type and the concrete object you'd like to adapt to.

    – Kyle Polansky
    Nov 15 '18 at 5:34






    I think you would want to create an adapter specific to each unique adaptee/adapted pair you have, so that you wouldn't have to typecheck inside the adapter. If you need a way to find the correct adapter, you could use dependency injection to inject an adapter based on the input object's type and the concrete object you'd like to adapt to.

    – Kyle Polansky
    Nov 15 '18 at 5:34














    0














    consider the following example :



    interface IDog

    void Bark();


    interface ICat

    void Meow();

    class SomeDog : IDog

    public void Bark()

    Console.WriteLine(@"bark bark");



    class SomeCat : ICat

    public void Meow()

    Console.WriteLine(@"meow meow");



    class CatToDogAdapter : IDog

    private ICat cat;
    public CatToDogAdapter(ICat cat)

    this.cat = cat;


    public void Bark()

    cat.Meow();



    new Dog().Bark(); // bark bark
    new CatToDogAdapter().Bark();// meow meow


    this is how adapter works.



    Let say you have a model MyAdaptedData and you recive data containing HotelName, Name,PropertyName from agoda , booking and tripadviser
    for each booking model you need to write adapter



    AgodaAdapter : MyAdaptedData
    public AgodaAdapter(AgodaModel agodaModel)
    ....

    public stirng HotelName
    get
    return agodaModel.HotelNamebyAgoda;


    ....



    Same for booking and tripadvisor.
    Adapter pattern helps you to retrieve necessary data from external models.



    Then you need to create specific adapter depending on the adaptee type. Use Factory method pattern



    MyAdaptedData AdaptersFactory(object adaptee)

    if(adaptee is AgodaModel)
    return new AgodaAdapter(adaptee);
    ....
    if(adaptee is XBookingModel)
    return new XBookingAdapter(adaptee);






    share|improve this answer





























      0














      consider the following example :



      interface IDog

      void Bark();


      interface ICat

      void Meow();

      class SomeDog : IDog

      public void Bark()

      Console.WriteLine(@"bark bark");



      class SomeCat : ICat

      public void Meow()

      Console.WriteLine(@"meow meow");



      class CatToDogAdapter : IDog

      private ICat cat;
      public CatToDogAdapter(ICat cat)

      this.cat = cat;


      public void Bark()

      cat.Meow();



      new Dog().Bark(); // bark bark
      new CatToDogAdapter().Bark();// meow meow


      this is how adapter works.



      Let say you have a model MyAdaptedData and you recive data containing HotelName, Name,PropertyName from agoda , booking and tripadviser
      for each booking model you need to write adapter



      AgodaAdapter : MyAdaptedData
      public AgodaAdapter(AgodaModel agodaModel)
      ....

      public stirng HotelName
      get
      return agodaModel.HotelNamebyAgoda;


      ....



      Same for booking and tripadvisor.
      Adapter pattern helps you to retrieve necessary data from external models.



      Then you need to create specific adapter depending on the adaptee type. Use Factory method pattern



      MyAdaptedData AdaptersFactory(object adaptee)

      if(adaptee is AgodaModel)
      return new AgodaAdapter(adaptee);
      ....
      if(adaptee is XBookingModel)
      return new XBookingAdapter(adaptee);






      share|improve this answer



























        0












        0








        0







        consider the following example :



        interface IDog

        void Bark();


        interface ICat

        void Meow();

        class SomeDog : IDog

        public void Bark()

        Console.WriteLine(@"bark bark");



        class SomeCat : ICat

        public void Meow()

        Console.WriteLine(@"meow meow");



        class CatToDogAdapter : IDog

        private ICat cat;
        public CatToDogAdapter(ICat cat)

        this.cat = cat;


        public void Bark()

        cat.Meow();



        new Dog().Bark(); // bark bark
        new CatToDogAdapter().Bark();// meow meow


        this is how adapter works.



        Let say you have a model MyAdaptedData and you recive data containing HotelName, Name,PropertyName from agoda , booking and tripadviser
        for each booking model you need to write adapter



        AgodaAdapter : MyAdaptedData
        public AgodaAdapter(AgodaModel agodaModel)
        ....

        public stirng HotelName
        get
        return agodaModel.HotelNamebyAgoda;


        ....



        Same for booking and tripadvisor.
        Adapter pattern helps you to retrieve necessary data from external models.



        Then you need to create specific adapter depending on the adaptee type. Use Factory method pattern



        MyAdaptedData AdaptersFactory(object adaptee)

        if(adaptee is AgodaModel)
        return new AgodaAdapter(adaptee);
        ....
        if(adaptee is XBookingModel)
        return new XBookingAdapter(adaptee);






        share|improve this answer















        consider the following example :



        interface IDog

        void Bark();


        interface ICat

        void Meow();

        class SomeDog : IDog

        public void Bark()

        Console.WriteLine(@"bark bark");



        class SomeCat : ICat

        public void Meow()

        Console.WriteLine(@"meow meow");



        class CatToDogAdapter : IDog

        private ICat cat;
        public CatToDogAdapter(ICat cat)

        this.cat = cat;


        public void Bark()

        cat.Meow();



        new Dog().Bark(); // bark bark
        new CatToDogAdapter().Bark();// meow meow


        this is how adapter works.



        Let say you have a model MyAdaptedData and you recive data containing HotelName, Name,PropertyName from agoda , booking and tripadviser
        for each booking model you need to write adapter



        AgodaAdapter : MyAdaptedData
        public AgodaAdapter(AgodaModel agodaModel)
        ....

        public stirng HotelName
        get
        return agodaModel.HotelNamebyAgoda;


        ....



        Same for booking and tripadvisor.
        Adapter pattern helps you to retrieve necessary data from external models.



        Then you need to create specific adapter depending on the adaptee type. Use Factory method pattern



        MyAdaptedData AdaptersFactory(object adaptee)

        if(adaptee is AgodaModel)
        return new AgodaAdapter(adaptee);
        ....
        if(adaptee is XBookingModel)
        return new XBookingAdapter(adaptee);







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 5:37

























        answered Nov 15 '18 at 4:16









        Z.R.T.Z.R.T.

        1,068512




        1,068512



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53311525%2fapply-constraint-to-class-adhere-to-interface%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto