Linq

Web

SSRS

Crystal Report

Sql Server

WPF

Recent Posts

Dec 25, 2016

Product application with Pluggable ASP.NET MVC application

This article discuss about how to develop ASP.NET MVC application which has loosely coupled modules (or features) which can be developed separately without any directly dependency and those modules can be plugged-in to main application without any extra bit of code.
Business Value:
This is specifically useful for product development. Using this approach, each module can be developed separately and deployed/shipped with product separately. This helps in building different version of product like: Basic, professional, premium and enterprise. This loosely coupled feature approach enables to either build separate installer with required features as per different versions or master setup which can install only defined features as per license key used during installation.
What is the trick to solving this?
A solution for the above problem is to develop application modules independently, without any direct dependencies among them and also on the main application code. Developers should always opt for a modular solution, in which larger modules are easily maintained over time, separately. Once each module is ready for integration, they could be then plugged into the main application with little to no adjustment of the same.
So in short, the structure of a complex asp.net MVC application can be very easily maintained using areas. The concept of this is to allow us to breakdown a large complex application into a several small sections called areas.
These areas can have their own set of:
 1. Models
 2. Views
 3. Controllers
 4. Routes
In this article we are going to develop an ASP.NET MVC application that contains loosely coupled modules (independent to main application), which we will further plug in the main application.
Benefits of this approach
This approach is extremely useful for product development. Utilizing this approach, each module may be developed and deployed for the product separately. This helps in building different versions of the product, like basic, professional, premium, and enterprise versions. It also enables product developers to create the isolation required to
ü Build separate installers with the required features for each different version
ü A master setup which can only install defined features as per license key used during installation, or
ü Some other configuration that requires the modular separation technique.
Demo for a Modular App
Here we will create a web application that contains the following modules:
· Sale
· Purchase
· Maintenance
· Marketing
· Stock
· Import
· Export
Step 1:
Open Visual Studio 2015 and create an ASP.NET MVC project as follows. Give it a meaningful name (in my case I have used MVCPluggableDemo).
Once it is created, your solution should look like this:
Step 2:
Right-click on the ‘Controllers’ folder and select “Add -> Controller”. Name it HomeController, and select “Empty MVC Controller” for the template:
Right-click on the Index method of the controller and select “Add View”. Using defaults, create the Index view for HomeController to form the landing page for the application:
Step 3:
Now edit the highlighted section in _Layout.cshtml (under the Views\Shared folder) in order to create menu items pointing to the desired modules:
Step 4:
Run the application and you will get the output as follows:
Now we will focus on creating separate modules for our application and then plug them into the main one.
Step 5:
Right-click on the main application project title (as in our case MVCPluggableDemo) in the Solution Explorer and select “Add->Area” from the context menu. This will create a basic folder structure for the Area framework. Specify “Sale” as Area name and click “Add”.
Then we have a folder solution structure that looks like:
Step 6:
Take a backup of the SaleAreaRegistration.cs file. This file will be needed later on when we add the Sale module to the solution as a separate project.
Now remove the Sale subfolder from the Areas folder, keeping the Areas folder in place.
Next add the new pluggable module for “Sale”, right-click on the solution and choose “Add-> New Project”.
· Select Empty ASP.NET MVC Application
· Name to project ‘Sale’
· Set Location as “…\MVCPluggableDemo\MVCPluggableDemo\Areas\
· A new project should be created in the Areas folder (previously created).
After this step, your solution should look like the one below:
As we saved new project Sale under the Areas folder, it appears as hidden folder under the MVCPluggableDemo/Areas folder in the Solution Explorer:
Step 7:
Adjust the MVCPluggableDemo’s RouteConfig.cs file to properly add the correct namespace overload to the routes.MapRoute() method:
We will also use the other overloaded version of routes.MapRoute() method which takes the namespace of the Controller.
Step 8:
Set the output directory of Sale project to ..\..\bin\ so that it’s compiled DLLs are placed in the bin directory of the MVCPluggableDemo application:
Setting related to connection strings, authentication, membership, role manager, profile, and session state are going to be placed in the web.config of your root directory. So remove all these section from the sale’s web.config.
Step 9:
· Create the Sale controller in the Sale project.
· Create an Index view and place the content “Welcome to Sales Module” in its header.
Step 10:
Now add SaleAreaRegistration.cs file (Backup file from step 6) to the root directory of the Sale project and change its namespace to MVCPluggableDemo; use the overloaded version of context.MapRoute() method which accepts the namespace of the controller to use as follows:
Step 11:
Now build the solution and check the MVCPluggableDemo\bin folder and you will have the compiled DLLs for the Sale module there:
Step 12:
Now run the application! If you have followed all the steps correctly, it will be working as expected.
You can follow the same steps and create other larger modules separately and plug them in to your application. Isn’t it simple yet such a powerful feature when it comes to product development?
Post your comments, doubts and queries below. The demo application is also available for download here. Happy coding!

