user348173
user348173

Reputation: 9278

Organization of business objects and code duplication

I have tabs on the website: Cinema, Concerts, Clubs, Theatres, Children, Other. Each tab displays the appropriate events and schedules. This is my object:
For Cinema:
Film, Cinema, CinemaSchedule
Theater:
Performance, Theater, TheaterSchedule
and so on.

Now consider Concert:
Concert, ConcertPlace, ConcertPlaceSchedule.
But this is not correct, because the concert may be in the club or in the theater (for example). The same situation is with the clubs. There are different events take place: concerts and parties.

So, What is the best way to organize classes? Another example: we have the film "Puss in Boots". On the one hand, this film, the other is a cartoon(Children tab). Now that the film appeared in the "Movies" and "Children" need to create it twice.

Another problem, I will show on example.

This is my repository for `Film`:

    public class FilmRepository:BaseRepository<Film>
        {
            public FilmRepository(DatabaseContext database) : base(database)
            {
            }
                /// <summary>
                /// Loads films for specific period
                /// </summary>
                /// <param name="period"></param>
                /// <returns></returns>
                public IList<Film> GetFilmsForPeriod(PeriodEvent period)
                {
                    switch (period)
                    {
                            case PeriodEvent.All:
                            return GetAllFilms();

                            case PeriodEvent.Today:
                            return GetFilmsForToday();

                            case PeriodEvent.Tomorrow:
                            return GetFilmsForTomorrow();

                            case PeriodEvent.Week:
                            return GetFilmsForWeek();

                            case PeriodEvent.FewWeek:
                            return GetFilmsForFewWeek();

                            case PeriodEvent.Month:
                            return GetFilmsForMonth();

                            case PeriodEvent.FewMonth:
                            return GetFilmsForFewMonth();

                        default:
                            return GetFilmsForToday();
                    }
                }

            // load films for today
            private IList<Film> GetFilmsForToday()
            {
                return
                    Database.Films.Where(c => c.CinemaSchedules.Any(s =>
                       s.ShowDate.Value.Date == DateTime.Now.Date)).ToList();
            }

           //implementation another methods from above.
    }

And each repository has identical methods. One difference in that instead CinemaSchedule is TheaterSchedule (or ClubSchedule, and so on).

How to avoid this duplication?

Thanks, and sorry for my english.

Upvotes: 1

Views: 112

Answers (3)

Archeg
Archeg

Reputation: 8462

Cinema, Theater and Club shold have the same parent that has IList that returns schedules for you. The same is for repository, it seems that you can have GetFilmsForPeriod changed to GetObjectsForPeriod and have it moved to BaseRepository. Both Club and Theater can have IList Concerts. Try to merge some similar properties of your objects into parents to avoid double-coding.

As Film is a class you can have it both in Movies and Children as it is reference class.

Upvotes: 2

Thomas Lindvall
Thomas Lindvall

Reputation: 599

My first idea, is that you should have an base object called Event (or something similar just brain storming here) and that one will have objects that extends it to being a Cinema Event, Concert Event and so on building upon the OCP.

On your second (or was it first? Too little coffee too much time) question I don't see anything wrong with having a table that connects the specified Event with several categories.

Upvotes: 1

nickgrim
nickgrim

Reputation: 5437

Feels like you need to model it a different way.

Perhaps you could have a Venue object which represents a physical location that stuff happens in (with subclasses for e.g. Cinema, Theatre), an Event object that represents that stuff (e.g. Film, Play) and a Performance which represents a join of those two models (and so will have two foreign-keys, and probably a field for time).

Upvotes: 3

Related Questions