Skip to content

Commit 10ca732

Browse files
committed
IoC Implementation with Factory Pattern
1 parent 94b7394 commit 10ca732

File tree

11 files changed

+236
-0
lines changed

11 files changed

+236
-0
lines changed

IoC_Principle/IoC_Principle.csproj

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{E6663652-D202-441B-AFE7-C6233140CE48}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>IoC_Principle</RootNamespace>
11+
<AssemblyName>IoC_Principle</AssemblyName>
12+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<DebugType>pdbonly</DebugType>
27+
<Optimize>true</Optimize>
28+
<OutputPath>bin\Release\</OutputPath>
29+
<DefineConstants>TRACE</DefineConstants>
30+
<ErrorReport>prompt</ErrorReport>
31+
<WarningLevel>4</WarningLevel>
32+
</PropertyGroup>
33+
<ItemGroup>
34+
<Reference Include="System" />
35+
<Reference Include="System.Core" />
36+
<Reference Include="System.Xml.Linq" />
37+
<Reference Include="System.Data.DataSetExtensions" />
38+
<Reference Include="Microsoft.CSharp" />
39+
<Reference Include="System.Data" />
40+
<Reference Include="System.Net.Http" />
41+
<Reference Include="System.Xml" />
42+
</ItemGroup>
43+
<ItemGroup>
44+
<Compile Include="WithIoC\AppDbContext\MongoDbContext.cs" />
45+
<Compile Include="WithIoC\AppDbContext\MsSqlDbContext.cs" />
46+
<Compile Include="WithIoC\ConnectionFactory.cs" />
47+
<Compile Include="WithIoC\DbConnection.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup />
51+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
52+
</Project>
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("IoC_Principle")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("IoC_Principle")]
13+
[assembly: AssemblyCopyright("Copyright © 2021")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("e6663652-d202-441b-afe7-c6233140ce48")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IoC_Principle.AppDbContext
8+
{
9+
public class MongoDbContext
10+
{
11+
public string GetData()
12+
{
13+
return "From MongoDb";
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IoC_Principle.AppDbContext
8+
{
9+
public class MsSqlDbContext
10+
{
11+
public string GetData()
12+
{
13+
return "From MsSql";
14+
}
15+
}
16+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using IoC_Principle.AppDbContext;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace IoC_Principle.WithIoC
9+
{
10+
public class ConnectionFactory
11+
{
12+
public static MongoDbContext GetConnection()
13+
{
14+
return new MongoDbContext();
15+
}
16+
}
17+
}

IoC_Principle/WithIoC/DbConnection.cs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using IoC_Principle.AppDbContext;
2+
using IoC_Principle.WithIoC;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace IoC_Principle
10+
{
11+
public class DbConnection
12+
{
13+
//Now the helper class is designed to import the DbConnection object instance.
14+
15+
16+
private MongoDbContext _mContext;
17+
public DbConnection()
18+
{
19+
_mContext = ConnectionFactory.GetConnection();
20+
}
21+
22+
public string GetData()
23+
{
24+
return _mContext.GetData();
25+
}
26+
}
27+
}

SoftwarePrinciples.sln

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DRY_Principle", "DRY_Princi
99
EndProject
1010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOC_SeparationOfConcernsPrinciple", "SOC_SeparationOfConcernsPrinciple\SOC_SeparationOfConcernsPrinciple.csproj", "{CD4B3A04-4157-4929-A950-99B19F4E4718}"
1111
EndProject
12+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IoC_Principle", "IoC_Principle\IoC_Principle.csproj", "{E6663652-D202-441B-AFE7-C6233140CE48}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{CD4B3A04-4157-4929-A950-99B19F4E4718}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{CD4B3A04-4157-4929-A950-99B19F4E4718}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{CD4B3A04-4157-4929-A950-99B19F4E4718}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{E6663652-D202-441B-AFE7-C6233140CE48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{E6663652-D202-441B-AFE7-C6233140CE48}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{E6663652-D202-441B-AFE7-C6233140CE48}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{E6663652-D202-441B-AFE7-C6233140CE48}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE

Solid_Principles/Solid_Principles.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
</Compile>
9797
<Compile Include="Program.cs" />
9898
<Compile Include="Properties\AssemblyInfo.cs" />
99+
<Compile Include="WithoutIoC\AppDbContext\MongoDbContext.cs" />
100+
<Compile Include="WithoutIoC\AppDbContext\MsSqlDbContext.cs" />
101+
<Compile Include="WithoutIoC\DbConnection.cs" />
99102
<EmbeddedResource Include="Properties\Resources.resx">
100103
<Generator>ResXFileCodeGenerator</Generator>
101104
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IoC_Principle.AppDbContext
8+
{
9+
public class MongoDbContext
10+
{
11+
public string GetData()
12+
{
13+
return "From MongoDb";
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace IoC_Principle.AppDbContext
8+
{
9+
public class MsSqlDbContext
10+
{
11+
public string GetData()
12+
{
13+
return "From MsSql";
14+
}
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using IoC_Principle.AppDbContext;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace IoC_Principle
9+
{
10+
public class DbConnection
11+
{
12+
//In this example, there is a tight bond between the two classes.
13+
/*
14+
*As soon as we change the class we use here to MsSQLDbContext
15+
*instead of MongoDbContext, it will be necessary to make manual
16+
*changes to the code in the DbConnection class and in all places
17+
*where it is used in other classes, if any. Therefore, changes made
18+
*in the MongoDbContext class will also be reflected in our DbConnection class.
19+
*/
20+
private MongoDbContext _mContext;
21+
public DbConnection()
22+
{
23+
_mContext = new MongoDbContext();
24+
}
25+
26+
public string GetData()
27+
{
28+
return _mContext.GetData();
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)