简体   繁体   中英

collection of derived class

I'm refactoring my code of separation of concern (SoC). Move non-AutoCAD code away to separate dll, so Common, Data Access, web service, AutoCAD related, … in different dll files.

Here is sample of my class:

// non-AutoCAD dll
public class Loc
{
    public int Id;
    public string Name;
    ...
}

// AutoCAD dll
public class AcadLoc : Loc
{
    public ObjectId oid;
}

// non-AutoCAD dll
public class Locs : List<Loc>
{
    public Loc Get_By_Id(long id)
    {
        return this.SingleOrDefault(n => n.Id == id);
    }
}

// AutoCAD dll
public class AcadLocs : Locs // or List<AcadLoc> ? 
{
}

My question is, can I run something like

AcadLocs myAcadLocs = new AcadLocs();
AcadLoc myAcadLoc = myAcadLocs.Get_By_Id(1);   // How to make this works? 

I tried:

public class AcadLocs : Locs // or List<AcadLoc> ? 
{
    public AcadLoc Get_By_Id(int id)
    {
        return this.SingleOrDefault(n => n.Id == id);
    }
}

it would not works.

Now I change to this:

public interface ILocData<T> where T : new() {} 

// non-AutoCAD dll
public class Loc : ILocData<Loc>
{
    public int Id;
    public string Name;
    ...
}

// AutoCAD dll
public class AcadLoc : Loc, ILocData<AcadLoc>
{
    public ObjectId oid;
}

public class LocDataCollection<T, TCollection> : List<T>
    where T : ILocData<T>, new()
    where TCollection : LocDataCollection<T, TCollection>, new()
{
}

// non-AutoCAD dll
public class Locs : LocDataCollection<Loc, Locs>
{
    public Loc Get_By_Id(int id)
    {
        return this.SingleOrDefault(n => n.Id == id);
    }

    public bool Check()
    {
        foreach (Loc loc in this)
        {
        ....
        }
    }
}

// AutoCAD dll  
public class AcadLocs : LocDataCollection<AcadLoc, AcadLocs>
{
    public AcadLoc Get_By_Id(int id)
    {
        return this.SingleOrDefault(n => n.Id == id);
    }
}

Now,

AcadLoc myAcadLoc = myAcadLocs.Get_By_Id(1);   // works

but

myAcadLocs.Check();  // failed

Thanks

Wes

I think what you need is extension method

public static class MyExtension 

{
    public static Loc Get_By_Id(this Locs l,int id) 
    {
        return l.SingleOrDefault(n => n.Id == id);
    }

    public static bool Check(this Locs l)
    {
        foreach (Loc loc in l)
        {
        ....
        }
    }
}

Once you have this code in place then just call this methods like this

 myAcadLocs.Get_By_Id(1);
 myAcadLocs.Check();
 myLocs.Get_By_Id(1);
 myLocs.Check();

I refer to your first attempt.

1. Inheritance

public class AcadLocs : Locs // or List<AcadLoc> ? 

If you want to derive from Locs or List<AcadLoc> depends on how you want to use this class. If you want to declare common members or if it is necessary that you can assign an instance of AcadLocs to a variable of type Locs like this

Locs locs = new AcadLocs();

then you surely need to derive from Locs .

2. Get_By_Id

For this method I would use the OfType<> method like this:

public class AcadLocs : Locs // or List<AcadLoc> ? 
{
    public AcadLoc Get_By_Id(int id)
    {
        return this.OfType<AcadLoc>().SingleOrDefault(n => n.Id == id);
    }
}

OfType<AcadLoc> creates an IEnumeralbe<AcadLoc> out of the existing sequence selecting only elements that are of the specified type ( AcadLoc ). This will help you with your Check method, too.

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