Can I create a super-class with a restricted generic type? - typescript1.4

I have the class:
export class LinkedList<t>
I presently create a super class using:
export class ListBody extends collections.LinkedList<Element>
And it all works great. However, I declare this class in several other classes and in each place, it actually only takes several super-classes of Element. So I would like in thos other places, instead of declaring:
this.body = new moduleListBody.ListBody();
declare:
this.body = new moduleListBody.ListBody<ElementOne | ElementTwo>();
Is there a way to do this? I can't skip past the ListBody class because I have a ton of code in it that operates on Element objects.
Update: example
export class Element {
}
export class LinkedList<t> {
public add(index : number, value : t);
public add(index : number, collection : LinkedList<t>);
public add(index : number, collection : t[]);
add(index : number, arg : t | LinkedList<t> | t[]) {
}
}
export class ListBody <T extends Element> extends LinkedList<T> {
public fubar(): void {
var element = new Element();
this.add(0, element);
}
}

This is definitely possible, the below code is an example of how you can accomplish something like this:
class LinkedList<T> {
el: T
}
class ListBody<T extends Element> extends LinkedList<T> { }
var list = new ListBody<HTMLElement | SVGElement>();
list.el will be HTMLElement | SVGElement.
UPDATE: You would need to pass the type into fubar, or alternatively store the constructor for T on the extended classes. See below:
export class ListBody <T extends Element> extends LinkedList<T> {
public fubar(Type: new () => T): void {
var element = new Type();
this.add(0, element);
}
}
or
export class ListBody<T extends Element> extends LinkedList<T> {
Type: new () => T;
public fubar(): void {
var element = new this.Type();
this.add(0, element);
}
}

Related

inner class in typescript nativescript

I want to use a Java inner class in typescript in nativescript framework for Android.
First I tried to put it in a separate class:
class LocalBinder extends android.os.Binder {
private foregroundService;
constructor(foregroundService) {
super();
this.foregroundService = foregroundService;
return global.__native(this);
}
public getService() {
return this.foregroundService;
}
}
and put this line in ForegroundService:
private mLocalBinder = new LocalBinder(this);
but I failed.
Here is the Java code I want to rewrite to typescript in nativescript:
public class ForegroundService extends Service {
private final LocalBinder mLocalBinder = new LocalBinder();
public class LocalBinder extends Binder {
public ForegroundService getService() {
return ForegroundService.this;
}
}
.......
}

How Get data from a derived class?

Get data from a derived class..
My samples.
public class Maintest
{
public string name = "2";
}
public class test : Maintest
{
string bla = name;
}
or
public class test : Maintest
{
test child = new test();
string bla = child.name;
}
Please reply
or
Share a link to explore
For example there is the main class
and I have a derived class that will output data of the first class.
As an example, I just wanted to pass the value of the derivative in the main class. For a proper understanding
If you return the field from a property, it might look a little something like this.
using System;
public class Program
{
public void Main()
{
var test = new Test();
Console.WriteLine(test.greeting);
}
}
// make this abstract if you're never directly instantiate MainTest
public abstract class MainTest
{
public string name = "world";
}
public class Test : MainTest
{
public string greeting {get { return "Hello " + name;}}
}
http://dotnetfiddle.net/R8kwh3
Also, you can enforce a contract by doing something like
public abstract class MainTest
{
public string name = "world";
// create an abstract property to ensure it gets implemented in the inheriting class
public abstract string greeting {get; private set;}
}
public class Test : MainTest
{
public override string greeting {get { return "Hello " + name;}}
}
Maybe you want get data in method? Therefore, you can use this:
public class test : Maintest
{
public string GetData()
{
return name;
}
}

Can you create multiple instances of a class using method injection or some other form of injection?

