[英]How to Access Class in Another Project (But in the Same Solution ) using C#.net (VS2012)
i am working first time on the three layer architecture, I've created three Projects in one Solution, Project one is named BLL , second is named DAL and third one is names Model , I've Created interface in Model, now want to Create Business Logics in BLL and want to connect it to the DAL where I've connected my Data Base. 我第一次在三层架构上工作,我在一个解决方案中创建了三个项目,一个名为BLL ,第二个名为DAL ,第三个名为Model ,我在模型中创建了接口,现在要创建BLL中的业务逻辑,并希望将其连接到我已连接数据库的DAL。
For this Purpose I've added Reference of each Project With other like I've Added Reference of BLL in Model and Added reference of BLL in DAL. 为了这个目的,我添加了每个项目的参考,其他像我在模型中添加了BLL的参考,并在DAL中添加了BLL的参考。
now for the time i've created a class is DAL in which i've Connected my DB and have alos Created a Windows Form in Model, 现在,我已经创建了一个类是DAL,其中我连接了我的数据库并且已经在模型中创建了一个Windows窗体,
the class in which I've connected DB is 我连接数据库的类是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient();
namespace WindowsFormsApplication3
{
class DB
{
public void fnc_ConnectoToDB(ref SqlConnection cn)
{
string strConnectionString = null;
strConnectionString = @"Data Source=AHSANMUGHAL;Initial Catalog=SurveyBuilder;User ID=sa;Password=ahsan";
cn = new SqlConnection();
cn.ConnectionString = strConnectionString;
cn.Open();
}
public void fnc_CloseCn(ref SqlConnection cn)
{
if (cn.State == ConnectionState.Open == true)
{
cn.Close();
}
}
}
}
i know this is bit confusing question but I hope you guys will understand it and will ans ASAP 我知道这是一个有点令人困惑的问题,但我希望你们能理解它并尽快解决
Try to use a dependency injection framework like ninject. 尝试使用像ninject这样的依赖注入框架。
Here is an example: 这是一个例子:
Your implementation: 你的实施:
public class Samurai {
public IWeapon Weapon { get; private set; }
public Samurai(IWeapon weapon)
{
this.Weapon = weapon;
}
}
And a module to give the Samurai it's weapon: 还有一个模块给武士它的武器:
public class WarriorModule : NinjectModule
{
public override void Load()
{
this.Bind<IWeapon>().To<Sword>();
}
}
Simple as that. 就那么简单。
first of all, you need to make your class public. 首先,你需要公开你的课程。
public class DB 公共类DB
Here is a simple example of Dependency Injection based on your classes. 以下是基于类的依赖注入的简单示例。
This doesnt utilize any IoC (Inversion of Control) containers that would make your instances easier to manage. 这不会利用任何IoC(控制反转)容器,这将使您的实例更容易管理。 I think its easier to conceptualize if you avoid the magic of an IOC container initially. 如果你最初避免使用IOC容器的魔力,我认为更容易概念化。 It also makes you glad to have it when you move up that rung. 当你向上移动时,它也会让你很高兴拥有它。
Ninject is an IoC container
mentioned in another answer, Unity is another. Ninject是另一个答案IoC container
提到的IoC container
, Unity是另一个。
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Model : Form
{
public Model()
{
InitializeComponent();
var bll = new BLL(new DAL());
bll.WriteToDatabase("mydbvalue");
}
}
public interface IBll
{
void WriteToDatabase(string value);
}
public class BLL: IBll
{
private IDal _dataLayer;
public BLL(IDal dataLayer)
{
_dataLayer = dataLayer;
}
public void WriteToDatabase(string value)
{
_dataLayer.WriteToDatabase(value);
}
}
public interface IDal
{
void WriteToDatabase(string value);
}
public class DAL:IDal
{
public void WriteToDatabase(string value)
{
fnc_ConnectToDB();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.