locked
Instantiate a class with contractor for IoC RRS feed

  • Question

  • User-1132163444 posted

    Hi, Please see code, i hope someone can advise me,

    i have these Class

    public class Membership : IMembership
        {
            IUnitOfWork unitOfWork;
            public Membership(IUnitOfWork _unitOfWork)
            {
                unitOfWork = _unitOfWork;
            }
            public UserViewModel Validate(string Username, string Password)
            {
                //Check user if exist
                var user = unitOfWork.GetRepository<SecurityUser>().Find(o => o.Username == Username).FirstOrDefault();
                if (user != null)
                {
                    if (StringHash.ValidatePassword(Password, new StringHashModel { hash = user.Password, salt = user.Salt }))
                    {
    // ToModel => Extension Method return Entity.ToModel<UserViewModel, SecurityUser>(user); } } return null; } }

    I need to call this "Validate" Method in the Controller, but as you can see, for me to call this method, i need to instantiate the "Membership" class that has a "IUnitOfWork" parameter in the constractor,

    My problem is I'm hoping that i will no longer need to expose the "IUnitOfWork" in my controller, is there an alternative way or better to do this?
    Thanks

    Saturday, September 16, 2017 10:03 AM

Answers

  • User-1132163444 posted

    Hi, for the sake of resolving this issue, this is what i did.

    1. I Declare the IMembership in my Unity Config

    //UnitOfWork and Entities are all in a separate Project Where only the Process project (business logic) has access
    public static class UnityConfig //<== This is not in the MVC project, its in the same Class Library project along with Membership class
        {
            public static UnityContainer RegisterComponents()
            {
    	    var container = new UnityContainer();
                container.RegisterType<IMembership, Membership>(); //<== this was not here before
                container.RegisterType<IUnitOfWork, UnitOfWork<Entities>>();
    
                return container;
            }
        }

    2. If you would look in the codes i have shared, this how my controller looks like

     public class HomeController : Controller //<= This does not have any direct access to UnitOfWork and most specially Entities
        {
            IMembership membership;
            public HomeController(IMembership _membership)
            {
                membership = _membership;
            }
    }

    3. My Membership class still looks the same, it will still reply on Unity for the UnitOfWork as you can see on item one

    public class Membership : IMembership
        {
            IUnitOfWork unitOfWork;
            public Membership(IUnitOfWork _unitOfWork)
            {
                unitOfWork = _unitOfWork;
            }
    }

    This way, i was still able to get what i want, where i was able to independently separate UnitOfWork and the Entities to my Controller

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 18, 2017 10:55 AM

All replies

  • User475983607 posted

    First, what IoC container are you using?  Commonly, IoC containers are configured at the application start to inject concrete types into the constructor (not the contractor).  How to configure the container is covered in the IoC documentation.

    Saturday, September 16, 2017 1:33 PM
  • User-1132163444 posted

    Hi, Thanks for your reply,

    I used Unity,  and yes i already declared my Resolver in the Global.asax

     public class MvcApplication : System.Web.HttpApplication
        {
            protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();
                GlobalConfiguration.Configure(WebApiConfig.Register);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
    
                //Unity Dependency Injection
                DependencyResolver.SetResolver(new UnityDependencyResolver(UnityConfig.RegisterComponents()));
            }
        }

    This is the structure of my app, please tell me if i'm doing it wrong....

    Project/Context =>this contains all Entities

    Project/Access => this contains all repositories that calls the Entities

    Project/Process=> this holds all business logics which includes the calling of Repositories to get some data, this is also where i add the Interface of the REPO class in the controller, so technically this is were the UNITY IoC is used

    Project/MVC => this is the UI where i call the Process Methods via Controller, this is also where i call in the Global.asax the Unity Resolver

    Sunday, September 17, 2017 2:32 AM
  • User-1132163444 posted

    Hi, for the sake of resolving this issue, this is what i did.

    1. I Declare the IMembership in my Unity Config

    //UnitOfWork and Entities are all in a separate Project Where only the Process project (business logic) has access
    public static class UnityConfig //<== This is not in the MVC project, its in the same Class Library project along with Membership class
        {
            public static UnityContainer RegisterComponents()
            {
    	    var container = new UnityContainer();
                container.RegisterType<IMembership, Membership>(); //<== this was not here before
                container.RegisterType<IUnitOfWork, UnitOfWork<Entities>>();
    
                return container;
            }
        }

    2. If you would look in the codes i have shared, this how my controller looks like

     public class HomeController : Controller //<= This does not have any direct access to UnitOfWork and most specially Entities
        {
            IMembership membership;
            public HomeController(IMembership _membership)
            {
                membership = _membership;
            }
    }

    3. My Membership class still looks the same, it will still reply on Unity for the UnitOfWork as you can see on item one

    public class Membership : IMembership
        {
            IUnitOfWork unitOfWork;
            public Membership(IUnitOfWork _unitOfWork)
            {
                unitOfWork = _unitOfWork;
            }
    }

    This way, i was still able to get what i want, where i was able to independently separate UnitOfWork and the Entities to my Controller

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 18, 2017 10:55 AM