public interface IFoo {}
public class Foo : IFoo {}
public sealed class NinjaModule : NinjectModule //Appropriately configured in project
{
public override void Load()
{
Bind<IFoo>.To<Foo>.InTransientScope();
}
}
public class SomeOtherClass : ISomeOtherInterface
{
public SomeOtherClass();
public IFoo GetFoo(IFoo foo)
{
return foo;
}
public void GetFoos()
{
foreach (var thing in everything)
{
var localFoo = GetFoo();
localFoo.UniqueProperty = "I am unique";
_fooList.Add(localFoo);
}
}
}
I need my code to look something like that.
Yes, I am fairly new to inject. I am fairly certain that I am missing a basic principle.
Thanks for any help.
I think the best approach here would be to use a factory to create the different Foo instances. And with Ninject and the Factory extension that's pretty easy.
public sealed class NinjaModule : NinjectModule //Appropriately configured in project
{
public override void Load()
{
Bind<IFoo>().To<Foo>().InTransientScope();
Bind<IFooFactory>().ToFactory();
}
}
public interface IFooFactory
{
IFoo CreateFoo();
}
public class SomeOtherClass : ISomeOtherInterface
{
private IFooFactory fooFactory;
public SomeOtherClass(IFooFactory fooFactory)
{
this.fooFactory = fooFactory;
}
public IFoo GetFoo(IFoo foo)
{
return this.fooFactory.CreateFoo();
}
public void GetFoos()
{
foreach (var thing in everything)
{
var localFoo = GetFoo();
localFoo.UniqueProperty = "I am unique";
_fooList.Add(localFoo);
}
}
}
This was a bad question. I figured out this needed to be implemented higher up by the class controlling these implementations.

How to test Singleton class that has a static dependency

I have a Singleton class that uses the thread-safe Singleton pattern from Jon Skeet as seen in the TekPub video. The class represents a cached list of reference data for dropdowns in an MVC 3 UI.
To get the list data the class calls a static method on a static class in my DAL.
Now I'm moving into testing an I want to implement an interface on my DAL class but obviously cannot because it is static and has only one static method so there's no interface to create. So I want to remove the static implementation so I can do the interface.
By doing so I can't call the method statically from the reference class and because the reference class is a singleton with a private ctor I can't inject the interface. How do I get around this? How do I get my interface into the reference class so that I can have DI and I can successfully test it with a mock?
Here is my DAL class in current form
public static class ListItemRepository {
public static List<ReferenceDTO> All() {
List<ReferenceDTO> fullList;
... /// populate list
return fullList;
}
}
This is what I want it to look like
public interface IListItemRepository {
List<ReferenceDTO> All();
}
public class ListItemRepository : IListItemRepository {
public List<ReferenceDTO> All() {
List<ReferenceDTO> fullList;
... /// populate list
return fullList;
}
}
And here is my singleton reference class, the call to the static method is in the CheckRefresh call
public sealed class ListItemReference {
private static readonly Lazy<ListItemReference> instance =
new Lazy<ListItemReference>(() => new ListItemReference(), true);
private const int RefreshInterval = 60;
private List<ReferenceDTO> cache;
private DateTime nextRefreshDate = DateTime.MinValue;
public static ListItemReference Instance {
get { return instance.Value; }
}
public List<SelectListDTO> SelectList {
get {
var lst = GetSelectList();
lst = ReferenceHelper.AddDefaultItemToList(lst);
return lst;
}
}
private ListItemReference() { }
public ReferenceDTO GetByID(int id) {
CheckRefresh();
return cache.Find(item => item.ID == id);
}
public void InvalidateCache() {
nextRefreshDate = DateTime.MinValue;
}
private List<SelectListDTO> GetSelectList() {
CheckRefresh();
var lst = new List<SelectListDTO>(cache.Count + 1);
cache.ForEach(item => lst.Add(new SelectListDTO { ID = item.ID, Name = item.Name }));
return lst;
}
private void CheckRefresh() {
if (DateTime.Now <= nextRefreshDate) return;
cache = ListItemRepository.All(); // Here is the call to the static class method
nextRefreshDate = DateTime.Now.AddSeconds(RefreshInterval);
}
}
}
You can use the singleton based on instance(not based on static), for which you can declare interface like this.
public interface IListItemRepository
{
List<ReferenceDTO> All();
}
public class ListItemRepository : IListItemRepository
{
static IListItemRepository _current = new ListItemRepository();
public static IListItemRepository Current
{
get { return _current; }
}
public static void SetCurrent(IListItemRepository listItemRepository)
{
_current = listItemRepository;
}
public List<ReferenceDTO> All()
{
.....
}
}
Now, you can mock IListItemRepository to test.
public void Test()
{
//arrange
//If Moq framework is used,
var expected = new List<ReferneceDTO>{new ReferneceDTO()};
var mock = new Mock<IListItemRepository>();
mock.Setup(x=>x.All()).Returns(expected);
ListItemRepository.SetCurrent(mock.Object);
//act
var result = ListItemRepository.Current.All();
//Assert
Assert.IsSame(expected, result);
}
Which DI framework are you using? Depending on your answer, IOC container should be able to handle single-instancing so that you don't have to implement your own singleton pattern in the caching class. In your code you would treat everything as instanced classes, but in your DI framework mappings you would be able to specify that only one instance of the cache class should ever be created.
One way to test it would be if you refactor your ListItemReference by adding extra property:
public sealed class ListItemReference {
...
public Func<List<ReferenceDTO>> References = () => ListItemRepository.All();
...
private void CheckRefresh() {
if (DateTime.Now <= nextRefreshDate) return;
cache = References();
nextRefreshDate = DateTime.Now.AddSeconds(RefreshInterval);
}
}
And then in your test you could do:
ListItemReference listReferences = new ListItemReference();
listReferences.References = () => new List<ReferenceDTO>(); //here you can return any mock data
Of course it's just temporary solution and I would recommend getting rid of statics by using IoC/DI.

