Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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,

Now my questions are

1)how can I access classes of DAL in BLL and BLL's in Model

2)and which logic I've to create (in BLL) to Access Database through BLL

the class in which I've connected DB is

DB.cs

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

like image 958
Ahsan Hussain Avatar asked Oct 02 '22 19:10

Ahsan Hussain


2 Answers

first of all, you need to make your class public.

public class DB

like image 70
manit Avatar answered Oct 12 '22 23:10

manit


Try to use a dependency injection framework like 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.

like image 20
Kees de Wit Avatar answered Oct 12 '22 23:10

Kees de Wit