Using Unity framework in .net MVC

Here I will just go about the approach needs to be taken when learning to use unity framework. Download the file  . This files explains from basics how and why to use unity.

After learning this. Add Nuget Project Unity.MVC in your App.
Once added It will add few dlls as below
It will add a file as below
 App_Start=> UnityMvcActivator.cs

In the UnityWebActivator.Start method add a method call at the end.
RegisterTypes(container);


also create a method as below
public static void RegisterTypes(IUnityContainer container)
{
           container.RegisterType<ICompanyRepository, CompanyRepository>();
}


Add Company.cs class in the Models folder

public class Company
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
}


Add Interface ICompanyRepository with below code

public interface ICompanyRepository
    {
        IEnumerable<Company> GetAll();
        Company Get(int id);
        Company Add(Company item);
        bool Update(Company item);
        bool Delete(int id);
    }

Add Class CompanyRepository with below code 

public class CompanyRepository : ICompanyRepository
   {
   
       private readonly List<Company> products = new List<Company>();
       private int _nextId = 1;
 
       public CompanyRepository()
       {
           // Add products for the Demonstration  
           Add(new Company { Name = "TIMKEN Eng", Category = "Engenering" });
           Add(new Company { Name = "Wipro", Category = "software" });
           Add(new Company { Name = "HSBC", Category = "Bank" });
       }
 
       public IEnumerable<Company> GetAll()
       {
           // TO DO : Code to get the list of all the records in database  
           return products;
       }
       public Company Get(int id)
       {
           // TO DO : Code to find a record in database  
           return products.Find(=> p.Id == id);
       }
       public Company Add(Company item)
       {
           if (item == null)
           {
               throw new ArgumentNullException(nameof(item));
           }
 
           // TO DO : Code to save record into database  
           item.Id = _nextId++;
           products.Add(item);
           return item;
       }
       public bool Update(Company item)
       {
           if (item == null)
           {
               throw new ArgumentNullException(nameof(item));
           }
 
           // TO DO : Code to update record into database  
           int index = products.FindIndex(=> p.Id == item.Id);
           if (index == -1)
           {
               return false;
           }
           products.RemoveAt(index);
           products.Add(item);
           return true;
       }
       public bool Delete(int id)
       {
           // TO DO : Code to remove the records from database  
           products.RemoveAll(=> p.Id == id);
           return true;
       }
   }

Add CompanyController with below code 

public class CompanyController : Controller
    {
        readonly ICompanyRepository repo;
        public CompanyController(ICompanyRepository tempProduct)
        {
            this.repo = tempProduct;
        }
        public ActionResult Index()
        {
            var data = repo.GetAll();
            //return JsonConvert.SerializeObject(data);
            ViewBag.data = data;
            return View();
        }
    }

Add Company/Index.csthml with below code 

@using BookIOC.Models
 
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<a href="~/Views/Shared/_Layout.cshtml">~/Views/Shared/_Layout.cshtml</a>
<h2>Index</h2>
<div>
    <table>
        <thead>
        <tr>
            <th width="50px">Id</th>
            <th width="250px">Name</th>
            <th width="250px">Category</th>
        </tr>
        </thead>
        <tbody>
            @foreach (var v in (IEnumerable<Company>)ViewBag.data)
            {
                <tr>
                    <td>@v.Id</td>
                    <td>@v.Name</td>
                    <td>@v.Category</td>
                </tr>
            }
        </tbody>
    </table>
</div>

Run the App and it should be able to use the Unity in  your app.

Download and try code https://github.com/samridhis/UnityExample
 

Comments

Popular posts from this blog

Authorize.net Integration eCommerce Payment Gateway ( Direct Post Method New! )

Get Organised with OneNote

Test your Sql Query Online