linq populate custom collection

I have a collection defined as:
public class MyCollection : List<MyItem>
{
...
}
public class MyItem
{
...
}
Using linq, I can use the Select method to return a IEnumerable, and I can call .ToList on that to get an IList but is there some way of getting back a type of MyCollection? Because I am tring to instantiate a class which has a property of type MyCollection, and I want to use object initialization.
For example:
public class MyClass
{
MyCollection TestCollection {get;set}
...
}
MyClass test = new MyClass()
{
...
TestCollection = SomeObject.Select(item => new MyItem()
{
...
}).ToList();
}
I get a compile error because ToList returns List and it can't cast to a MyCollection object. Is there a way to do that?
You'll need to construct your MyCollection instance with the IEnumerable<MyItem>. Add a constructor like:
public MyCollection(IEnumerable<MyItem> items) : base(items) {}
Then, when you go to use this, you can do:
TestCollection = new MyCollection(SomeObject.Select(item => new MyItem());
You could make your own extension method that builds your collection from an IEnumerable, like
public static class Extensions
{
public static MyCollection ToMyCollection(this IEnumerable<MyItem> items)
{
//build and return your collection
}
}
or if MyItem is a placeholder for generic types in your context :
public static class Extensions
{
public static MyCollection ToMyCollection<T>(this IEnumerable<T> items)
{
//build and return your collection
}
}
Just mimic ToList:
public static class MyCollectionExtensions {
public static MyCollection ToMyCollection(this IEnumerable<MyItem> source) {
if (source == null) throw new NullReferenceException();
return new MyCollection(source);
}
}
MyCollection needs a new constructor:
public class MyCollection : List<MyItem> {
public MyCollection() : base() { }
public MyCollection(IEnumerable<MyItem> source) : base(source) { }
...
}
Also, it's generally not advisable to expose a setter for a collection. You should encapsulate mutation of the collection inside the class:
public class MyClass {
public MyCollection TestCollection { get { ... } } // should return a read-only collection
public void Add(MyItem item) {
_testCollection.Add(item);
}
MyCollection _testCollection = ...;
...
}

Resources