[英]Add reference Error in c# console app
I am creating a Console App in C# using VS2010. 我正在使用VS2010在C#中创建控制台应用程序。 It is based in 3-Layer Architecture containing three layers 它基于三层体系结构,包含三层
To remove Circular Dependency between PMS.DAL and PMS.BL I added an extra layer PMS.Service. 为了删除PMS.DAL和PMS.BL之间的循环依赖关系,我添加了一个额外的层PMS.Service。
Vehicle
class in PMS.BL which implements interface IVehicle
from PMS.Service. 我创建了一个Vehicle
在PMS.BL类,它实现接口IVehicle
从PMS.Service。 AddNewVehicle()
method of Vehicle
class of BL which implements IVehicle
现在,UI调用BL的Vehicle
类的AddNewVehicle()
方法,该方法实现了IVehicle
AddNewVehicle(IVehicle obj)
method of VehicleDao in PMS.DAL... BL在PMS.DAL中调用AddNewVehicle(IVehicle obj)
方法... All working fine but at time of build Compiler says to add reference of PMS.Service in PMS.UI. 一切正常,但在构建时,编译器表示要在PMS.UI中添加对PMS.Service的引用。
PMS.UI doesn't implement any interface of PMS.Service but calls AddNewVehicle()
method of Vehicle class of PMS.BL which implements IVehicle
. PMS.UI没有实现PMS.Service的任何接口,而是调用实现IVehicle
的PMS.BL的Vehicle类的AddNewVehicle AddNewVehicle()
方法。
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.. 仅当PMS.UI创建实现IVehicle
存在的IVehicle的Vehicle
Class of PMS.BL的实例时,才有必要向PMS.UI添加对PMS.Service的引用。
Please help me I am new to use Interface in c#... 请帮助我,我是刚在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). 我将在这里展示我的代码。我已经将所有四层添加为不同的c尖锐类库(不同的层)。
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();
}
}
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. 在这种情况下,如果要公开Vehicle,则还必须添加对PMS.Service的引用。 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. 我宁愿考虑创建PMS.Contracts并在那里拥有我的模型/服务合同。
Hope that helps. 希望能有所帮助。
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. 依赖项:IHM参考BLL / BLL参考DAL。 An all of these reference Core. 所有这些都参考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 : 4个项目:
In Core : Two things : An Interface IVehicle and a class that implement this class call Vehicle 在核心中:两件事:接口IVehicle和实现该类的类称为Vehicle
Because we need to use a DAL, i don't know how to do for not use Core.Vehicle. 因为我们需要使用DAL,所以我不知道如何不使用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. 抽象类不好,因为如果DAL需要返回“ IVehicule”对象,则需要实现一个对象,而不能实现Abstract或Interface。
In BLL : Two objects : Car and Truck that implement Core.Vehicule 在BLL中:两个对象:实现Core.Vehicule的Car和Truck
In DAL : One object : Vehicule with a method for return a Core.Vehicule 在DAL中:一个对象:具有返回Core.Vehicule的方法的车辆
In IHM : A call of BLL.Car 在IHM中:BLL.Car的调用
And it's doing the thing... 它在做...
EDIT : I've post a question like yours : POO and Interface (in C#) 编辑:我已经发布了一个像您一样的问题: POO和接口(在C#中)
Hope it help you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.