<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Marek Żyła on Medium]]></title>
        <description><![CDATA[Stories by Marek Żyła on Medium]]></description>
        <link>https://medium.com/@marekzyla95?source=rss-d22a6f6328e6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*y5nJ1vAsWP8GDRIAqyeJ2A.jpeg</url>
            <title>Stories by Marek Żyła on Medium</title>
            <link>https://medium.com/@marekzyla95?source=rss-d22a6f6328e6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 02:21:32 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marekzyla95/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Mongo Repository in .NET Core]]></title>
            <link>https://medium.com/@marekzyla95/mongo-repository-pattern-700986454a0e?source=rss-d22a6f6328e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/700986454a0e</guid>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[generics]]></category>
            <category><![CDATA[repository-pattern]]></category>
            <category><![CDATA[mongodb]]></category>
            <dc:creator><![CDATA[Marek Żyła]]></dc:creator>
            <pubDate>Tue, 24 Dec 2019 00:12:51 GMT</pubDate>
            <atom:updated>2019-12-24T00:12:51.525Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*75A-U34x1AgwNiQiAywTjg.png" /></figure><h3>Generic Mongo Repository pattern implemented in .NET Core</h3><h3>Jump into code</h3><p>Hello, today I want to present you modern Mongo Generic Repository implemented in loved by back-end developers .NET Core, let’s jump right into the shark’s mouth! 🦈</p><h3>Quick Start</h3><h4>Packages installation</h4><p>Okay, to get started we have to prepare our environment, let’s install necessary .nuget packages, you can do it in several ways:</p><ul><li>using .NET CLI<br>dotnet add package MongoDB.Driver</li><li>using Package Manager<br>Install-Package MongoDB.Driver</li></ul><h4>Mongo Db server configuration</h4><p>In this article I hope you already have MongoDb server instance configured. I’m using <strong>Docker</strong> to run my MongoDb server. The most important thing is to get proper connection string while connecting to our database. My server is running on localhost:32772.</p><h4>Configure Startup</h4><p>Once, we have our packages installed, what we need to do, is to connect our application to Mongo database. Let’s do it!</p><p>Modify appsettings.json to provide proper connection string and create database name. I created additional JSON object containing important settings.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18b0718aa37d40a267f618af580e85fc/href">https://medium.com/media/18b0718aa37d40a267f618af580e85fc/href</a></iframe><p>Now, let’s create <strong>settings </strong>class to keep clean code inStartup.cs when it comes to types registration.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04c4a76634cfa0eecb15abe7ec4d463e/href">https://medium.com/media/04c4a76634cfa0eecb15abe7ec4d463e/href</a></iframe><p>Next, we have to register our MongoDb client instance in Startup.cs using previously prepared settings class. Do it <strong>before </strong>AddControllers() in ConfigureServices method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/47494418c994a6d2c2b3580c666c69bc/href">https://medium.com/media/47494418c994a6d2c2b3580c666c69bc/href</a></iframe><h3>Repository implementation</h3><h4>Document preparation</h4><p>We’ll start our implementation by creating proper abstraction layer for our documents (<em>entities </em>in Mongo). I created a class and interface that contains base information about each document</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d9c8b56d45264a6d44c3fde11222ca9/href">https://medium.com/media/5d9c8b56d45264a6d44c3fde11222ca9/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b0f393851d06dbe3a686f12f825e547c/href">https://medium.com/media/b0f393851d06dbe3a686f12f825e547c/href</a></iframe><p>As you can see, Id property has Bson attributes for configuration. It tells our Mongo Driver to treat this property as native mongoobjectId.</p><h4>Collection naming</h4><p>Mongo Driver lacks attribute that allows us to set the collection name for documents, so I created one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89eb531d7ce504749d303fda39267bd4/href">https://medium.com/media/89eb531d7ce504749d303fda39267bd4/href</a></iframe><p>And here is this attribute in action for our sample document</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/695d9b8885d5527f56ec1fb4522317ba/href">https://medium.com/media/695d9b8885d5527f56ec1fb4522317ba/href</a></iframe><p>As you can see, we have a class called Person, but actual collection in database will have name of people.</p><h4>Repository</h4><p>Oh and that’s what you’ve been waiting for! Finally, repository pattern implemented in dotnet core. We have here interface and its implementation, I definitely suggest you putting it in separate files, because we have here a lot of methods.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c80331118e6305fc47797fa61b08ab6/href">https://medium.com/media/9c80331118e6305fc47797fa61b08ab6/href</a></iframe><p>This interface defines all public methods that our repository will serve. As you can see there are special methods for each CRUD operation. Below I pasted an implementation and longer description of each method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92736d4669ea640b72846ad2941cd2e6/href">https://medium.com/media/92736d4669ea640b72846ad2941cd2e6/href</a></iframe><h4>Repository implementation explained</h4><p>Constructor<strong>: </strong>it connects to our database server by MongoClient and gets the proper database by GetDatabase method. Next, we get collection data and store it in private field called _collection.</p><p>GetCollectionName: this method uses our previously prepared attribute and gets documents collection by type of document provided in parameter (<em>ex. when we pass </em><em>Person as the parameter, this method will return “people” as a result</em>).</p><p>AsQueryable: this method let’s us to get whole non-materialized collection and perform some operations later (it’s slower than other methods because adds extra abstraction layer).</p><p>FilterBy: those methods allows us to filter data by sending expressions in parameters. This works similar to <em>LINQ</em>, but operations are performed on database, so it’s much faster than doing it later in code-side. I provided extra parameter called projectionExpression, this parameter is useful, when you want to get only some fields, rather than full objects from filtered results. You can also add sorting when necessary, simply using Sort method provided by IFindFluent interface.</p><p>FindOne and FindById: those methods are here to get single objects from database, I provided here also asynchronous implementations.</p><p>InsertOne and InsertMany: methods are used to insert documents to our database, also implemented in synchronous and asynchronous ways.</p><p>ReplaceOne: this method is used to replace documents in our application. You can also use Update method if you need.</p><p>DeleteOne and DeleteById: those are for hard-deleting documents in our app. When it comes to business application, i’d also suggest you using soft-delete, by simply updating some field called documentState and setting it to deleted. It’ll secure your data from being deleted by accident or some other cases.</p><h3>Usage of Repository</h3><h4>Registering repository in Startup</h4><p>As we finished implementation, the last thing to do, is to register our repository in Startup.cs in ConfigureServices method.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a778634116a08a5fadabd7868b4b8511/href">https://medium.com/media/a778634116a08a5fadabd7868b4b8511/href</a></iframe><h4>Usage in API</h4><p>Eventually, we can use our mechanism to perform some operations in actual application. Here, I have sample controller, you should never use your repository there, but it’s just to make this article shorter. When it comes to real application, consider adding some application services to communicate with your <strong>data layer</strong> which repository belongs to.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/285282f72820484cd11912fb7cce6826/href">https://medium.com/media/285282f72820484cd11912fb7cce6826/href</a></iframe><h3>Conclusion</h3><p>That’s all for today, we implemented together Mongo Generic Repository, using MongoDb Driver. You can also make it even faster by creating correct indexes for each collection, but this is a topic for separate article.</p><p>If you appreciate my work, don’t forget to react for this story by positive feedback. I have also other interesting articles on my profile, so make sure to check them out!</p><p><strong>Thanks.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=700986454a0e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mapping database tables using code-first approach and Entity Framework Core]]></title>
            <link>https://medium.com/@marekzyla95/entityframeworkmapping-c2e3f9bbe831?source=rss-d22a6f6328e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c2e3f9bbe831</guid>
            <category><![CDATA[database]]></category>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[csharp]]></category>
            <category><![CDATA[entity-framework-core]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <dc:creator><![CDATA[Marek Żyła]]></dc:creator>
            <pubDate>Mon, 18 Mar 2019 17:36:00 GMT</pubDate>
            <atom:updated>2019-03-18T18:03:03.624Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z1F6nzcHsutLL624h1zqoQ.png" /></figure><p>Hello, in this article I’d like to show you a proper way of mapping C# classes into database tables using <strong>Entity Framework Core</strong> and code-first<strong> </strong>approach. This tutorial focuses on simple mappings, just to show you a proper way how to map entities (one-to-one, one-to-many and owns-one), in the next article I’ll show you more advanced mappings. I’ll be very happy if you left your <strong>comments </strong>right below the end of this article if you find this interesting. Let’s get started!</p><h3>Entity Framework Core installation</h3><p>Firstly we have to install Entity Framework Core from .nuget gallery. I’m using here dotnet cli:</p><p>Package-Manager: Install-Package Microsoft.EntityFrameworkCore<br>dotnet cli: dotnet add package Microsoft.EntityFrameworkCore</p><p>Now we’re going to add DbContext in Startup.cs . First, let’s prepare proper database server by your own. Then we must prepare <em>connection string</em> that’ll tell Entity Framework where it should search for our database. Here’s an example of mine (you should prepare another that meets your db name and ports):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e98dc6a0a2ef8c27dd2cb41bb7fd0c6/href">https://medium.com/media/0e98dc6a0a2ef8c27dd2cb41bb7fd0c6/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d28fe3cba89a0a5b57f433db0f514750/href">https://medium.com/media/d28fe3cba89a0a5b57f433db0f514750/href</a></iframe><p>As we’ve already finished configuring our db connection, let’s move forward and create simple classes, that we want to map. In my case, these are classes about cars.</p><h3>Creating database entities</h3><p>Firstly let’s prepare base entity class, because all of our entities should have ID, creation date and modification date. Here’s my example that I prepared for you.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13c92d50afaf59ad6729eb8c26bd717f/href">https://medium.com/media/13c92d50afaf59ad6729eb8c26bd717f/href</a></iframe><p>As you can see, we have here also EntityStatus to manage our records state, especially to enable <em>soft-delete.</em></p><p>Now we can continue creating our entities inheriting from EntityBase class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b87bd5ec974e077140052013a49c3437/href">https://medium.com/media/b87bd5ec974e077140052013a49c3437/href</a></iframe><p>As you can see, these are very simple classes, just to show you core functionality. Address class doesn’t inherit from EntityBase, because we want Customer to own Address. Let’s map them to database tables.</p><h3>Mapping entities</h3><p>This is my approach of mapping entities using base mapping class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c0a5974edd5a0636ea000bc0fb5e7ea0/href">https://medium.com/media/c0a5974edd5a0636ea000bc0fb5e7ea0/href</a></iframe><p>This class provide EntityBase<em> </em>mapping, but also it has interesting abstract method ConfigureEntity. I decided to put this method here, because it enforces other classes to implement mapping while it maps base properties under cover. That’s the difference between my and other approaches. You don’t need here to map all of your props in each entity, which means that you won’t make any mistake while mapping and of course you won’t copy your code in each entity class.</p><p>Here are specific mappings that all useEntityBaseConfiguration class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da78b0b009b9726b4984a72b14d2335a/href">https://medium.com/media/da78b0b009b9726b4984a72b14d2335a/href</a></iframe><p>Here we’ve mapped all of our classes. We have <strong>one-to-many </strong>relation between <strong>Car</strong> and <strong>Customer </strong>tables. We have also <strong>one-to-one<em> </em></strong>relation between <strong>Car </strong>and <strong>Engine</strong> tables. And finally, <strong>Customer</strong> class owns one <strong>Address</strong>, which means that <strong>Address </strong>properties’ll be connected with <strong>Customers </strong>table. Finally we can apply those mappings in our DbContextclass:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/84011eea4688eb21aa80867776b0da37/href">https://medium.com/media/84011eea4688eb21aa80867776b0da37/href</a></iframe><p>That’s all for today, I know that there are a lot of more advanced mappings that we can perform in EntityFramerowk, but I decided to put it in separate article. So in the next article you’ll see <strong>TPH</strong>, <strong>TPT </strong>and <strong>many-to-many</strong> maps. Make sure to <strong>follow me</strong>, then you <strong>won’t miss</strong> any new things.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c2e3f9bbe831" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scrutor, a Dependency Injection types registration library for ASP.NET Core]]></title>
            <link>https://medium.com/@marekzyla95/scrutor-a-dependency-injection-types-registration-library-for-asp-net-core-c0a73af47905?source=rss-d22a6f6328e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0a73af47905</guid>
            <category><![CDATA[aspnetcore]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[microsoft]]></category>
            <dc:creator><![CDATA[Marek Żyła]]></dc:creator>
            <pubDate>Thu, 24 Jan 2019 19:11:35 GMT</pubDate>
            <atom:updated>2019-03-01T00:10:45.060Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QDe3fAtdtwhWGsyL4xlPRA.png" /></figure><h3>Register types automatically using Scrutor library for ASP.NET Core</h3><p>Hello, today I would like to introduce you to one of most fancy libraries that I found recently on .nuget. Scrutor is a supporting library for <em>Microsoft.Extensions.DependencyInjection,</em> it allows to register collections of types in our DI container. Here, I want to propose you one of strategies that you can use when it comes to types registration in your application.</p><h4>Step 1:</h4><p>First of all, we have to install Scrutor from .nuget, we can find this using <em>Manage Nuget packages</em> tool in Visual Studio/Rider, or using console and typing this command:</p><ul><li>using Package Manager: Install-Package Scrutor</li><li>using .NET Core cli: dotnet add package Scrutor</li></ul><h4>Step 2:</h4><p>Next step that we have to do is to prepare interfaces called <em>assembly markers </em>in this context. <em>Assembly Markers</em> are empty interfaces placed in each project’s root. Here’s an example of that interface</p><pre>public interface <strong>IOurAssemblyMarker</strong><br>{<br>}</pre><p>Yeah, you might be wondering why we’re using empty interface here😮. Let me show you. We prepared this interface only for purpose of finding our assembly by Scrutor, now we’re going to use it as a <em>marker.</em></p><h4>Step 3:</h4><p>Lets move to Startup.cs and use our <em>markers</em> to register types from that assembly (project/class library).</p><pre>services.Scan(sc =&gt;<br>            sc.FromCallingAssembly()<br>              .FromAssemblies(<br>                typeof(<strong>IOurAssemblyMarker</strong>).Assembly,<br>                typeof(IAnotherAssemblyMarker).Assembly<br>            )<br>            .AddClasses()<br>            .AsImplementedInterfaces()<br>        );</pre><p>Here we have registered types from assemblies (projects) that contain <em>IOurAssemblyMarker </em>and <em>IAnotherAssemblyMarker</em> interfaces, as implemented interfaces.</p><p>Now, we can also filter our interfaces by standard LINQ queries.Here’s an example:</p><pre>.AddClasses(@class =&gt;<br>        @class.Where(type =&gt;<br>              !type.Name.StartsWith(&#39;I&#39;)<br>              &amp;&amp; type.Name.EndsWith(&quot;Repository&quot;)<br>               )<br>           )</pre><p>As you probably noticed, we can register types on different ways in ASP.NET Core. To change the way that certain interfaces group is registered, all we have to do is to use proper method, here’s the list of avaiable:</p><ul><li>AsImplementedInterfaces</li><li>AsSelf</li><li>AsMatchingInterface</li><li>AsSelfWithInterfaces</li></ul><p>And thats all for today, hope you enjoyed this simple tutorial. Don’t forget leaving a <strong>clap</strong> if you found this article useful 😉 and share with your developers friends.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0a73af47905" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generic Repository Pattern implemented in .NET Core with EF Core]]></title>
            <link>https://medium.com/@marekzyla95/generic-repository-pattern-implemented-in-net-core-with-ef-core-c7e088c9c58?source=rss-d22a6f6328e6------2</link>
            <guid isPermaLink="false">https://medium.com/p/c7e088c9c58</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[entity-framework-core]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[net-core]]></category>
            <category><![CDATA[repository-pattern]]></category>
            <dc:creator><![CDATA[Marek Żyła]]></dc:creator>
            <pubDate>Tue, 13 Nov 2018 14:41:05 GMT</pubDate>
            <atom:updated>2018-11-13T14:41:05.424Z</atom:updated>
            <content:encoded><![CDATA[<h3>Generic Repository Pattern implemented in .NET Core with EF Core</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mNbzuPzTPvgTq_UiegyQgw.png" /></figure><p><em>When we think about creating CRUD system with data access through Entity Framework ORM, it’s really recommended not to use pure context, but to hide this in repositories. This way let us to do many things before our record finally go to database, for example we can check if record isn’t null, or instantly send some events to other application layers. Many developers use Non generic repositories, so they preparing a reporitory for each entity (or aggregate root if you have DDD architecture), because they think that generics cannot replace exact types. It’s partly true, but we can prepare generic repository for small project, because we don’t need super complicated architecture and of course in bigger projects we can use generic repository and then create derived types that override generic methods if necessary. In this article you’ll get knowledge how to prepare generic repository using Entity Framework Core 2.x+. Let’s start coding!</em></p><p>First, let me create simple database entity classes that are necessary to create generic repository.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d38ef8f268eb378be1377a383229bce/href">https://medium.com/media/6d38ef8f268eb378be1377a383229bce/href</a></iframe><p>Now we have to implement our DbContext, I skipped this part, because it depends on your database classes.</p><p>So let’s implement our generic repository, I’ll start with it’s interface because we have to satisfy Inversion Of Controlpattern that is needed to use Dependency Injection in our application. If you don’t know IoC and DI patterns, learn it immediately because those are very important.</p><p>So we have our IRepository:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7400ff60631a932526abb710b45d18ab/href">https://medium.com/media/7400ff60631a932526abb710b45d18ab/href</a></iframe><p>As you can see, we have generic type TEntity which must be type of IEntity and class . This small trick secure our Repository from using classes that are not in database. Now, when we want to use Repositoty, we must use classes that inherit from IEntity interface.</p><p>So finally, let’s implement our IRepository :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef83b8db93b19426415e63f5e3c13ddd/href">https://medium.com/media/ef83b8db93b19426415e63f5e3c13ddd/href</a></iframe><p>I used here _context.Set&lt;TEntity&gt;() to grab only entities from table of type TEntity . We can see, that there are two main sections in this repository, read side and write side. This sections could be possibly separated, especially when you use CQRS pattern to accessing your data and you need such a separation.</p><p>On the read side, here we have interesting Filter method, one is pure for getting all records from table, while second overload has Func&lt;TEntity,bool&gt; predicate parameter. This overload let’s us to filter our results exactly like Linq.Where do. As a result it’s neccessary to return IEnumerable collection, or IQueryable, because those types don’t materialize our records till we need to.</p><p>That’s all for today, hope you enjoyed my implementation of Generic Repository pattern, sorry for all issues in text, it’s my first publication :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c7e088c9c58" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>