简体   繁体   中英

C# Boilerplate code

I'm thinking of building some generic extensions that will take a way all these null, throw checks and asserts and instead use fluent APIs to handle this.

So I'm thinking of doing something like this.

Shall() - Not quite sure about this one yet
    .Test(...) - Determines whether the contained logic executed without any errors
    .Guard(...) - Guards the contained logic from throwing any exception
    .Assert(...) - Asserts before the execution of the code
    .Throw(...) - Throws an exception based on a certain condition
    .Assume(...) - Similar to assert but calls to Contract.Assume

Usage: father.Shall().Guard(f => f.Shop())

The thing is that I don't want these extra calls at run-time and I know AOP can solve this for me, I want to inline these calls directly to the caller, if you have a better way of doing that please do tell.

Now, before I'm researching or doing anything I wonder whether someone already done that or know of a tool that is doing it ?

I really want to build something like that and release it to public because I think that it can save a lot of time and headache.

Some examples.

DbSet<TEntity> set = Set<TEntity>();

if (set != null)
{
    if (Contains(entity))
    {
        set.Remove(entity);
    }
    else
    {
        set.Attach(entity);

        set.Remove(entity);
    }
}

Changes to the following.

Set<TEntity>().Shall().Guard(set =>
{
    if (Contains(entity))
    {
        set.Remove(entity);
    }
    else
    {
        set.Attach(entity);

        set.Remove(entity);
    }
});

Instead of being funny and try to make fun of other people, some people can really learn something about maturity, you can share your experience and tell me what's so good or bad about it, that I'll accept.

I'm not trying to recreate Code Contracts, I know what it is I'm using it everyday, I'm trying to move the boilerplate code that is written to one place.

Sometimes you have methods that for each call you have to check the returned object and is not your code so you can't ensure that the callee won't result a null so in the caller you have to perform null checks on the returned object so I thought of something that may allow me to perform these checks easily when chaining calls.

Update: I'll have to think about it some more and change the API to make the intentions clear and the code more readable.

I think that the idea is not polished at all and that indeed I went too far with all these methods.

Anyways, I'll leave it for now.

听起来您正在描述类似代码契约的内容: http : //msdn.microsoft.com/zh-cn/devlabs/dd491992

If I understand what you're looking for, then the closest thing I've come up with is the extension method:

public static Chain<T>(this T obj, Action<T> act)
{
    act(obj);
    return obj;
}

This allows you to do the following:

Set.Remove(Set.FirstOrDefault(entity) ?? entity.Chain(a => Set.Add(a)));

As you can see, though, this isn't the most readable code. This isn't to say that Chain extension method is bad (and it certainly has its uses), but that Chain extension method can definitely be abused, so use cautiously or the ghost of programming past will come back to haunt you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM