none
How to share a class in two different project RRS feed

  • Question

  • I have a class which should be shared from two different process.

    My project has two major separate process(front & backend process)

    I need to share a List<appointment> from those two process.

    Each process has it's own project.

    I'd like to put codes which have List<appointment> in a class and share in two separate process.

    (I mean I don't want codes are duplicated in two process)

    What is the optimal way to design class in this case?

    Sunday, March 17, 2019 3:27 PM

Answers

  • Hi Jeff0803,

    Thank you for posting here.

    A simple way for your reference. Create a class with the list you want to share. And then add reference of the project with the sharing class.

    ConsoleApp1 project

    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
            }
        }
        public class SharingClass
        {
            public List<Appointment> appointments { get; set; }
        }
        public class Appointment
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }


    ConsoleApp2 project

    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                ConsoleApp1.SharingClass sharingClass = new ConsoleApp1.SharingClass();
                var list = sharingClass.appointments;
            }
        }
    }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Wednesday, March 20, 2019 5:14 PM
    Monday, March 18, 2019 3:01 AM
    Moderator

All replies

  • From your Solution folder, you can create three subfolders: project A, project B and Common Classes. Place your class in the "Common classes" subfolder, and then from the other two projects select the option to "add existing item". Navigate to the common class, but don't press the "Add" button. Instead, look at a small arrow that expands the options and lets you select "add as a link". When you select add as a link, you get a special entry in the .csproj file that tells the compiler to compile the class from the original folder, without making a copy in the project folder.

    Screenshot

    Sunday, March 17, 2019 4:14 PM
    Moderator
  • My project has two major separate process(front & backend process)

    You should be looking at a Layered style that is like n-tier, which can be two layers.

    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ee658117(v=pandp.10)

    https://www.codeproject.com/Articles/36847/Three-Layer-Architecture-in-C-NET

    I'd like to put codes which have List<appointment> in a class and share in two separate proces

    List<appointment> seems to an object that is carrying data. It should be called DtoAppointment.

    https://en.wikipedia.org/wiki/Data_transfer_object

    <copied>

    A data transfer object (DTO) is an object that carries data between processes.

    <end>

    https://www.codeproject.com/Articles/1050468/Data-Transfer-Object-Design-Pattern-in-Csharp

    So, you create a classlib project call it Entities, or you can call it anything you like. You create a class named DtoAppointment in the Entities project. The classlib project makes an Entities.DLL. 

    You set reference to the Entities.DLL in both projects,  and both projects will know about DtoAppointment, which is in one spot the Entities.DLL that is shared between the two projects.

    Sunday, March 17, 2019 6:53 PM
  • Hi Jeff0803,

    Thank you for posting here.

    A simple way for your reference. Create a class with the list you want to share. And then add reference of the project with the sharing class.

    ConsoleApp1 project

    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
            }
        }
        public class SharingClass
        {
            public List<Appointment> appointments { get; set; }
        }
        public class Appointment
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }


    ConsoleApp2 project

    namespace ConsoleApp2
    {
        class Program
        {
            static void Main(string[] args)
            {
                ConsoleApp1.SharingClass sharingClass = new ConsoleApp1.SharingClass();
                var list = sharingClass.appointments;
            }
        }
    }

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Jeff0803 Wednesday, March 20, 2019 5:14 PM
    Monday, March 18, 2019 3:01 AM
    Moderator
  • Hi,

    how DA924x said, you can use a third project for code used by both other projects.

    But I would add reference of the project instead of the dll, like Wendy Zang said.

    Greetings,

    Chris


    • Edited by DerChris88 Monday, March 18, 2019 10:50 PM
    Monday, March 18, 2019 5:43 PM