Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

oop - Clean smaller Objects

Trying to break up a large aggregate root and just want some help to see if I'm doing it correctly

Consider the following aggregate root "Team" which looks like the following


public class Team : IAggregateRoot
{
   public List<Player> Players {get; set;}
   
   // Lots of other properties

   public void AddPlayer(Player player){}

   public void RemovePlayer(string playerId){}

   public void MakePlayerCaptain(string playerId){}

   // More Methods
}

So to break that up and make the Team class smaller, I create a new class called Roster


public class Roster
{
   public List<Player> Players {get; set;}

   public void AddPlayer(Player player){}

   public void RemovePlayer(string playerId){}

   public void MakePlayerCaptain(string playerId){}
}

And Team now becomes

public class Team : IAggregateRoot
{
    public Roster Roster {get; set;}

   // Lots of other properties


   // More Methods
}

which makes Team smaller and gives me more cohesive models.

But this causes me to peek into the team aggregate root to access roster and make changes. i.e


team.Roster.MakePlayerCaptain()

Or I would just add the methods back onto team and they then call roster object? But then I'm back to having lots of methods (but smaller I guess) in my Team object.

What is the cleanest approach to take with this kind of thing or can anyone link me to some reading around this?

question from:https://stackoverflow.com/questions/65601383/clean-smaller-objects

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not an expert, but I think the best thing to do in terms of defensive programming would be to have a method in Team that calls the Roster... Roster could even be a subclass of Team if you have some REALLY defensive programming involved.

Team class:

public class Team : IAggregateRoot
{
    private Roster roster {get; set;} // Now private

    public void AddPlayer(Player player){roster.AddPlayer(player)} // call roster.AddPlayer() and do other stuff if you need to

    public void RemovePlayer(string playerId){roster.removePlayer(playerId)} // call roster.RemovePlayer() and do other stuff if you need to

    public void MakePlayerCaptain(string playerId){roster.MakePlayerCaptain(playerId)} // call roster.MakePlayerCaptain() and do other stuff if you need to

    private class Roster // Now a subclass of Team for defensive programming, assuming it will not be used outside of Team class
    {
       public List<Player> Players {get; set;}

       public void AddPlayer(Player player){}

       public void RemovePlayer(string playerId){}

       public void MakePlayerCaptain(string playerId){}
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...