0

It's a beginner question and might be super easy for most of you experts.

I'm trying to find the correct architecture/structure for a case study project (let's say a building construction sample project).

I'm trying to define a team for a project which is identical through all project phases and referenced and its members are referenced and used in different phases.

I've created a Person class which defines required fields and used it in my ProjectTeam class which is a generic List<Person>.

  1. I want to know how to use this ProjectTeam class in other classes which are going to define phases in my project? I'm thinking of a static class, but I don't know how to use it (p1.png).

  2. Should I follow the concept of one project with different classes, or it's better to follow the concept of one solution, with a class library used with different projects (representing different phases) in my solution. I've graphically shown the second idea in the not-a-UML diagram (p2.png).

My classes: my classes

Solution architecture diagram: solution architecture diagram

namespace MyClassLibrary
{    public class Person
    {        public enum ERole { Admin, Head, InCharge }
        public string Name { get; set; }
        public ERole Role;
        private decimal rate = 15.00m;
        public decimal Rate
        {   get { return rate; }
            set{ if (rate >= 15.00m) { rate = value; } }
        }
        public Person()
        {
            this.Name = "";
            this.Role = ERole.InCharge;
            this.Rate = 15.00m;
        }
    }
}

namespace MyClassLibrary
{
    public static class ProjectTeam
    {
        public static List<Person> TeamMembers = new List<Person>();
        static ProjectTeam()
        {
            TeamMembers.Add(new Person() { Name = "APerson",
                                           Role = Person.ERole.Admin,
                                           Rate = 30.00m });

            TeamMembers.Add(new Person() { Name = "BPerson",
                                           Role = Person.ERole.Head,
                                           Rate = 25.00m });

            TeamMembers.Add(new Person() { Name = "CPerson",
                                           Role = Person.ERole.InCharge,
                                           Rate = 15.00m });

        }

    }
}


namespace MyClassLibrary
{
    public class Project
    {
        public string ProjectName { get; set; }
        public int ProjectNumber{ get; set; }
        public string ProjectScope { get; set; }


    }
}
arash deilami
  • 25
  • 1
  • 1
  • 7
  • 1
    Thank you for taking the time to share your problem. But there is something missing from your question. What is your goal? What is your issue? What have you done so far? Please try to better explain your difficulty and share more code. To help you improve the content, title and tags of your query, consider reading the *[How do I ask a good question](https://stackoverflow.com/help/how-to-ask)* which is in the help center. –  Oct 03 '19 at 19:31
  • Hi Oliver, Sorry if it's inconvenient. I have received a comment to post the code instead of its picture and shared what I had. – arash deilami Oct 03 '19 at 19:39
  • Hi Oliver, Sorry if it's inconvenient. I have received a comment to post the code instead of its picture and shared what I had.I'll go through the link you mentioned. I have two goals and maybe that's why it's causing confusion. First of all, I'm trying to find out the best common practice dealing with a project with so many parts and pieces. Second, I want to know how to use a single identical instance of "ProjectTeam()" class through the project. – arash deilami Oct 03 '19 at 19:45
  • For a single instance of a class, the pattern is the [singleton](https://stackoverflow.com/questions/12316406/thread-safe-c-sharp-singleton-pattern). But what do you mean by "dealing with a project with so many parts and pieces" ? –  Oct 03 '19 at 19:53
  • I mean different classes with different members representing different tasks and phases of my sample project. – arash deilami Oct 03 '19 at 20:08
  • By the way, Thanks for the SINGLETON pattern. I believe it solves my question as a standard pattern in the same cases. Appreciate it. – arash deilami Oct 03 '19 at 20:41

3 Answers3

1

You can use static class for person in projects like below

public static class Team
{

    public static List<Person> GetPersons()
    {
        var person = new List<Person>();
        person.Add(new Person() { Name = "testPerson" });
        return person;
    }

}
public class Person
{
    public string Name { get; set; }
}
public class Project
{
    public string Name { get; set; }
    public string Scope { get; set; }
    public List<Person> Person
    {
        get
        {

            return Team.GetPersons();
        }
    }
}
saif iqbal
  • 219
  • 1
  • 9
0

Try using singleton. This is pattern, well known and very usefull. Essentially, a singleton is a class which only allows a single instance of itself to be created, and usually gives simple access to that instance. Implementation looks like this:

    public sealed class Singleton
{
    private static Singleton m_oInstance = null;
    private int m_nCounter = 0;

    public static Singleton Instance
    {
        get
        {
            if (m_oInstance == null)
            {
                m_oInstance = new Singleton();
            }
            return m_oInstance;
        }
    }

    public void DoSomething()
    {
        Console.WriteLine("Hello World {0}!", m_nCounter++);
    }

    private Singleton()
    {
        m_nCounter = 1;
    }
}

And then you can access it that way:

Singleton.Instance.DoSomething();
cebilon123
  • 551
  • 1
  • 4
  • 7
  • Your singleton implementation is flawed. It's not thread safe and it uses misleading naming conventions. – dymanoid Oct 03 '19 at 19:21
  • You can easily fix the thread-safety issue by moving the instance creation into the static constructor. – pikausp Oct 03 '19 at 19:26
0

If you have 3 different projects you want to reference the DLL, do the following:

1 - in solution explorer, under each project you want to use the Library in... right click the References node and click "Add Reference"

2 - On left side of Reference Manager that comes up, click "Projects"

3 - Click the checkbox for the Library project. This adds a reference of your Library to the project that you want to consume that library.

4 - Now inside your project you want to use the ProjectTeam class, add this at top of the .cs file:

using MyClassLibrary;

5 - now in one of your functions you can add this code to use that ProjectTeam class:

ProjectTeam.TeamMembers.Add(new Person......)

6 - Then do some research on difference between static and non-static functions.

CarCar
  • 680
  • 4
  • 13