1

I have a solution that has multiple projects. There is a MAIN project that I want to be the only external interface for my solution. In it I have provided methods which will fire off code and return results from one of the included projects. So far no problem for the test app to call the methods without including a reference to the other internal projects.

However I am making use of custom classes in my calls. these classes live in one of the internal projects and are used to pass data to the methods or get a custom response from a method or raise a custom error. These classes reside in one of the internal Projects. My problem is I cannot figure out how to get a reference to the required custom classes in the calling application without including a reference to that project the class resides in.

As an example here is a method that is made available to the consumer through the main_Project:

  public class UtilitiesController
{

    public static ShapeVerifyResponse VerifyShape(int shapeId, float[] dimensions)
    {
        ShapeVerifyResponse result = null;
        IValidateShape worker = new clsGraphics();

        try
        {                
            result = worker.VerifyShape(shapeId, dimensions);
            return result;
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (result != null)
            {
                result = null;
            }
            if (worker != null)
            {
                worker = null;
            }
        }  

    }

The ShapeVerifyResponse is defined in another internal project that contains all the models and interfaces I am using. Here is that class:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBV2_DomainClasses.Models
{
    public class ShapeVerifyResponse
    {
        public float Width { get; set; } // Width of shape
        public float Height { get; set; } // Height of shape
        public Boolean edgework { get; set; } // is edgework allowed
        public ArrayList  errorlist { get; set; } // list of errors

    }
}

How would I code a property in the main project that returns the class of the models project to the consumer application?

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
Dan Rowe
  • 141
  • 1
  • 4
  • 15
  • 2
    "My problem is I cannot figure out how to get a reference to the required custom classes in the calling application without including a reference to that project the class resides in." Well no, you can't. You can't expect a client to know about a type without knowing about the assembly containing that type. – Jon Skeet Jul 08 '16 at 20:08
  • So shouldn't I be able to provide that access through a property of my DLL? – Dan Rowe Jul 08 '16 at 20:13
  • I don't know what you mean by that. But basically, the client will need to add a reference to the assembly containing `ShapeVerifyResponse`. – Jon Skeet Jul 08 '16 at 20:14
  • Or the assemblies can be loaded at runtime, and reflection can be used to access the internal classes http://stackoverflow.com/questions/1259222/how-to-access-internal-class-using-reflection (thanks Jon). You still need to know something about the classes like their member names. Implement an accessible interface to guarantee you do – djv Jul 08 '16 at 20:17
  • What I mean is I would like to have a reference to the shapeVerifyResponse through the Main project which is the DLL interface. Is there a way to provide a property to the consumer that has an instance of the shapeVerifyResponse? Other than moving the class to the main project? – Dan Rowe Jul 08 '16 at 20:18
  • 1
    The internal class would need to implement an interface which can be made public through the API. You can either have the internal project reference the main project, or have another "support" dll which is also referenced by the consumer. In either case, this can be accomplished with reflection, and you would also need to provide the internal project's dll (and support dll if you do it that way). – djv Jul 08 '16 at 21:28

1 Answers1

0

You should move all classes that represents data for request and response to the MAIN module. You should also create interfaces in MAIN module that will specify API for submodules. In submodules reference MAIN module and implement interfaces.

In this design submodules only implement functionality that MAIN needs, all data structures and interfaces are in MAIN. If some interface should not be visible to the client code you should make it internal and use InternalsVisibleTo attribute on assembly level to share it with other submodules of your library but not with client code (more on that Make internal classes visible to others assemblies).

Community
  • 1
  • 1
csharpfolk
  • 4,124
  • 25
  • 31