C# Structure - Access class from external library

2019-05-25 20:11发布

I have a question regarding correct way to structure my application. I am a beginner on C# so excuse if I have some basis missing. I looked on the web(not only stackoverflow) but didn't find any answer, so maybe this is just because I am doing something wrong?

I have a project in which there are several classes to define my objects.

Object1.class.cs

Object2.class.cs ...

Now for some functions, I created class libraries (.dll files) to externalise the code. The idea is if I need to make an update, I just need to replace .dll file, and not all the code.

But I meet a problem when I want to send objects to that class, I think because they are external to the solution?

So I begin to wonder if I do it correctly.

1) Is there a way to send an object from my project to a class I created separately?

2) Is this a good idea to work this way? I mean, I could of course add the library in my project, but in that case I will not have separated .dll files, and all get in a single .exe file.

3) When I need to test my class, I always need to close my project, and open the class to edit it, then come back to my project to test it. Is there an easier way to do it?

Thanks in advance for your advises

Edit 2.

Simone Cifani perfercly answered to my question :

1) Classes must be externalised

2) Add references to necessary classes in my external libraries

3) Add references to each class and external libraries in my main project

As I don't know how to use Interfaces I will do without it, but I think all will be fine even without.

1条回答
混吃等死
2楼-- · 2019-05-25 20:50

A very common way to externalize part of the code is by using interfaces.

A simple example is:

//CalculatorInterface.dll
namespace Calculator.Interface
{
    interface ICalculator
    {
        int DoCalculus(List<object> list);
    }
}

//CalculatorImplementation_1.dll
//  Add a reference to CalculatorInterface.dll
namespace Calculator.Implementation
{
    using Calculator.Interface;

    class CalculatorImplementation_1 : ICalculator
    {
        public int DoCalculus(List<object> list)
        {
            int result = 0;

            foreach (Object obj in list)
            {
                if (obj is int)
                {
                    result += (int)obj;
                }
            }

            return result;
        }
    }
}

//Calculator.dll
//  Add a reference to CalculatorInterface.dll
//  Add a reference to CalculatorImplementation_1.dll (or dynamically load it)
namespace Calculator
{
    using Calculator.Interface;
    using Calculator.Implementation;

    class MyCalculator
    {
        void CalculateSomething()
        {
            List<Object> list = new List<Object>();
            list.Add(1);
            list.Add(2);
            list.Add("SomeString");

            ICalculator calculator = new CalculatorImplementation_1();

            int result = calculator.DoCalculus(list);

            Console.Write(result);
        }
    }
}
查看更多
登录 后发表回答