How to create factory partern using C#.asp.net mvc5 application

coder rock 356 Reputation points
2023-10-19T14:01:42.9166667+00:00

How to create factory partern using C#.asp.net mvc5 application

I have created mvc5 application and Done Registration and login using simple ado.net to insert and getting the things

but i need to implement factory pattern to make my application fast and reuseable, but i am new to in this architech factory pattern.

if you provide any sampling code using factory pattern to insert using C# ado.net.

Thank you

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,507 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,010 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 29,751 Reputation points Microsoft Vendor
    2023-10-23T01:56:27.7033333+00:00

    Hi @coder rock,

    Please suggest me we are developing an mvc web application and which one is the best

    First, the two patterns are not related; the factory pattern is a creation pattern, while the repository pattern is a structural pattern. Factories and repositories work well together.

    In some cases you can delegate responsibility for object creation from your repository to your factory classes when you're reconstructing objects from the data store.

    client <=> repository -> factory
                   |
                   v
                database
    
    1. The client requests an object from the repository.
    2. The repository queries the database.
    3. The repository sends raw data to the factory.
    4. The factory returns object.

    Depending on your situation, I personally think using repository pattern is enough.

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 66,706 Reputation points
    2023-10-19T16:02:02.15+00:00

    the factory pattern is for configuration and customization, not performance. the most common factory pattern with ado.net is the repository pattern. say you wanted your code to work a relational data like sql, and no-sql like mongo db.

    you would define an interface for all the database actions. then you would write an implementation for each database type you wanted to support.

    now when you need to create an instance of the repository interface, you need to know the database type desired. here the factory pattern can be used.

    IMyRespository repo = MyRespository.Create();

    a simple implementation might be:

    public class MyRespository
    {
         public static IMyRespository Create()
         {
             switch (setting.Type)
             {
                case "MONGODB": return new MongoDBRepo();  
                case "SQL": return new SqlRepo();
                default: throw ...
             }
         }
    }
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.