Microsoft Technologies Bidirectional এবং Unidirectional Association গাইড ও নোট

258

NHibernate তে, Association Mapping ব্যবহার করা হয় একাধিক টেবিলের মধ্যে সম্পর্ক স্থাপন করার জন্য। এটি মূলত One-to-One, One-to-Many, Many-to-One, এবং Many-to-Many সম্পর্কগুলোর মধ্যে কাজ করে। একাধিক সম্পর্কের মধ্যে Bidirectional এবং Unidirectional হল দুটি গুরুত্বপূর্ণ ধারণা, যা সম্পর্কের দিক নির্দেশ করে।


Unidirectional Association

Unidirectional Association হল এমন একটি সম্পর্ক যেখানে একটি ক্লাস থেকে অন্য ক্লাসের দিকে একমুখী (one-way) সম্পর্ক স্থাপন করা হয়। এই ধরনের সম্পর্কের মধ্যে, এক ক্লাসের মধ্যে অন্য ক্লাসের রেফারেন্স থাকবে, তবে বিপরীত দিক থেকে কোনো রেফারেন্স থাকবে না।

উদাহরণ: One-to-Many (Unidirectional)

ধরা যাক, আমাদের দুটি Entity Class রয়েছে - Department এবং Employee। এখানে, একটি Department এর অনেক Employee থাকতে পারে, কিন্তু Employee এর মধ্যে কোনো Department রেফারেন্স থাকবে না। এটি Unidirectional One-to-Many Association

public class Department
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    
    // Unidirectional association - Department has many Employees
    public virtual IList<Employee> Employees { get; set; }
}
public class Employee
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual int Age { get; set; }
    
    // No reference to Department in Employee class
}

এখানে Department ক্লাসের মধ্যে Employees নামক একটি লিস্ট রয়েছে, যা অনেক Employee কে ধারণ করে, তবে Employee ক্লাসের মধ্যে Department ক্লাসের কোনো রেফারেন্স নেই।

Mapping (XML - .hbm.xml)

<!-- Department Mapping -->
<class name="Department" table="Department">
    <id name="Id" column="Id">
        <generator class="identity"/>
    </id>
    <property name="Name" column="Name"/>
    
    <bag name="Employees" table="Employee">
        <key column="DepartmentId"/>
        <one-to-many class="Employee"/>
    </bag>
</class>

এখানে:

  • ট্যাগটি Employee গুলোর লিস্ট ধারণ করে।
  • দ্বারা Employee টেবিলের মধ্যে DepartmentId কলামটি সংযুক্ত হয়, যা তাদের সম্পর্ক স্থাপন করে।

Bidirectional Association

Bidirectional Association হল এমন একটি সম্পর্ক যেখানে দুটি ক্লাস একে অপরকে রেফারেন্স করে। এটি two-way relationship এর মতো কাজ করে, যেখানে এক ক্লাসের রেফারেন্স অন্য ক্লাসে এবং সেই ক্লাসের রেফারেন্স প্রথম ক্লাসে থাকে।

উদাহরণ: One-to-Many (Bidirectional)

এখানে, Department এবং Employee এর মধ্যে Bidirectional সম্পর্ক স্থাপন করা হয়েছে, অর্থাৎ Employee ক্লাসের মধ্যে Department রেফারেন্স থাকবে, এবং Department ক্লাসের মধ্যে Employees লিস্ট থাকবে।

public class Department
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }

    // Bidirectional association - Department has many Employees
    public virtual IList<Employee> Employees { get; set; }
}
public class Employee
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual int Age { get; set; }
    
    // Bidirectional association - Employee has one Department
    public virtual Department Department { get; set; }
}

এখানে, Employee ক্লাসে Department রেফারেন্স থাকছে, এবং Department ক্লাসে Employees রেফারেন্স আছে, ফলে এটি একটি Bidirectional One-to-Many সম্পর্ক।

Mapping (XML - .hbm.xml)

<!-- Department Mapping -->
<class name="Department" table="Department">
    <id name="Id" column="Id">
        <generator class="identity"/>
    </id>
    <property name="Name" column="Name"/>
    
    <bag name="Employees" table="Employee">
        <key column="DepartmentId"/>
        <one-to-many class="Employee"/>
    </bag>
</class>

<!-- Employee Mapping -->
<class name="Employee" table="Employee">
    <id name="Id" column="Id">
        <generator class="identity"/>
    </id>
    <property name="Name" column="Name"/>
    <property name="Age" column="Age"/>
    
    <!-- Reference to Department -->
    <many-to-one name="Department" column="DepartmentId" class="Department"/>
</class>

এখানে:

  • Department ক্লাসে Employees এর সাথে bag ব্যবহৃত হয়েছে এবং DepartmentId দিয়ে এটি Employee টেবিলের সাথে যুক্ত করা হয়েছে।
  • Employee ক্লাসে many-to-one ব্যবহার করে Department এর রেফারেন্স রাখা হয়েছে, যেখানে DepartmentId কলামটি Employee টেবিলের সাথে যুক্ত।

Bidirectional এবং Unidirectional এর মধ্যে পার্থক্য

  • Unidirectional Association-এ সম্পর্কটি শুধুমাত্র একদিকে থাকে (যেমন, Department এর মধ্যে Employees থাকে, কিন্তু Employee এর মধ্যে Department রেফারেন্স নেই)।
  • Bidirectional Association-এ সম্পর্ক দুটি দিক থেকেই কাজ করে (যেমন, Employee এর মধ্যে Department এবং Department এর মধ্যে Employees রেফারেন্স থাকে)।

Bidirectional সম্পর্ক অধিকাংশ সময় ব্যবহার করা হয় যখন আপনি দুইটি Entity এর মধ্যে উভয় দিকের সম্পর্ক চান এবং একে অপরের সাথে যোগাযোগ করতে চান। তবে, Unidirectional সম্পর্ক সাধারণত ব্যবহার করা হয় যখন আপনি শুধুমাত্র এক দিকে সম্পর্ক রাখতে চান এবং বিপরীত দিকটি দরকার নয়।


এই দুই ধরনের সম্পর্ক ব্যবহারের মাধ্যমে আপনি NHibernate এর সাথে আরও জটিল ডেটাবেস স্ট্রাকচার ডিজাইন করতে পারবেন, যা ডেটা রিলেশনশিপ সহজ এবং পরিষ্কারভাবে ম্যানেজ করতে সাহায্য করে।

Content added By
Promotion

Are you sure to start over?

Loading...