Thursday 20 March 2014

Dependency Injection in MVC , Web Api

Dependency Injection :


What is this? Why we use this?

feel free and learn from Google, Many peoples are write more articles for there

See,

Explains from Code project

Explains from MSDN


Now we'll see, How to implement this in our local application?

First create a class and interface and Implement the interface into the class

public class CompanyService : ICompanyService
    {
        public AppContext _salesAppContext = new AppContext();    // this is object reference   for your entity 

        public IList<Company> GetAll()

        {
            return _salesAppContext.Companies.ToList();
        }

        public Company Create(Company model)

        {
            throw new NotImplementedException();
        }

        public Company Update(Company model)

        {
            throw new NotImplementedException();
        }

        public Company Delete(Company model)

        {
            throw new NotImplementedException();
        }

        public Company Get(int modelId)

        {
            throw new NotImplementedException();
        }

        public Company Get(Guid modelId)

        {
            throw new NotImplementedException();
        }
    }

Interface


 public interface ICompanyService
    {
        TModel Create(TModel model);
        TModel Update(TModel model);
        TModel Delete(TModel model);
        TModel Get(int modelId);
        TModel Get(Guid modelId);
        IList<TModel> GetAll();
    }

Okay, once it's fine . Then you need to add a Nuget package named as  "NUnit" . and  add the below methods in your global.asax file

public static void Initialise()
        {
            var container = BuildUnityContainer();

            // DependencyResolver.SetResolver(new UnityDependencyResolver(container));

            // register dependency resolver for WebAPI RC
            GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
            // if you still use the beta version - change above line to:
            //GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new Unity.WebApi.UnityDependencyResolver(container));
        }



        private static IUnityContainer BuildUnityContainer()

        {
            var container = new UnityContainer();
            container.RegisterType<ICompanyService, CompanyService>();
            return container;
        }

and also you must add this namespace

using Microsoft.Practices.Unity;


Now the sample configurations are done. Then you need to call the Initialise() method into the Application_Start()  Method 

That Look like 

  protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //  Database.SetInitializer<SalesAppContext>(null);
            GlobalConfiguration.Configuration.MessageHandlers.Add(new CorsHandler());
            Initialise();

        }
Done.

How to reduce angular CLI build time

 Index: ----- I am using angular cli - 7 and I am going to tell how to reduce the build time as per my knowledge. Problem: -------- Now the ...