Dec 5, 2016

ASP.NET MVC 5 Authentication Filters - Phân quyền cho action và controller MVC5






Chức năng phân quyền này mình dựa trên phần quyền của AspNet
Đầu tiên các bạn cần chuẩn bị DB (mình phân tích thì sử dụng các bảng sau)

Sau đó các bạn tạo lớp AuthAttribute.cs trong phần Models (ở đâu cũng được)
Lớp này để kiểm tra phân quyền tại các Controller nó giống Authorize

Sau đó bạn viết bổ sung function IsAuthenticated(this IIdentity identity, AuthenticationChallengeContext filterContext) function này dùng để kiểm tra User đăng nhập có quyền truy cập vào Action đang request tới hay không.
Mình tạo thêm lớp ControllerModel.cs để tiện cho việc truyền dữ liệu từ action qua view.
Tạo action GetController() trong RolesController.cs action này dùng để lấy tất cả những action của MVC. Hiện tại thì cái này đang lấy lên hết tất cả, mọi người có phương pháp nào lấy được theo Areas thì chia sẻ dưới comment nha. Thanks!
Tạo view cho action này, khi chạy view này xong thì dữ liệu sẽ được lưu xuống DB.

Ok giờ thì mình đã có tất cả Acction và Controller nếu muốn thì các bạn có thể vào db để xóa bớt, cái này chỉ cần chạy một lần, tuy nhiên khi có thêm Acction hoặc Controller cần phân quyền thì chạy lại nha.

Mình tạo thêm lớp AddController trong RolesController.cs để xét Action cho từng phân quyền của chúng ta.
View của AddController
Đây là giao diện khi hoàn thành.
Và cuối cùng chúng ta thụ hường thành quả của mình bằng cách add [AuthAttribute] vào các Controller bạn muốn phân quyền.

OK Cảm ơn các bạn đã xem bài viết. Nếu các bạn có ý kiến hay góp ý cho chức năng tốt hơn xin vui lòng để lại comment. 

Sep 16, 2016

How to join 3 tables with lambda expression?

Just a guess:
var myList = Companies
    .Join(
        Sectors, 
        comp => comp.Sector_code,
        sect => sect.Sector_code,
        (comp, sect) => new { Company = comp, Sector = sect })
    .Join(
        DistributionSectorIndustry.Where(dsi => dsi.Service == "numerical"), 
        cs => cs.Sector.Sector_code,
        dsi => dsi.Sector_code,
        (cs, dsi) => new { cs.Company, cs.Sector, IndustryCode = dsi.Industry_code })
    .Select(c => new {
        c.Company.Equity_cusip,
        c.Company.Company_name,
        c.Company.Primary_exchange,
        c.Company.Sector_code,
        c.Sector.Description,
        c.IndustryCode
});
http://stackoverflow.com/questions/9120088/how-to-join-3-tables-with-lambda-expression

Nam Le © 2014 - Designed by Templateism.com, Distributed By Templatelib