Sunday, December 20, 2015

Action injection in MVC6

Dependency injection is very flexible in MVC6 architecture. Now, it’s possible to inject dependency only in action level.

Earlier, if there was need to inject dependency then there were three possible ways

1) Constructor injection
2) Function injection
3) Property injection

Now, the common part of these technique is, it will resolve dependency in class level. So, if there is need to resolve dependency only in one function those techniques are not best fit.
In MVC6 Microsoft has introduce injection in function level. Just we need to use [FromService] 

attribute in parameter and it will resolve dependency from IoC container.

Let’s implement small example to understand the same. Here is implementation of Message service.

    public interface IMessage
        string Message { get; set; }

    public class Message : IMessage
        string _message;

        string IMessage.Message
                _message = "System message";
                return _message;
                _message = value;

Message class has implemented IMessage interface. Now, we have to register those in IoC container of MVC6. I am using default IoC container in this example.

public void ConfigureServices(IServiceCollection services)
                services.AddTransient<IMessage, Message>();

All done, now we are allowed to resolve dependency from action using “FromService” attribute.

public IActionResult Index([FromServices] IMessage Today)
            string message = Today.Message;
            return View();


Friday, December 18, 2015

Another story of a great mother.

Few days ago I have been in my hometown, went as routine visit from my workplace. Time is really fast when you spent it with your dearest person. I could not even imagine how 10 days passed just like 10 minutes.

Anyway, one evening my mom told that she has one marriage invitation from our neighbor village on this evening and started preparation to attend.

When she came back from marriage ceremony I was watching TV by sitting on sofa. I casually asked, whose marriage it was? Mom replied, it is the marriage of her friend’s house (I personally don’t know her but hear her name many times in time of conversation with mother). Mon started to tell how she became mate with her and why she respect and value to my mom!

It’s been very early when her husband passed away by leaving few kids along with her. She has no Idea to manage her and sons life. She took maid’s job and started to earn their living which was not really sufficient. Just to secure food for same day, she accepted hard work in a brick factory where physically strong people normally deny to work due to heavy labor.

In spite of being illiterate, she has great desire that her son will get good education. It seems that education is nothing but there day dream.

She decided, “I don’t have enough resource to educate all of my children but can educate one among them”. Now, the question is, to whom to choose?  She found that, the youngest one has real interest in education. She send him in a nearest Government school.

Situation became worst, now a large part of earning started to go behind his education (though government school is free, but there is expenditure of private tuition and education materials). To collect additional money she started begging door to door.

 Once she came to our house too. My mother asked, why she is begging (casually! just to know the reason). She replied, she wish “AT LEAST ONE OF HER SON WILL BE EDUCATED” and for sake of it, she started begging.

My mother got surprised (me too! when, I hear from my mother). Mother gave few books (English grammar etc., as my father is teacher of English subject and used to get book in free as specimen copy) copy/note of mine and get her promise that “In any situation , she will not stop her son’s education”.

She said Didi ! (Elder sister in Bengali, she used to call my mom like this) I faced that day, when alms is not enough for all family member, I distributed rice of my part among my children and drunk water at night. If I can handle this situation, I can handle any situation.

What is great sacrifice! Yes, she kept her promise, she never stopped her son’s education. Today her son became teacher of a government school and and and…? Yes, my mother just return by attending his marriage.

I asked, how the bride looks like. Replied: very nice... hahhah... 

Inject service to View in MVC6

This is new feature of MVC6, you can inject service to view directly. It will help to push/pull data from UI to business logic directly by skipping controller layer.

Here, we will implement small example to demonstrate the concept.
     1)      At first we will create one simple service like this.

namespace MVC6Web.Services
    public class Person
        public int Id { get; set; }
        public string Name { get; set; }

    public class DataService
        Person _person;
        public DataService()
            _person = new Person
            Id = 1, Name = "Sourav Kayal" };

        public Person GetPerson()
            return _person;

DataService is simple C# class containing GetPerson() function which will return an object of Person class.
      2)      Make the service visible to MVC environment.
 In this point we will attach DataService to environment of current project. So, modify the ConfigureService function like below.

public void ConfigureServices(IServiceCollection services)
      3)      Inject DataService to view.
This is the final step, here we will inject the service to view. Here is the procedure to get it done.

@inject MVC6Web.Services.DataService DataService   

    <li>Item : @DataService.GetPerson().Name</li>

The format to inject to Service is like this.

@inject <ServicePath> <Identifier>

Once we inject service, we are allowed to call GetPerson() function from view itself, though the function is not part of controller.