开发者

Populate parent instance

I have a parent class, like this:

public abstract class Business<T> : IBusiness<T> where T : Entity
{
    protected Data<T> data;

    public IEnumerable<T> Select(DataContext db)
    {
        return data.Select(db);
    }
}

And I need to populate the instance of the "data" member on the child clas.

At the moment I'm doing this:

public class UserBusiness : Business<User>
{
    public UserBusiness()
    {
        dat开发者_运维技巧a = new UserData();
    }
}

I wonder if there are some other methods.

I also used an abstract function in the parent class link this:

protected abstract Data GetData();

To force the child class passing the instance.

Which is the best approach? Any suggestion?

Thanks in advance


create an abstract constructor:

public abstract class Business<T> : IBusiness<T> where T : Entity
{
    protected Data<T> data;

    Business<T>( T _data) 
    {
        data = _data
    }
    public IEnumerable<T> Select(DataContext db)
    {
        return data.Select(db);
    }
}

Then call it in the derived constructor:

public class UserBusiness : Business<User>
{
    public UserBusiness() :
        base (new UserData())
    {

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