0

I am creating a Console App in C# using VS2010. It is based in 3-Layer Architecture containing three layers

  • PMS.UI
  • PMS.DAL
  • PMS.BL

To remove Circular Dependency between PMS.DAL and PMS.BL I added an extra layer PMS.Service.

  1. I created a Vehicle class in PMS.BL which implements interface IVehicle from PMS.Service.
  2. I added reference of PMS.Service in both DAL and BL.
  3. Now UI calls AddNewVehicle() method of Vehicle class of BL which implements IVehicle
  4. BL calls AddNewVehicle(IVehicle obj) method of VehicleDao in PMS.DAL...

All working fine but at time of build Compiler says to add reference of PMS.Service in PMS.UI.

PMS.UI doesn't implement any interface of PMS.Service but calls AddNewVehicle() method of Vehicle class of PMS.BL which implements IVehicle.

Is it necessary to add reference of PMS.Service to PMS.UI only if it creates instance of Vehicle Class of PMS.BL which implements IVehicle present in PMS.Service..

Please help me I am new to use Interface in c#...

Thankyou Guys for your answers but i am still confused. I will present my code here.I have added all four layers as different c sharp class library(different layers).

1)PMS.UI(Added reference of PMS.BL)
Program.cs
using System;
using PMS.BL;
namespace PMS.APP
{
    class Program
    {
        static void Main()
        {
            var vBo = new VehicleBo();//Compiler Says Add reference of PMS.Service here.Why is it necessary to add Reference of it??
            vbo.VehicleNumber = "BA1PA 1212";
            vbo.VehicleType = "Bike";
            vbo.SaveNewVehicle();
        }
    }
}

2)PMS.BL(Added reference of PMS.DAL and PMS.Service)
VehicleBO.cs

using PMS.DAL;
using PMS.Service;
namespace PMS.BL
{
    public class VehicleBo : IVehicle
    {
        public string VehicleNumber { get; set; }
        public string VehicleType { get; set; }
        public void SaveNewVehicle()
        {
            var vDao = new VehicleDao();
            vDao.SaveNewVehicle(this);
        }
    }
}

3)PMS.DAL(Added reference of PMS.Service)
using PMS.Service;
namespace PMS.DAL
{
    public class VehicleDao
    {
        public void SaveNewVehicle(IVehicle obj)
        {
            //code to insert in database
        }
    }
}

4)PMS.Service
IVehicle.cs
namespace PMS.Service
{
    public interface IVehicle
    {
        string VehicleNumber { get; set; }
        string VehicleType { get; set; }
        void SaveNewVehicle();
    }
}
rriwaj
  • 11
  • 2
  • 1
    Please add pieces of code where you call `AddNewVehicle()` method of `Vehicle` class in PMS.UI? It is critical whether you call it on instance of `Vehicle` class or `IVehicle` interface. – Andrii Kalytiiuk Nov 24 '13 at 09:46
  • i have added code above Andrii Kalytiiuk please review it.. – rriwaj Nov 25 '13 at 02:51
  • You need references in PMC.UI for PMS.Service because PMS.UI references PMS.BL which defines functions like `SaveNewVehicle(IVehicle obj)` where `IVehicle` is defined in PMS.Service. So to ensure that all calls from PMS.BL will be valid in PMS.UI - you need PMS.Service to be referenced in PMS.UI together with PMS.BL (otherwise invocation of `SaveNewVehicle(IVehicle obj)` will be invalid as PMS.UI will not know anything about `IVehicle`). – Andrii Kalytiiuk Nov 25 '13 at 10:41
  • Is this a best practice ?? or there are other alternatives to it – rriwaj Nov 25 '13 at 11:06
  • It is just how .NET framework and Visual Studio works. There are a lot of alternatives but that implies change of your application architecture and belongs to *Design Patterns* topic and that greatly exceeds scope of one comment or one question. – Andrii Kalytiiuk Nov 25 '13 at 11:15

2 Answers2

0

With the given details (and no code). This is what I understand.

PMS.Service (IVehicle.cs)
PMS.BL (Vehicle : IVehicle)

In this scenario, if you are exposing Vehicle, you will have to add reference to PMS.Service also. In any case, having model interfaces/contact in service implementation does not look right. I would rather consider creating PMS.Contracts and have my model/service contracts there.

Hope that helps.

Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
  • PMS.Service contains only interface which are implemented in PMS.BL why do i need to add reference to PMS.Service if it only contains interface which is not depended to other. – rriwaj Nov 25 '13 at 04:30
0

I think that you have an architecture problem. Basically, if you are in three layer, this is the good way :

IHM => BLL => DAL
Core

Core is a project contains tools function (format date, number etc.) and your interface.

The dependencies : IHM reference BLL / BLL reference DAL. An all of these reference Core. Core have no dependency.

I'm beginner like you with interface. Here the way i'll choose if i have to do it :

4 projects :

  1. Core
  2. BLL (depecencies DAL - Core)
  3. DAL (depecencies Core)
  4. IHM (depecencies BLL - Core)

In Core : Two things : An Interface IVehicle and a class that implement this class call Vehicle

Because we need to use a DAL, i don't know how to do for not use Core.Vehicle. An abstract class is not good because if DAL need to return a "IVehicule" object, we need to implement an object and we can't implement an Abstract or Interface.

In BLL : Two objects : Car and Truck that implement Core.Vehicule

In DAL : One object : Vehicule with a method for return a Core.Vehicule

In IHM : A call of BLL.Car

And it's doing the thing...

EDIT : I've post a question like yours : POO and Interface (in C#)

Hope it help you.

Community
  • 1
  • 1
Portekoi
  • 1,087
  • 2
  • 22
  • 44