c# - Alternatives to inheritance to adding additional functionality - TagMerge
4Alternatives to inheritance to adding additional functionalityAlternatives to inheritance to adding additional functionality

Alternatives to inheritance to adding additional functionality

Asked 5 months ago
1
4 answers

Solution I settled on was not to extend the ToolStrip or BindingNavigator directly at all. Instead, I created a new CustomToolStrip class that has a ToolStrip object as a property, and added all the new functionality to the Toolstrip object. I then created a CustomBindingNavigator class that extends CustomToolStrip, and adds all the BindingNavigator specific functionality. The ToolStrip property I overrode to make it a BindingNavigator.

In CustomToolStrip

private virtual ToolStrip ToolStrip { get; set; } = new ToolStrip();

In CustomBindingNavigator

private override ToolStrip ToolStrip {get; set;} = new BindingNavigator();

The toolstrip could then be injected to become a decorator pattern.

Source: link

0

Here is an approach that uses an extension method that targets your custom interface. Extension methods are a special kind of static methods on a static class that allow you to attach new functionality to any class or interface.

more info on extension methods here

I stripped out any implementation details and just showed how the types connect.

public class ToolStrip { }
public class BindingNavigator { }
public static class CustomToolStripExtensions
{
    public static void Setup<T>( this T target )  where T: ICustomToolStrip
    {
        // do setup work here!
    }
}
public interface ICustomToolStrip { }
public class ToolStripDecorator : ToolStrip, ICustomToolStrip
{
    public ToolStripDecorator()
    {
        this.Setup<ToolStripDecorator>();
    }
}
public class CustomBindingNavigator : BindingNavigator, ICustomToolStrip
{
    public CustomBindingNavigator()
    {
        this.Setup<CustomBindingNavigator>();
    }
}

Source: link

0

class Stack extends ArrayList {
    public void push(Object value) { … }
    public Object pop() { … }
}

Source: link

0

Let’s say we have the following code:
interface IBookController
{
    List<string> GetBookNames();
}

class BookController: IBookController
{
    private readonly IBookService _bookService;
    private readonly IOrderService _orderService;

    public BookController(IBookService bookService, IOrderService orderService)
    {
        _bookService = bookService;
        _orderService = orderService;
    }

    public List<string> GetBookNames()
    {
        //some code that retrieves and returns list of book names.
        return new List<string>();
    }

    //other code here...
}

class NonFictionBookController : BookController
{
    public NonFictionBookController(IBookService bookService, IOrderService orderService)
        : base(bookService, orderService)
    {

    }
}
The first thing we need to do is to define a new property in the BookController:
public ISessionService SessionService { get; set; }
Now it’s easy to inject a new dependency by initializing this property.
BookController bookController =
    new BookController(new BookService(), new OrderService());

bookController.SessionService = new SessionService();
In case I want to use an Inversion of Control container (also known as DI container) such as Autofac, the first thing you have to do is to register all necessary components:
//create builder object that will register all classes
var builder = new ContainerBuilder();

//register book controllers
builder.RegisterType<BookController>()
                .As<IBookController>().AsSelf().PropertiesAutowired();
builder.RegisterType<NonFictionBookController>();

//register book dependencies
builder.RegisterType<BookService>().As<IBookService>().AsSelf();
builder.RegisterType<OrderService>().As<IOrderService>().AsSelf();
builder.RegisterType<SessionService>().As<ISessionService>().AsSelf();

//build the container can create, wire and manage all dependencies
_container = builder.Build();
The code to resolve BookController is as follows:
var bookController = _container.Resolve<IBookController>();

Source: link

Recent Questions on c#

    Programming Languages